Repository navigation
[Python] Do not fall back to the GIL on free-threaded Python - #22548
guitargeek wants to merge 1 commit into
Conversation
On a free-threaded ("GIL-less") Python build, importing a C extension
module that does not explicitly declare free-threading support makes
CPython re-enable the GIL at runtime and print a warning. Mark both the
CPyCppyy module and the ROOT pythonizations module with
Py_MOD_GIL_NOT_USED via PyUnstable_Module_SetGIL so that importing ROOT
no longer forces the GIL back on.
The calls are guarded by Py_GIL_DISABLED and therefore have no effect on
regular GIL-enabled builds.
Note that this is a declaration of intent, not a guarantee of full
thread-safety: ROOT's interpreter and global state have not yet been
audited for concurrent use, so unsynchronized multithreaded access may
still produce wrong results or crashes. The purpose of the change is to
signal our commitment to supporting free-threaded Python and to
encourage users to run multithreaded workloads and report issues, so
that the remaining thread-safety problems can be surfaced and fixed.
Test Results 22 files 22 suites 3d 15h 54m 0s ⏱️ For more details on these failures, see this check. Results for commit 7ac7cd3. |
vepadulano
left a comment
There was a problem hiding this comment.
I'm not opposed to the idea, I wonder if it would be wise to print a message when a user is building/using ROOT in a free-threaded Python environment to communicate them what you write in the PR decsription.
Interesting question! My new answer to this is that we avoid using the C Python API in ROOT completely, and then we don't have the responsibility of making a CPython extension compatible with free-threading anymore. Here is the current state: |
On a free-threaded ("GIL-less") Python build, importing a C extension module that does not explicitly declare free-threading support makes CPython re-enable the GIL at runtime and print a warning. Mark both the CPyCppyy module and the ROOT pythonizations module with Py_MOD_GIL_NOT_USED via PyUnstable_Module_SetGIL so that importing ROOT no longer forces the GIL back on.
The calls are guarded by Py_GIL_DISABLED and therefore have no effect on regular GIL-enabled builds.
Note that this is a declaration of intent, not a guarantee of full thread-safety: ROOT's interpreter and global state have not yet been audited for concurrent use, so unsynchronized multithreaded access may still produce wrong results or crashes. The purpose of the change is to signal our commitment to supporting free-threaded Python and to encourage users to run multithreaded workloads and report issues, so that the remaining thread-safety problems can be surfaced and fixed.