allthingssecurity opened a new pull request, #30245: URL: https://github.com/apache/camel/pull/30245
# Description [CAMEL-27039](https://issues.apache.org/jira/browse/CAMEL-27039) Follow-up of #30001 (CAMEL-26953), named as out of scope in its review: `EventNotifierProducer.process` has the same catch that sets the exception and calls the callback again. The same rule as CAMEL-26882 and CAMEL-26953: the callback of a send is called once, also when an `AsyncProducer` breaks the async contract (calls the callback and then throws). `EventNotifierProducer` is the outer producer that `DefaultDeferServiceFactory` makes, so it is the producer injected with `@EndpointInject` / `@Produce` into a `Producer` field, and the producer behind the `@Produce` proxies (`ProxyHelper`). It calls `SendHelper.send` and caught `Exception` by setting it on the exchange and calling the callback with `true`. When the producer of the endpoint called the callback and then threw, the exchange was completed twice: the callback of the caller was called a second time, and the exchange, which the producer had completed, then carried the exception. (The unit of work processor between them, `CamelInternalProcessor`, throws such an exception on when the callback was called first.) A sync `producer.process(exchange)` on an injected producer saw a failed exchange. Change, as `SendProcessor.SentCallback` in #30001: `EventNotifierProducer.process` gives the producer a small callback per send (`SendCallback`, an `AtomicBoolean`) that records that it was called and calls the callback of the caller. A throw after it was called is logged at WARN, not set on the exchange, and `process` returns the `doneSync` the producer passed. A throw before it sets the exception and calls the callback, as before. A further call of the callback by the producer is logged and passed on to the callback of the caller, as on main. An `Error` is thrown on as before (the catch is still `Exception`, CAMEL-26741). The events are unchanged: `SendHelper.send` fires the sent event in the callback, so once. Cost: one small object and one compare-and-set per send of an injected producer. Tests, new `EventNotifierProducerProducerThrowsTest` (an `AsyncProducer` that sets the body, calls the callback and then throws, or throws first): - `testCallbackThenThrowCompletesOnce`: the callback is called once, `process` returns `true`, the exchange has no exception and keeps the body; - `testCallbackThenThrowWithEventsCompletesOnce`: the same with an event notifier, which gets one sending and one sent event; - `testInjectedProducerCallbackThenThrowSucceeds`: the producer that `@EndpointInject` injects (`DeferServiceFactory`), `process(exchange)`: the exchange has no exception; - `testThrowBeforeCallbackFailsTheExchange`: a control, the exchange fails with the exception and the callback is called once. Without the change the three tests other than the control fail on main (the callback called twice, the exception set on the completed exchange); with it all four pass, and the camel-core tests pass (9435 run, 0 failures, 45 skipped; 12 errors that are not related: 11 `DumpModel*` tests with a `NoClassDefFoundError` of `OgnlExpression` from a stale camel-yaml-io jar in my local repository, and `ValidatorExternalResourceTest`, which needs network access to raw.githubusercontent.com). Upgrade guide (4.24), own section next to the one of CAMEL-26953: an injected producer whose producer calls the callback and then throws no longer fails the exchange; the exception is logged at WARN. Not changed here, and a possible follow-up JIRA: when the producer behind an injected producer throws before it calls the callback, the unit of work of `UnitOfWorkProducer` is not done (the `CamelInternalProcessor` there throws the exception on without its after advice), so the exchange stays in the inflight repository. A throwaway test showed it with this change, and that path is the same on main; it is the same gap #30001 closed for the sync send of the producer cache. Backport: camel-4.18.x has the same catch in `EventNotifierProducer` (with its own sent-event callback instead of `SendHelper`), so the change needs a small manual port there. # Target - [x] I checked that the commit is targeting the correct branch (Camel 4 uses the `main` branch) # Tracking - [x] If this is a large change, bug fix, or code improvement, I checked there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for the change (usually before you start working on it): [CAMEL-27039](https://issues.apache.org/jira/browse/CAMEL-27039). # Apache Camel coding standards and style - [x] I checked that each commit in the pull request has a meaningful subject line and body. - [ ] I have run `mvn clean install -DskipTests` locally from root folder and I have committed all auto-generated changes. (I built the core modules with the formatter and import-sort plugins and ran the camel-core tests. I did not run the full root build.) # AI-assisted contributions - [x] If this PR includes AI-generated code, commits have proper co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR description identifies the AI tool used. This PR was prepared with Claude Code (Claude Opus 5.5). The commits carry a `Co-Authored-By` trailer. _Claude Code on behalf of allthingssecurity_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
