Skip to content

gh-152075: Avoid lock contention in _Py_Specialize_LoadGlobal under free threading#153720

Open
hawkinsp wants to merge 1 commit into
python:mainfrom
hawkinsp:contention
Open

gh-152075: Avoid lock contention in _Py_Specialize_LoadGlobal under free threading#153720
hawkinsp wants to merge 1 commit into
python:mainfrom
hawkinsp:contention

Conversation

@hawkinsp

@hawkinsp hawkinsp commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Under high thread concurrency in free-threaded builds, _Py_Specialize_LoadGlobalsuffers from lock contention when acquiring the critical section mutexes for the globals and builtins dictionaries during bytecode specialization.

This PR introduces non-blocking critical section entry (PyCriticalSection2_TryBegin) and updates LOAD_GLOBAL bytecode specialization to attempt a non-blocking lock acquisition. If acquiring the two object mutexes would block, specialization is skipped.

…nder free threading

Under high thread concurrency in free-threaded builds, `_Py_Specialize_LoadGlobal`suffers from lock contention when acquiring the critical section mutexes for the `globals` and `builtins` dictionaries during bytecode specialization.

This PR introduces non-blocking critical section entry (`PyCriticalSection2_TryBegin`) and updates LOAD_GLOBAL bytecode specialization to attempt a non-blocking lock acquisition. If acquiring the two object mutexes would block, specialization is skipped.
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.

1 participant