allthingssecurity opened a new pull request, #30759: URL: https://github.com/apache/camel/pull/30759
# Description [CAMEL-27416](https://issues.apache.org/jira/browse/CAMEL-27416) `DefaultAsyncProcessorAwaitManager` keeps the threads that wait for an asynchronous callback, so that JMX (`interrupt(exchangeId)`), the blocked console and the stop of the CamelContext (`interruptThreadsWhileStopping`) can see and release them. The lazy latch of CAMEL-26473 is not changed and is correct in the model; the problems are around it: 1. `interrupt(exchange)` read the entry, built the WARN line (the blocked thread, its route and the message history), and only then set `RejectedExecutionException`, marked the exchange interrupted and counted the latch down. When the callback fired in between, the thread had already returned the exchange to its caller as done, and the interrupt then failed it and marked it interrupted while the caller went on with it. A consumer could report a completed exchange as failed, and the later steps see `isInterrupted()`, so `RedeliveryErrorHandler` stops the route there. The stop of the CamelContext goes through the same method for every entry. 2. The entries were kept in a map keyed by the exchange. Two threads can wait for the same exchange: a thread waits in `process()` while the exchange goes on on another thread (a threads step, an asynchronous producer), which calls a step that waits for it too, such as `ProducerTemplate.send(uri, exchange)` (`DefaultProducerCache.send` to `SharedCamelInternalProcessor.process` to the await manager). The second thread replaced the entry of the first, and its `finally` removed the entry by exchange when it returned. The first thread was still blocked but no longer listed: `size()`, `browse()`, JMX and the blocked console did not show it, `interrupt()` did not find it, and the stop logged "Shutting down with no inflight threads". 3. `doStop` interrupted the entries it saw and then cleared the map, so a thread that started to wait while the stop ran was dropped from the list without being interrupted. Changes, all in `DefaultAsyncProcessorAwaitManager`: - One entry per waiting thread (`ConcurrentHashMap.newKeySet()` of the entries). A thread removes only its own entry. - `AwaitThreadEntry.interrupt()` decides under the lock of the entry: when the thread has not returned and the callback has not fired (the latch count is still 1), it sets the exception, marks the exchange interrupted and counts the latch down; otherwise it does nothing. The waiting thread calls `finish()` under the same lock in its `finally`, before it returns the exchange. So the interrupt either lands before the thread returns (the thread sees the failure, as before) or not at all. The WARN line is still built first (outside the lock), and logged only when the thread was released; the interrupted counter of the statistics counts only those. - `interrupt(exchange)` releases every thread that waits for the exchange. It goes through the entries instead of a map lookup; in Camel it is only called from JMX (`interrupt(exchangeId)`) and the stop (the blocked console only lists the entries). - `doStop` interrupts each entry it sees, and no longer clears the entries. A thread that is not released (with `interruptThreadsWhileStopping=false`, or one that started to wait after the stop listed the entries) stays listed until it returns, so it can still be seen and interrupted through JMX. The waiting path does the same work as before: one entry and one node of the set per wait (the map entry before), plus an uncontended lock of the entry in `finish()`, and only on a thread that is about to block on the latch. No API or option changes, no upgrade guide entry: the visible differences are that a late interrupt no longer changes an exchange that was already done, and that `size()` and `browse()` now list every blocked thread. Not changed: - A thread that starts to wait while the stop runs can still be missed by the iteration of the stop (it is weakly consistent), so it is not interrupted by the stop. With this change it stays listed and can be interrupted, but the stop does not release it. Closing that window would need the manager to refuse or interrupt new waits once it is stopping; I left it out. - When two threads wait for the same exchange, `interrupt(exchange)` releases both. Before, only the thread that started to wait last was listed and released, and the first one went on later, once the exchange came back to it from the released one. Now the first thread also goes on at once, while the route of the exchange may still run on the second thread; that is what an interrupt does for a single waiting thread too (the Javadoc of `interrupt`: the other thread is still assumed to be processing the exchange). - When two threads wait for the same exchange and one of them is interrupted, the exception is set on the shared exchange, also when the other thread already returned it as done. That is inherent to the two threads sharing one exchange. - On the `InterruptedException` path (the waiting thread itself is interrupted) the exchange is not marked interrupted, so the processor that is still running can clear the exception before the caller reads it (model lead, not reproduced). - `await()` drains only the queue of the reactive executor, not the queue that `scheduleMain` moves aside, so a nested drain can block on work parked on its own thread (model lead; the known case is handled in `AggregateProcessor`). Model: found with a TLA+ model of the await manager (the lazy latch, the drain loop, the callback on the same or another thread, duplicate callbacks, `interrupt` from JMX, `doStop`, a restart, a processor that throws, and a thread interrupted while it waits, with 2 waiters on distinct or the same exchange). I added a `Fixed` switch to it. On main it finds an interrupt that changes an exchange its own waiter already returned as done (9,244 distinct states), a blocked waiter that is not listed (8,270) and a waiter that removes the entry of another (20,849), both with two waiters on one exchange, and entries that the stop clears. With the fix all the invariants of the model hold (the waiter returns only after the callback or an interrupt that failed the exchange, an entry exists only while its waiter waits, every blocked waiter is listed, no waiter removes another's entry, an interrupted exchange is never reported as done, no late interrupt of a returned exchange), and so do no lost wake-up and "every call ends" (everystate: 304,881 distinct states with distinct exchanges, 119,637 with one exchange). TLC gives the same counts for the fixed configurations (304,881 and 119,637 distinct states, liveness included) and finds the same three violations on main. Tests: new `DefaultAsyncProcessorAwaitManagerInterruptRaceTest` (3, real manager and `DefaultExchange`, a test processor whose callback the test fires; the window inside `interrupt` is held by a `UnitOfWork` proxy whose `getRoute()` parks the interrupting thread while the WARN line is built; no sleeps): - `interruptLeavesAnExchangeAloneWhoseCallbackFiredWhileItLoggedTheBlockedThread`: on main the returned exchange then has `RejectedExecutionException`; - `twoThreadsWaitingForTheSameExchangeAreBothListedAndInterrupted`: on main `size()` is 1 while both threads are blocked; - `threadThatWaitsWhileTheManagerStopsIsReleasedOrStaysListed`: two exchanges chosen by identity hash so that, on main, the iteration of the stop has passed the bin of the second when it starts to wait; on main it is no longer listed (`size()` 0) and cannot be interrupted. With the fix it is either released by the stop or still listed and released by `interrupt`. On main all 3 fail. With the change, the camel-core tests of the await manager, the async processors, the seda component, the split/aggregate tests that use the await manager and `CamelContextRestartInternalServicesTest` pass: 287 run, 0 failures. The await manager tests (`DefaultAsyncProcessorAwaitManagerInterruptRaceTest`, `DefaultAsyncProcessorAwaitManagerTest`, `DefaultAsyncProcessorAwaitManagerProcessTest`, `AsyncProcessorAwaitManagerInterruptTest`, `AsyncProcessorAwaitManagerInterruptWithRedeliveryTest`, `AsyncProcessorAwaitManagerTest`) 20 times in one JVM: 340 run, 0 failures. # 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-27416](https://issues.apache.org/jira/browse/CAMEL-27416). # 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 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]
