CircuitBreaker: stale automatic state transitions after reset() and concurrent transitions, and HALF_OPEN timer self-interrupt
Resilience4j version: master (7e3ab525), also present in 2.4.0
Java version: 21
CircuitBreakerStateMachine can leave a scheduled automatic transition (OpenState → HALF_OPEN, HalfOpenState → OPEN/CLOSED) alive after the state that scheduled it has been replaced. The breaker then changes state later without any calls being recorded. There is also a related self-interrupt in HalfOpenState.
1. reset() does not cancel the previous state's automatic transition
stateTransition() calls currentState.preTransitionHook(), but reset() swaps in a new ClosedState without it. With maxWaitDurationInHalfOpenState set, the old HalfOpenState timer still fires, its isHalfOpen flag is still true, and it calls transitionToOpenState(). CLOSED → OPEN is a valid transition, so a freshly reset breaker opens with no failures.
CircuitBreaker cb = CircuitBreaker.of("test", CircuitBreakerConfig.custom()
.maxWaitDurationInHalfOpenState(Duration.ofSeconds(1))
.build());
cb.transitionToOpenState();
cb.transitionToHalfOpenState();
cb.reset();
Thread.sleep(2000);
assertThat(cb.getState()).isEqualTo(CLOSED); // fails: OPEN2. HalfOpenState automatic transition interrupts its own thread
HalfOpenState.cancelAutomaticTransition() uses transitionToFuture.cancel(true). When the max-wait timer fires, it calls transitionToOpenState() itself, the pre-transition hook calls cancel(true) on the task that is currently running (isDone() is still false), and the scheduler thread interrupts itself. State-transition event consumers then run with the interrupt flag set, so any blocking work in them (I/O, queues, Thread.sleep) throws InterruptedException. OpenState already uses cancel(false).
CircuitBreaker cb = CircuitBreaker.of("test", CircuitBreakerConfig.custom()
.maxWaitDurationInHalfOpenState(Duration.ofMillis(200))
.build());
AtomicBoolean interrupted = new AtomicBoolean();
CountDownLatch latch = new CountDownLatch(1);
cb.getEventPublisher().onStateTransition(e -> {
if (e.getStateTransition() == StateTransition.HALF_OPEN_TO_OPEN) {
interrupted.set(Thread.currentThread().isInterrupted());
latch.countDown();
}
});
cb.transitionToOpenState();
cb.transitionToHalfOpenState();
latch.await(3, TimeUnit.SECONDS);
assertThat(interrupted).isFalse(); // fails: true3. Concurrent transitions leave orphaned automatic transitions
stateTransition() builds the new state inside AtomicReference.getAndUpdate(...). That function is re-run when threads race, and the OpenState/HalfOpenState constructors schedule timers, so each lost attempt leaves a state that is never installed but whose timer is still scheduled. That timer later moves the real breaker (e.g. FORCED_OPEN → HALF_OPEN).
CircuitBreaker cb = CircuitBreaker.of("test", CircuitBreakerConfig.custom()
.automaticTransitionFromOpenToHalfOpenEnabled(true)
.waitDurationInOpenState(Duration.ofMillis(100))
.build());
// 8 threads call cb.transitionToOpenState() at the same time (CyclicBarrier)
cb.transitionToForcedOpenState();
Thread.sleep(500);
assertThat(cb.getState()).isEqualTo(FORCED_OPEN); // fails for ~70% of breakers: HALF_OPENProposed fix
- Replace the state under the existing
lockso the new-state generator runs exactly once, and havereset()use the same path so the previous state'spreTransitionHook()is always called. - Use
cancel(false)inHalfOpenState, asOpenStatedoes.
I have a PR ready with regression tests for all three cases.
Source: resilience4j/resilience4j