allthingssecurity opened a new pull request, #30810: URL: https://github.com/apache/camel/pull/30810
# Description [CAMEL-27445](https://issues.apache.org/jira/browse/CAMEL-27445) A call of a Circuit Breaker with `timeoutEnabled` runs on a thread pool. When its timeout fires while the call still waits in the queue of that pool, the exchange goes on with the `TimeoutException` (or the fallback). What happened to the queued call depended on the mode: - synchronous mode without a bulkhead: `TimeLimiter.decorateFutureSupplier` cancels the future of the call (`timeoutCancelRunningFuture`, default `true`), and `CompletableFuture` skips the call when a thread takes it; - asynchronous mode: the timeout completes the future that `processAsync` handed to the time limiter, and the future returned by `executorService.submit(...)` was discarded, so nothing cancelled the call: it ran once a thread was free. `timeoutCancelRunningFuture` had no effect (`TimeLimiter.decorateCompletionStage` does not read it); - synchronous mode with a bulkhead: the bulkhead wraps the future of the call, so that the permit is held until the call is done ([CAMEL-25129](https://issues.apache.org/jira/browse/CAMEL-25129)). The future that the time limiter cancels is then the one of the bulkhead, not the one of the call: the call ran once a thread was free, and kept its permit until then. So the backend got a call for an exchange that had already failed with a timeout or run its fallback, possibly after the circuit breaker had opened (with `inheritErrorHandler`: the first attempt times out, its redelivery times out while queued and opens the breaker, and then a thread starts the call of the redelivery). The change: a small `CallStart` per call. The task of the call and the caller that cancels it both claim the call (a compare-and-set), and only one of them gets it: - asynchronous mode: the submitted task starts the call only if it claims it. When `timeoutCancelRunningFuture` is true, the time limiter's stage is wrapped so that a completion with `TimeoutException` cancels the call; - synchronous mode with a bulkhead: the call is wrapped the same way, and when the time limiter cancels the future of the bulkhead, the cancel is passed on to the call. The time limiter cancels only when `timeoutCancelRunningFuture` is true, so the option keeps its meaning; - a cancelled call completes its future at once, which releases its bulkhead permit (before, the permit stayed taken until a thread ran the call); - a call that has started keeps running and is not interrupted, as in synchronous mode without a bulkhead (`CompletableFuture.cancel` does not interrupt), and keeps its bulkhead permit until it is done (CAMEL-25129); - synchronous mode without a bulkhead is unchanged (the time limiter already cancels the future of the call). A call that is cancelled this way is logged at DEBUG with the exchange id. Found with a TLA+ model of the circuit breaker, the time limiter, the thread pools, the bulkhead and the route redelivery: at its smallest bound (2 route threads, 1 worker, queue 1, 1 redelivery) it finds a call that starts after its caller timed out, in asynchronous mode (with and without a bulkhead, 114 distinct states) and in synchronous mode with a bulkhead (148), and not in synchronous mode without a bulkhead. Tests: new `ResilienceTimedOutQueuedCallTest` (package-private, latches, no sleeps). Each route runs its calls on a pool of one thread, started before X1 (so X1 starts well within its timeout): X1 blocks the thread and times out, X2 waits in the queue and times out; then X1 is released and the test waits for a task submitted to the same pool after them, so the thread has taken X2 by then. - asynchronous, asynchronous with a bulkhead, synchronous with a bulkhead: the backend gets only X1, and with a bulkhead X2's permit is released when X2 times out (4 of 5 free while X1 runs, 5 at the end); - synchronous without a bulkhead: the control, unchanged (only X1); - asynchronous with `timeoutCancelRunningFuture=false`: X2 still runs, as before. On `main` the three tests of the changed modes fail (the backend gets X2 as well); the control and the `timeoutCancelRunningFuture=false` test pass on both. With the change all pass, and the camel-resilience4j unit tests pass (107 run, 0 failures, 0 skipped; run offline). The new class with `ResilienceBulkheadTimeoutTest`, 20 times in one JVM: 140 run, 0 failures. Not changed: - A call that has already started when its timeout fires keeps running, in every mode, as before. - The cancelled call stays in the queue of the thread pool until a thread takes it and skips it; it no longer holds a bulkhead permit. (The fix for the synchronous mode running a call on the route thread also removes a cancelled call from the queue of a `ThreadPoolExecutor`; the asynchronous mode could do the same with the future of `submit`, left for a follow-up.) The change for asynchronous mode builds on CAMEL-27212 (only on `main`, 4.24); the synchronous mode with a bulkhead has the same problem on the older lines since CAMEL-25129. Related changes in the same area, each its own PR and merging cleanly with this one: [CAMEL-27356](https://issues.apache.org/jira/browse/CAMEL-27356) (in asynchronous mode the fallback after a timeout runs off the timeout thread), the fix for the asynchronous timeout scheduler that rejected timeouts once 1000 were pending, and the fix for the synchronous mode running a call on the route thread when the thread pool is full. That last one changes how the synchronous mode submits the call; this one only wraps the task it submits and the stage around it, so the two compose (the new tests of all of them pass together). # 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-27445](https://issues.apache.org/jira/browse/CAMEL-27445). # 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]
