-
Notifications
You must be signed in to change notification settings - Fork 128
Use trylock to eliminate the remaining race condition in Test.cancel().
#1415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
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
…el()`. This PR fixes the race condition in `Test.cancel()` that could occur if an unstructured task, created from within a test's task, called `Test.cancel()` at just the right moment. The order of events for the race is: - Unstructured task is created and inherits task-locals including the reference to the test's unsafe current task; - Test's task starts tearing down; - Unstructured task calls `takeUnsafeCurrentTask()` and gets a reference to the unsafe current task; - Test's task finishes tearing down; - Unstructured task calls `UnsafeCurrentTask.cancel()`. The fix is to use `trylock` semantics when cancelling the unsafe current task. If the test's task is still alive, the task is cancelled while the lock is held, which will block the test's task from being torn down as it has a lock-guarded call to clear the unsafe current task reference. If the test's task is no longer alive, the reference is already `nil` by the time the unstructured task acquires the lock and it bails early. If we recursively call `cancel()` (which can happen via the concurrency-level cancellation handler), the `trylock` means we won't acquire the lock a second time, so we won't end up deadlocking or aborting (which is what prevents calling `cancel()` while holding the lock in the current implementation). I hope some part of that made sense.
Contributor
Author
|
Sigh. The Linux implementation treats |
Contributor
Author
…GNU_SOURCE" This reverts commit 9d6c9fb.
… jgrynspan/test-cancel-race
briancroom
approved these changes
Nov 11, 2025
ktoso
approved these changes
Nov 12, 2025
ktoso
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks reasonable enough, thanks for looking into the safety of this approach.
stmontgomery
approved these changes
Nov 12, 2025
Contributor
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
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.
This PR fixes the race condition in
Test.cancel()that could occur if an unstructured task, created from within a test's task, calledTest.cancel()at just the right moment. The order of events for the race is:takeUnsafeCurrentTask()and gets a reference to the unsafe current task;UnsafeCurrentTask.cancel().The fix is to use
trylocksemantics when cancelling the unsafe current task. If the test's task is still alive, the task is cancelled while the lock is held, which will block the test's task from being torn down as it has a lock-guarded call to clear the unsafe current task reference. If the test's task is no longer alive, the reference is alreadynilby the time the unstructured task acquires the lock and it bails early. If we recursively callcancel()(which can happen via the concurrency-level cancellation handler), thetrylockmeans we won't acquire the lock a second time, so we won't end up deadlocking or aborting (which is what prevents callingcancel()while holding the lock in the current implementation).It is possible for
cancel()to trigger user code, especially if the user has set up a cancellation handler, but there is no code path that can then lead to a deadlock because the only user-accessible calls that might touch this lock usetrylock.I hope some part of that made sense.
Checklist: