allthingssecurity opened a new pull request, #30250:
URL: https://github.com/apache/camel/pull/30250

   # Description
   
   [CAMEL-27047](https://issues.apache.org/jira/browse/CAMEL-27047)
   
   Follow-up of CAMEL-27039 (the PR that opens in the same batch, 
`EventNotifierProducer`: a producer that calls the callback and then throws), 
named there as out of scope: when the producer behind an injected producer 
throws before it calls the callback, the exchange stays in the inflight 
repository.
   
   The producer injected with `@EndpointInject` / `@Produce` into a `Producer` 
field, and the producer behind the `@Produce` proxies, is made by 
`DefaultDeferServiceFactory`: `EventNotifierProducer` around 
`UnitOfWorkProducer` around the producer of the endpoint. `UnitOfWorkProducer` 
calls the producer through a `CamelInternalProcessor` with the unit of work 
advice. The advice creates the unit of work, which adds the exchange to the 
inflight repository, and the unit of work is done (which removes it) in the 
after advice, run from the callback. When the producer throws instead of 
calling the callback (an `AsyncProducer` that breaks the async contract), 
`CamelInternalProcessor` throws the exception on without its after advice. 
`EventNotifierProducer` then sets the exception and calls the callback of the 
caller, so the caller sees a failed exchange, but the unit of work is never 
done: the exchange stays inflight, the synchronizations of the unit of work do 
not run, and no completed or f
 ailed event is fired. It is the same gap that CAMEL-26953 (#30001) closed for 
the sync send of the producer cache (`DefaultProducerCache.SyncSend`).
   
   Change, in `UnitOfWorkProducer` only: the producer is called through a small 
`ProducerSend` (a `DelegateAsyncProcessor`, so the producer is still built, 
started and stopped with the processor) that gives the producer a callback per 
send (`SendCallback`, an `AtomicBoolean`). A throw before the callback sets the 
exception on the exchange and calls the callback, so the after advice runs and 
the unit of work is done once; `process` returns `true`. A throw after the 
callback is thrown on as it is, as before, so the caller that knows its 
callback was called handles it (that is `EventNotifierProducer`, which 
CAMEL-27039 changes). A further call of the callback by the producer (more than 
once, or after it threw) is logged at WARN and not passed on, since it would do 
the unit of work a second time; this is what `SyncSend` does. As for a 
processor (CAMEL-26741), an `Error` that is not a `VirtualMachineError` is 
handled as an exception (the exchange fails with it), and a `VirtualMachineError
 ` is thrown on as before. A checked exception thrown sneakily is not caught, 
as in `SyncSend`.
   
   The change does not touch `EventNotifierProducer`, so it is independent of 
CAMEL-27039: git merges the two branches in either order, and with both, the 
tests of both pass. Without CAMEL-27039, a producer that calls the callback and 
then throws still completes the exchange twice in `EventNotifierProducer` (that 
is CAMEL-27039), but the unit of work is done once.
   
   Cost: one small object and one compare-and-set per send of an injected 
producer.
   
   Tests, new `UnitOfWorkProducerProducerThrowsTest` (an `AsyncProducer` that 
throws before or after it calls the callback, an event notifier that counts the 
completed and failed events, one per unit of work done):
   - `testInjectedProducerThrowBeforeCallbackLeavesNothingInflight`: the 
producer that `@EndpointInject` injects (`DeferServiceFactory`), 
`process(exchange)`: the exchange fails with the exception, nothing is left in 
the inflight repository, one unit of work done;
   - `testThrowBeforeCallbackCompletesOnce`: `UnitOfWorkProducer` directly: the 
callback is called once, `process` returns `true`, the exchange has the 
exception and no unit of work, nothing inflight;
   - `testThrowBeforeCallbackThenCallbackCompletesOnce`: the producer throws 
and calls the callback afterwards: the callback is called once and the unit of 
work is done once;
   - `testErrorBeforeCallbackFailsTheExchange`: the same with an 
`AssertionError`;
   - `testVirtualMachineErrorIsThrownOn`: a `StackOverflowError` is thrown on, 
as before;
   - `testCallbackThenThrowIsThrownOn`: the exception thrown after the callback 
reaches the caller, the callback is called once, the exchange keeps its body, 
the unit of work is done once;
   - `testCallbackOnlyCompletesOnce`: a control, a producer that keeps the 
contract.
   
   Without the change four of the seven tests fail on main (the injected 
producer leaves the exchange inflight, size 1; `UnitOfWorkProducer` throws the 
exception on without calling the callback, also for the `AssertionError`); the 
three controls pass. With it all seven pass, and the camel-core tests pass 
(9439 run, 0 errors, 45 skipped; the one failure in that run was the 
`AssertionError` test of this PR asserting the `Error` itself, where the 
exchange keeps it wrapped in a `CamelExecutionException` as for a processor; 
with the assertion fixed the seven tests pass). With CAMEL-27039 merged locally 
as well, the tests of both PRs and the producer, inject, produce and send tests 
of camel-core pass (565 run, 0 failures, 6 skipped).
   
   Upgrade guide: none. The visible change is that such an exchange is no 
longer left inflight; the exception the caller sees is the same.
   
   Backport: camel-4.18.x has the same `UnitOfWorkProducer`, so the change 
ports as it is.
   
   # 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-27047](https://issues.apache.org/jira/browse/CAMEL-27047).
   
   # 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]

Reply via email to