Skip to content

gh-161868: Keep the GIL switch interval deadline across early wake-ups - #161930

Open
oinopion wants to merge 2 commits into
python:mainfrom
oinopion:gil-switch-interval-deadline
Open

oinopion wants to merge 2 commits into
python:mainfrom
oinopion:gil-switch-interval-deadline

Conversation

@oinopion

@oinopion oinopion commented Nov 13, 2026 •

Copy link
Copy Markdown

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 at support.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 patchcheck on macOS (arm64). I did not test on Windows.

Fixes #161868.

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.
@python-cla-bot

python-cla-bot Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Short, frequent GIL releases block other threads for seconds

1 participant