Feature or enhancement
Proposal:
We should audit every built-in module for thread safety and make any necessary fixes. This can be done separately from tagging the modules as safe using Py_mod_gil; any data races in built-in modules are considered bugs in the free-threaded build and not incompatibilities.
Below is a list of all source files that contain at least one module definition, as defined by files that contain either a call to PyModule_Create() or an array of PyModuleDef_Slots. If you'd like to help out with this task, take a look at one of the incomplete files. If the conversion is trivial, check it off and attach the PR to this issue. Otherwise, convert the line into an issue and assign it to yourself.
Every module is different, but here are a few high-level points to guide the work:
- Use
PyMutex as the basic unit of locking.
- If you need to hold a lock for a longer period of time, especially across calls that may reenter Python code or acquire other locks, use critical sections.
- If the module you're looking at is a thin wrapper around related code elsewhere in CPython (e.g.,
Modules/_codecsmodule.c and Python/codecs.c), you can also audit/convert the related non-module code. Otherwise, try to contain your work to just the module code, and create separate issues for any dependencies that aren't thread-safe.
- Remember that C API functions are generally expected to handle thread-safety internally. C code that operates on Python objects using only C API calls is usually thread-safe by default (but look out for code that reads or modifies
PyObject*s in C structs, since that needs synchronization).
- Watch out for functions/macros that return borrowed references, like
PyList_GetItem() or PyDict_GetItem(). If other threads could have references to the object, prefer functions like PyList_GetItemRef() or PyDict_GetItemRef() that return owned references (and will safely raise an error if the index/key doesn't exist).
Files to audit
Completed Issues
Completed Issues
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
https://peps.python.org/pep-0703/, especially this section
Linked PRs
Feature or enhancement
Proposal:
We should audit every built-in module for thread safety and make any necessary fixes. This can be done separately from tagging the modules as safe using
Py_mod_gil; any data races in built-in modules are considered bugs in the free-threaded build and not incompatibilities.Below is a list of all source files that contain at least one module definition, as defined by files that contain either a call to
PyModule_Create()or an array ofPyModuleDef_Slots. If you'd like to help out with this task, take a look at one of the incomplete files. If the conversion is trivial, check it off and attach the PR to this issue. Otherwise, convert the line into an issue and assign it to yourself.Every module is different, but here are a few high-level points to guide the work:
PyMutexas the basic unit of locking.Modules/_codecsmodule.candPython/codecs.c), you can also audit/convert the related non-module code. Otherwise, try to contain your work to just the module code, and create separate issues for any dependencies that aren't thread-safe.PyObject*s in C structs, since that needs synchronization).PyList_GetItem()orPyDict_GetItem(). If other threads could have references to the object, prefer functions likePyList_GetItemRef()orPyDict_GetItemRef()that return owned references (and will safely raise an error if the index/key doesn't exist).Files to audit
Completed Issues
Completed Issues
_warnings.cthread-safe in free-threaded build #116664_threadmodule.cthread-safe in--disable-gilbuilds #114271_structmodule thread-safe in--disable-gilbuilds #112062defaulttimeoutin free-threaded build #116616Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
https://peps.python.org/pep-0703/, especially this section
Linked PRs
_codecsmodule thread-safe #117530_csvmodule thread-safe (GH-118344) #125328dbm.gnumodule on FT Python build #138467cProfilemodule thread-safe (GH-138229) #138575resourcemodule on free-threading builds (GH-138504) #138793mmapmodule thread-safe (GH-139237) #139825dbm.gnumodule on FT Python build (GH-138467) #139987uuidmodule thread safety in free-threading (GH-140068) #140229contextvars.Contextthread safe #143074