shashank created CAMEL-27443:
--------------------------------

             Summary: camel-resilience4j - Circuit Breaker EIP: in synchronous 
mode with a timeout, a full thread pool runs the call on the route thread, 
where the timeout never fires
                 Key: CAMEL-27443
                 URL: https://issues.apache.org/jira/browse/CAMEL-27443
             Project: Camel
          Issue Type: Bug
          Components: camel-core
            Reporter: shashank
            Assignee: shashank


h3. What happens

A Circuit Breaker with {{timeoutEnabled=true}} in synchronous mode (the 
default) runs each call with {{CompletableFuture.supplyAsync(task, 
executorService)}} and then waits for it with the time limiter 
({{future.get(timeoutDuration)}}). When no {{timeoutExecutorService}} is set, 
the thread pool comes from the default thread pool profile: 10 threads, up to 
20, a queue of 1000 calls, and the {{CallerRuns}} rejected policy.

When all the threads are busy and the queue is full, {{CallerRuns}} runs the 
call on the route thread, inside {{supplyAsync}}, before the time limiter has a 
future to wait on. Then:

* no timeout can fire, however long the call takes;
* a backend that does not answer (no socket timeout) blocks the route thread 
for good;
* when the call returns, the circuit breaker records a *success* (only a slow 
call once {{slowCallDurationThreshold}}, 60 s by default, is passed).

The thread pool fills up on its own during a slowdown of the backend: a call 
that timed out keeps its thread (cancelling a {{CompletableFuture}} does not 
interrupt it), and a call that timed out while still queued is cancelled but 
stays in the queue until a thread takes it and skips it. With a hung backend no 
thread takes it, so the queue fills with calls that already timed out, and the 
next calls (half-open probes included) run on the route threads. During a 
slowdown the breaker then stays CLOSED, because these slow calls count as 
successes.

h3. How to reproduce

Default thread pool profile scaled down to 1 thread and a queue of 1, 
{{timeoutDuration(200)}}:
# call 1 blocks in the backend; it times out after 200 ms and keeps the only 
thread;
# call 2 is queued, times out, and its cancelled task keeps the queue slot;
# call 3 finds the queue full and runs on the caller thread: 2 seconds later it 
is still in the backend call (10 times the timeout), and when the backend 
answers, the breaker records a successful call and the exchange has no 
exception.

Found with a TLA+ model of the circuit breaker, the time limiter, the thread 
pool, the bulkhead and the route redelivery (2 route threads, one call that 
times out and one redelivery are enough).

h3. Fix

For the thread pool that Camel creates itself:
* it uses the default thread pool profile with the {{Abort}} rejected policy, 
so a call that does not fit is rejected: the exchange fails with a 
{{RejectedExecutionException}} (or the fallback runs), and the circuit breaker 
records a failed call;
* a call that the time limiter cancels while it is queued is removed from the 
queue at once.

With virtual threads ({{camel.threads.virtual.enabled}}) the thread pool is a 
thread per call bounded to the queue size (1000 calls); a call over that still 
waits for a free slot up to the keep alive time (60 s by default), and is then 
rejected instead of running on the route thread.

A thread pool given with {{timeoutExecutorService}} is used as it is (its own 
rejected policy); a thread pool with {{CallerRuns}} given there keeps the old 
behaviour. The Resilience4j EIP page recommends a bulkhead no larger than the 
queue size, or sizing the default profile; upgrade guide entry for 4.24.

_Filed with Claude Code on behalf of allthingssecurity._



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

Reply via email to