Kotlin: acquirePermissionSuspend() should await Bulkhead#acquirePermissionAsync() instead of blocking a Dispatchers.IO thread

Author: o-shevchenkoCreated Sep 23, 2026Updated Sep 23, 2026

Resilience4j version: master after #2506 (2.4.1-SNAPSHOT)

Java version: 21

Follow-up to #1592 / #2506.

Bulkhead.executeSuspendFunction and Flow.bulkhead acquire the permission through acquirePermissionSuspend(), which for maxWaitDuration > 0 runs the blocking acquirePermission() inside withContext(Dispatchers.IO). That keeps the caller's dispatcher free but still occupies one IO thread per waiting coroutine for up to the wait duration, and coroutine cancellation cannot interrupt the wait: when the coroutine is cancelled while waiting, withContext throws CancellationException on return even if acquirePermission() succeeded in the meantime, and that permission is never released.

Proposal: acquirePermissionAsync().await() — CompletionStage.await() is part of kotlinx-coroutines-core since 1.7 (the project is on 1.11.0). Waiting then costs no thread, cancelling the coroutine cancels the queued request (await() cancels the future), and a permission granted concurrently with the cancellation is handed back instead of leaking. The fast path (permission available) stays synchronous because the returned future is already completed, and maxWaitDuration = 0 keeps failing fast with BulkheadFullException.

I am preparing a PR and will reference this issue.

Source: resilience4j/resilience4j