fix: semaphore release submitted to a dead executor#2374
Merged
Conversation
ConsumerSupport uses 3 semaphores to gate consumeFinalBestSolution(). Each is acquired before a task runs, and released after the task completes via whenCompleteAsync(release, consumerExecutor). whenCompleteAsync(action, executor) does not run action inline. It submits action as a new task to executor when the prior future completes. Two-step: 1. Prior task finishes → future completes 2. Java calls executor.execute(releaseTask) Between steps 1 and 2, executor can be shut down. shutdownNow() rejects new submissions and cancels queued ones. RejectedExecutionException → release task never runs → semaphore stays at 0. Who shuts the executor down? ConsumerSupport.close() — called from DefaultSolverJob.close() when a solver job is cleaned up. Its finally block calls shutdownConsumerExecutor() unconditionally, even if acquireAll() threw InterruptedException. If the close-thread is interrupted mid-acquire(), the executor dies with semaphores still at 0 and their release tasks still queued. Race that produces the hang: 1. Consumer thread finishes start-job task → future F completes 2. Concurrently: close-thread interrupted in acquireAll() → finally → shutdownNow() 3. whenCompleteAsync tries executor.execute(startSolverJobConsumption.release()) → rejected 4. Solver thread later calls consumeFinalBestSolution() → acquireAll() → startSolverJobConsumption.acquire() → blocks forever Why whenComplete fixes it: whenComplete(action) (no executor) runs action synchronously on the thread that completed the future — here, the consumer executor's own thread, immediately after the task finishes, before returning to the executor's work loop. No second submission, no queue, nothing to reject. The release is atomic with the task's completion from the executor's perspective. Executor shutdown can only happen after acquireAll() succeeds in consumeFinalBestSolution, which is after all releases have run. The release is now unreachable by any shutdown path.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR prevents solver event-consumption semaphores from getting stuck when the consumer executor is shut down by ensuring semaphore releases run inline on the completing thread (instead of being resubmitted to an executor that may already be terminated).
Changes:
- Replace
whenCompleteAsync(..., consumerExecutor)withwhenComplete(...)for intermediate best-solution consumption to avoid rejected-release tasks. - Apply the same
whenComplete(...)change for first-initialized solution consumption semaphore release. - Apply the same
whenComplete(...)change for start-solver-job consumption semaphore release.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
Contributor
|
Were you able to reproduce the issue before the fix? |
Collaborator
Author
The issue was failing our CI builds couple times a day. |
zepfred
approved these changes
Jun 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



I ran the original test 100_000 times, it failed zero times.
Claude's summary follows:
ConsumerSupport uses 3 semaphores to gate consumeFinalBestSolution(). Each is acquired before a task runs, and released after the task completes via
whenCompleteAsync(release, consumerExecutor).
whenCompleteAsync(action, executor) does not run action inline. It submits action as a new task to executor when the prior future completes. Two-step:
Between steps 1 and 2, executor can be shut down. shutdownNow() rejects new submissions and cancels queued ones. RejectedExecutionException → release task
never runs → semaphore stays at 0.
Who shuts the executor down?
ConsumerSupport.close() — called from DefaultSolverJob.close() when a solver job is cleaned up. Its finally block calls shutdownConsumerExecutor()
unconditionally, even if acquireAll() threw InterruptedException. If the close-thread is interrupted mid-acquire(), the executor dies with semaphores still
at 0 and their release tasks still queued.
Race that produces the hang:
Why whenComplete fixes it:
whenComplete(action) (no executor) runs action synchronously on the thread that completed the future — here, the consumer executor's own thread, immediately
after the task finishes, before returning to the executor's work loop. No second submission, no queue, nothing to reject. The release is atomic with the
task's completion from the executor's perspective.
Executor shutdown can only happen after acquireAll() succeeds in consumeFinalBestSolution, which is after all releases have run. The release is now
unreachable by any shutdown path.