fix: in event consumption, always release the lock if we already aquired it#2169
Merged
triceo merged 2 commits intoTimefoldAI:1.xfrom Mar 6, 2026
Merged
fix: in event consumption, always release the lock if we already aquired it#2169triceo merged 2 commits intoTimefoldAI:1.xfrom
triceo merged 2 commits intoTimefoldAI:1.xfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a concurrency bug in the Timefold Solver's event consumption mechanism where semaphore locks (used to order solver events) were not always properly released. The fix moves lock release logic into whenComplete/whenCompleteAsync callbacks on CompletableFuture, ensuring locks are released even when exceptions occur. Additionally, mutable fields accessed across threads are converted to AtomicReference for proper thread safety, and a timeout is added to terminateEarly() to prevent indefinite blocking.
Changes:
- Refactored
ConsumerSupportto release semaphores inwhenCompletecallbacks instead offinallyblocks inside async tasks, ensuring lock release even on exceptional paths. - Converted
DefaultSolverJobfields (solverStatus,finalBestSolutionFuture,consumerSupport) fromvolatile/plain fields toAtomicReferencefor proper thread-safe access. - Added a 1-minute timeout to
terminateEarly()with escalation logic to prevent indefinite blocking.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
core/src/main/java/ai/timefold/solver/core/impl/solver/DefaultSolverJob.java |
Converted fields to AtomicReference, extracted terminateEarlyLocked() with timeout and idempotency, added null-safety checks |
core/src/main/java/ai/timefold/solver/core/impl/solver/ConsumerSupport.java |
Moved semaphore releases to whenComplete callbacks, added @NullMarked, added class-level Javadoc, refactored to return CompletableFuture for chaining |
core/src/main/java/ai/timefold/solver/core/impl/solver/BestSolutionHolder.java |
Removed redundant @NonNull annotation (class is already @NullMarked) |
core/src/main/java/ai/timefold/solver/core/api/solver/SolverJob.java |
Updated Javadoc for terminateEarly() to document the new timeout behavior |
core/src/main/java/ai/timefold/solver/core/impl/solver/DefaultSolverJob.java
Outdated
Show resolved
Hide resolved
Christopher-Chianelli
approved these changes
Mar 6, 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.
Cherry-pick of 5065095