Repository navigation
Conversation
take_gil() set the drop request only when a single COND_TIMED_WAIT call timed out. Every GIL release signals gil->cond, so each release by the holder woke the waiter early and restarted the full switch interval. On a multi-core machine, a holder that releases the GIL briefly and often (for example zlib.crc32 or hashlib on large inputs between short stretches of Python code) re-takes it before the waiter runs. The waiter's interval then never expires, and it can wait for seconds. Measure the interval from the start of the wait, or from the last switch to another thread, so that early wake-ups no longer restart it. Wait only for the remaining time, and decide from the monotonic clock whether the interval has passed. On macOS, the condition variable uses the wall clock, so the result of the wait alone is not sufficient.
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've attempted to fix the issue that I've submitted yesterday (#161868).
I'm not a C expert and the code has been written with a lot help from AI, but I've spend good time understanding the problem and the solution and I think it's correct. I'm also not very attached to this code, so I'd be happy for someone else to fix it with or without any ideas in this PR.
How it works
take_gil()now keeps one switch interval deadline across early wake-ups. The interval starts when the thread begins to wait. It starts again only after a switch to another thread, or after this thread has set a drop request. Each wait is only for the time that remains, and the monotonic clock decides whether the interval has passed.With the reproducer from the issue, the longest
time.sleep(0.001)in the main thread is now 7–8 ms on my macOS.Test
I wasn't sure on the best way to test this, but in the end testing observable behaviour felt least bad.
The test makes the main thread take the GIL back 20 times from a thread that releases it often in
zlib.crc32(). With the fix, the test takes about 150 ms. Without the fix, the main thread gets the GIL only when the worker stops atsupport.SHORT_TIMEOUT, and the test fails. On a single core, the test cannot detect the bug, because the waiting thread wins at every release.I ran the full test suite and
make patchcheckon macOS (arm64). I did not test on Windows.Fixes #161868.