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

   # Description
   
   [CAMEL-27321](https://issues.apache.org/jira/browse/CAMEL-27321)
   
   CAMEL-24124 made the reply manager complete the InOut exchanges that wait 
for a reply when it stops: `CorrelationTimeoutMap.doStop` drains the map and 
hands each handler to its timeout task, which completes the exchange with a 
`RejectedExecutionException`. A few windows were left in which an exchange is 
never completed, so a synchronous caller waits for ever and the exchange stays 
in flight:
   
   - **A request registered during or after the stop.** `JmsProducer` checks 
`isRunAllowed()` once and registers the reply later, from the `MessageCreator`, 
while the request is created. Nothing in the map checks whether it has stopped. 
A handler added between the drain and the `map.clear()` of 
`DefaultTimeoutMap.doStop` was cleared without an event; one added after the 
stop stayed in the stopped map (its purge task is cancelled). With 
`useMessageIDAsCorrelationID`, `updateCorrelationId` removes the handler under 
its provisional id and puts it back under the JMSMessageID, and the drain can 
run in between, so the handler is put into the drained map.
   - **Timeout tasks dropped by the forced shutdown.** At stop, each drained 
handler is handed to the onTimeout executor, and its task continues the routing 
of the exchange on that thread. When those continuations take longer than 
`shutdownAwaitTermination`, `shutdownGraceful` calls `shutdownNow()`, which 
drops the tasks still queued.
   - **A rejected timeout task.** With `camel.threadpool.rejectedPolicy=Abort` 
and a full queue, `submit` throws `RejectedExecutionException` after the entry 
was removed from the map, and `DefaultTimeoutMap.emitEvent` swallowed every 
exception of a listener without a log.
   
   The change, the same in camel-jms and camel-sjms:
   
   - `CorrelationTimeoutMap` uses its own lock (passed to `DefaultTimeoutMap`) 
and a `closed` flag set under that lock before the drain. Once closed, 
`putIfAbsent` (a new request, `registerReply`) throws 
`RejectedExecutionException`, so the producer does not send the request and 
completes the exchange with that exception, as it does when the producer is no 
longer running. `put` (the move to the JMSMessageID) completes the handler at 
once with its timeout task, as the drain would have done, as the handler is in 
no map any more and the request has been sent.
   - The timeout task of a handler runs once (a claim flag). A task that the 
executor rejects runs in the calling thread (as the CallerRuns policy does) 
with a WARN. The tasks that the executor did not run are completed by 
`ReplyManagerSupport.doStop` after it shut the executor down, on the stopping 
thread.
   - `DefaultTimeoutMap.emitEvent` logs an exception of a listener at WARN 
instead of ignoring it (the entry has already been removed from the map at that 
point). The other users of `DefaultTimeoutMap` (camel-core): the Aggregate EIP 
and camel-rocketmq catch the exceptions of their listeners themselves, so 
nothing changes for them; the eviction listener of camel-netty's 
`TimeoutCorrelationManagerSupport` submits to its worker pool, and a rejected 
submit (pool shut down, or the Abort policy), which leaves that exchange 
waiting, is now logged instead of being silent.
   
   So every exchange that reaches the reply manager is completed exactly once: 
by its reply, by its timeout, by the drain, or by the producer when the request 
is refused.
   
   Model: the bug was found with a TLA+ model of the request-reply correlation 
(producer threads with the provisional id update and send failures, the reply 
listener with the early-reply wait, the purge thread, the onTimeout executor 
with its queue, the CallerRuns and Abort policies and the forced shutdown, and 
the stop sequence). I added the change to it. On main it finds a handler lost 
in each of the three windows. With the change, no handler is lost, none is 
completed twice, at most one key per handler, the map is empty after the stop 
and every exchange completes, with up to 3 requests, a send failure, a forced 
shutdown and the Abort policy (1,556,457 distinct states; TLC and everystate 
agree on the scopes with 2 requests).
   
   Tests: new `ReplyManagerStopCompletesRequestsTest` in camel-jms and 
camel-sjms. The reply manager is set up as the producer does it, without a 
broker (these paths do not involve the listener container), and the thread 
orders are forced with latches on the map's listener events:
   
   - a request registered after the stop is refused, and one registered while 
the stop drains the map is refused;
   - a handler moved to its JMSMessageID after the drain is completed;
   - with the onTimeout thread busy and a short `shutdownAwaitTermination`, the 
timeout task that the forced shutdown drops is completed;
   - with a pool of one thread, a queue of one and the Abort policy, the third 
timeout task is rejected and runs in the calling thread.
   
   On main all 5 fail in both modules. With the change they pass, and so do the 
unit tests of the modules (no ITs): camel-jms 547 run, 0 failures, 11 skipped; 
camel-sjms 231 run, 0 failures, 5 skipped; and `DefaultTimeoutMapTest` and 
`DefaultTimeoutMapListenerThrowsErrorTest` in camel-core (8). The new class 20 
times in one JVM, in each module: 100 run, 0 failures.
   
   One PR for the three, as they share the root cause: an exchange whose 
handler leaves the correlation map without its timeout task being run. Claus 
fixed the main case of it in CAMEL-24124 in the same classes of both modules.
   
   No upgrade guide entry: a request that reaches a stopped reply manager now 
fails at once with a `RejectedExecutionException` instead of hanging.
   
   Not changed:
   
   - camel-rocketmq has the same register-during-stop window in its 
`ReplyTimeoutMap`; it runs the timeout tasks on the purge thread, so the 
executor parts do not apply. I would fix it in a follow-up.
   - The exchanges completed at the end of the stop continue their routing on 
the stopping thread, as a CallerRuns task would.
   
   # 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-27321](https://issues.apache.org/jira/browse/CAMEL-27321).
   
   # 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 and tested the affected modules, including the formatter and 
import-sort plugins. 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