shashank created CAMEL-27445:
--------------------------------

             Summary: camel-resilience4j - a call that times out while it waits 
for a thread still runs later, in asynchronous mode and in synchronous mode 
with a bulkhead
                 Key: CAMEL-27445
                 URL: https://issues.apache.org/jira/browse/CAMEL-27445
             Project: Camel
          Issue Type: Bug
          Components: camel-core
            Reporter: shashank
            Assignee: shashank


h3. What happens

A call of a Circuit Breaker with {{timeoutEnabled}} runs on a thread pool. When 
the timeout fires while the call is still waiting in the queue of that pool, 
the exchange goes on with the {{TimeoutException}} (or the fallback). What 
happens to the queued call depends on the mode:

* Synchronous mode without a bulkhead: the time limiter cancels the future of 
the call ({{timeoutCancelRunningFuture}}, default {{true}}), and the call is 
skipped when a thread takes it.
* Asynchronous mode: the timeout completes the future that the processor handed 
to the time limiter. The future returned by {{executorService.submit(...)}} is 
discarded, so nothing cancels the call: it runs once a thread is free. 
{{timeoutCancelRunningFuture}} has no effect.
* Synchronous mode with a bulkhead: the bulkhead wraps the future of the call 
(the permit is held until the call is done, CAMEL-25129), so the future that 
the time limiter cancels is the one of the bulkhead, not the one of the call. 
The call runs once a thread is free, and its bulkhead permit stays taken until 
then.

So the backend gets a call for an exchange that already failed with a timeout 
or already ran its fallback, possibly after the circuit breaker opened.

h3. How to reproduce

A Circuit Breaker with {{timeoutEnabled(true).timeoutDuration(200)}} whose 
calls run on a pool of one thread: X1 blocks the thread and times out; X2 waits 
in the queue and times out. When X1 ends, the thread runs the call of X2 in 
asynchronous mode and in synchronous mode with a bulkhead (the backend gets 2 
calls); in synchronous mode without a bulkhead it skips it (1 call).

Found with a TLA+ model of the circuit breaker, time limiter, thread pools, 
bulkhead and route redelivery.

h3. Fix

When {{timeoutCancelRunningFuture}} is true, a call that has not started when 
its timeout fires is cancelled in every mode: the task of the call and the 
timeout claim the call, and only one of them wins. In asynchronous mode the 
processor cancels it after the time limiter completes with a 
{{TimeoutException}}; with a bulkhead in synchronous mode the cancel of the 
bulkhead future is passed on. A cancelled call releases its bulkhead permit at 
once. A call that has started keeps running and is not interrupted, as in 
synchronous mode today, and keeps its permit until it is done. With 
{{timeoutCancelRunningFuture=false}} the call still runs later, as before.

_Filed with Claude Code on behalf of allthingssecurity._



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to