gh-158336: Fix race in lock_tests test_set_and_clear - #158337
Open
matthiasgoergens wants to merge 1 commit into
Open
matthiasgoergens wants to merge 1 commit into
matthiasgoergens wants to merge 1 commit into
Conversation
test_set_and_clear started N threads calling event.wait() and then slept 50 ms (wait_threads_blocked) before calling event.set() and event.clear(). If a thread only reached wait() after clear(), it saw the flag unset and blocked for LONG_TIMEOUT, and Bunch.__exit__ gave up after SHORT_TIMEOUT. This happened on CI in the TSan free-threading job. Wait until all N threads are in the Condition's waiter list instead. Condition.wait() appends the waiter while holding the lock, and notify_all() releases every waiter in the list, so each of those threads is guaranteed to be woken by set(). On a TSan free-threading debug build, pinned to 2 CPUs shared with 4 busy-looping processes, the test failed in 9 of 40 runs before this change and 0 of 40 after.
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.
Instead of sleeping for 50 ms, wait until all five threads are registered as waiters on the event's condition before calling
set()andclear().Condition.wait()appends the waiter to_waiterswhile holding the lock, andnotify_all()wakes every waiter in that list, soset()then wakes all of them.With the reproducer from the issue, the test failed in 0 of 40 runs, against 9 of 40 before. The test still catches gh-57711 about as often as before: with
Event.wait()changed back to return the flag after waking, it failed in 8 of 20 runs, against 7 of 20 without this change.The other callers of
wait_threads_blocked()are not affected, because a thread that arrives late still finds the lock released, the semaphore available or the event set.This PR was prepared with the help of an AI assistant (Claude Code) and reviewed by me.