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

   # Description
   
   [CAMEL-26953](https://issues.apache.org/jira/browse/CAMEL-26953)
   
   Follow-up of #29786 (CAMEL-26882), as asked in its review. The same rule as 
CAMEL-26445, CAMEL-26461, CAMEL-26464 and CAMEL-26882: the callback of an 
exchange is called exactly once, also when an `AsyncProcessor` breaks the async 
contract (calls the callback and then throws).
   
   One part is deliberately left as it is: the fast path of `SendProcessor`. 
When the send needs no callback of its own (no event notification, the same 
exchange pattern, no `variableReceive`, which is the common `to`), 
`SendProcessor` passes the callback of its caller to the producer as is, 
without any object per send. There it cannot tell whether the producer called 
that callback before it threw without adding an object to every send, so it 
still sets the exception and calls the callback, as on main. The step around it 
(the channel and the error handlers, since CAMEL-26445, CAMEL-26461 and 
CAMEL-26464) ignores the second call; the exception still ends up on the 
exchange. I kept the fast path free of allocations, as the JIRA asks; if 
reviewers prefer one small object per `to`, it is a two-line change.
   
   Change:
   - `SendProcessor.sendUsingProducer`, when the send has its own callback: 
that callback is now a small class (`SentCallback`, an `AtomicBoolean`, one 
object per send as the lambda it replaces) that completes the send once 
(variable result, exchange pattern, sent event, callback of the caller). A 
throw after the callback is logged at WARN, not set on the exchange, and the 
send is not completed again; `sendUsingProducer` returns the `doneSync` the 
producer passed. A second call of the callback by the producer is logged and 
passed on to the callback of the caller, as on main (without running the 
variable, pattern and event part again), so the step around it, which ignores 
it, and the exchange sanitizer still see it (`SanitizerUserCodeTest` checks 
that it reports such a producer behind `to`). A throw before the callback now 
completes the send through that callback, as `doInAsyncProducer` does since 
CAMEL-26882: the exchange pattern is restored and the sent event fired, which 
main skipp
 ed.
   - `DefaultProducerCache.send` (the sync send, used by the sync sends of the 
producer template): the producer is called through a small `SyncSend` (one 
object per sync send) that calls the callback once and handles the throw the 
same way, so the await manager and the advice of the shared internal processor 
see a producer that keeps the contract. A throw before the callback now runs 
the after advice and the result processor, as when the producer sets the 
exception.
   - `SendHelper.HookedSend` (a send with a send hook, today only camel-chaos): 
a state field decides once who completes the send. A throw after the callback 
is thrown on to the caller as it is: the exchange is left alone and `afterSend` 
is not called a second time. The callers above, and `doInAsyncProducer`, then 
log it. A second call of the callback is passed on without the hooks, as 
without a hook. Also for the second send of `SEND_TWICE`.
   - `DefaultProducerCache.asyncDispatchExchange` no longer fires its own 
sending and sent events: `doInAsyncProducer`, its only caller in Camel, already 
fires them, so the async sends of the producer template fired each event twice. 
`EventNotifierCallback` (package-private) had no other use and is removed; the 
list of send places in `design/chaos.adoc` is updated. A subclass that calls 
this protected method directly now gets no events from it.
   
   An `Error` is left as before on every path: `SendProcessor` still catches 
`Exception`, and `SyncSend` and `HookedSend` catch `RuntimeException`, so a 
`VirtualMachineError` (or another `Error`) is thrown on as on main 
(CAMEL-26741).
   
   Cost: on the fast path of `to` nothing changes. On the path with its own 
callback the lambda becomes one object of about the same size, plus one 
compare-and-set per send. The sync send of the producer cache gets one more 
small object per send. `HookedSend` gets one int field.
   
   Tests:
   - new `SendProcessorProducerThrowsTest` (a `to` with an exchange pattern, so 
the send has its own callback; an `AsyncProducer` that calls the callback and 
then throws, or throws first):
     - `testCallbackThenThrowRoutesOnce`: the next step gets the message once 
and the exchange has no exception;
     - `testThrowBeforeCallbackFailsAndRestoresPattern`: the exchange fails 
with the exception and has its pattern back;
     - `testCallbackThenThrowWithSendHookRoutesOnce`: the same with a send 
hook, which runs `afterSend` once.
   - `DefaultProducerCacheProducerThrowsTest`, new methods:
     - `testSyncSendCallbackThenThrowKeepsTheResult`: the sync send keeps the 
result of the producer;
     - `testSyncSendThrowBeforeCallbackFails`: a control, the sync send fails 
and the producer is back in the pool;
     - `testAsyncSendEmitsOneSendingAndOneSentEvent`: one sending and one sent 
event per async send;
     - `testToDCallbackThenThrowWithSendHookRoutesOnce`: `toD` with a send 
hook, the exception is not set and `afterSend` runs once.
   - `SendHookTest.testProducerTemplateAsync` pinned the doubled pair of the 
async template send ("as it did before the hook"); it now expects one sending 
and one sent event around the hook.
   
   Without the change the six new tests other than the control fail on main 
(the next step not run because the exception was set on the exchange, the 
exchange pattern not restored, the sync send failing a send that succeeded, two 
sending and two sent events, the hook setting the exception). With it they 
pass, and the camel-core tests pass (9326 run, 0 failures, 45 skipped; 11 
errors in the `DumpModel*` tests, a `NoClassDefFoundError` of `OgnlExpression` 
from a stale camel-yaml-io jar in my local repository since camel-ognl was 
removed, the same as on other branches), as do the tests of the core modules it 
depends on (1423 run, 0 failures).
   
   Upgrade guide (4.24), own section: an exception thrown by a producer after 
it called the callback is now logged instead of set on the exchange in these 
places, a `to` whose producer throws first restores the exchange pattern and 
fires the sent event, and the async sends of the producer template fire one 
sending and one sent event.
   
   Backport: builds on CAMEL-26882 and the send hook of CAMEL-26331, both only 
on main.
   
   # 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-26953](https://issues.apache.org/jira/browse/CAMEL-26953).
   
   # 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 named above. 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 commit 
carries 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