Skip to content

Audit all built-in modules for thread safety #116738

Open
@swtaarrs

Description

@swtaarrs

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:

  1. Use PyMutex as the basic unit of locking.
  2. 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.
  3. 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.
  4. 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).
  5. 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
- [x] Modules/_abc.c
- [ ] Modules/arraymodule.c - https://github.com/python/cpython/pull/130771
- [x] Modules/_asynciomodule.c
- [x] Modules/atexitmodule.c
- [ ] Modules/binascii.c
- [ ] Modules/_bisectmodule.c
- [x] Modules/_blake2/blake2module.c
- [ ] Modules/_bz2module.c
- [ ] Modules/cjkcodecs/cjkcodecs.h
- [ ] Modules/cjkcodecs/multibytecodec.c
- [ ] Modules/cmathmodule.c
- [x] Modules/_codecsmodule.c
- [x] Modules/_collectionsmodule.c
- [ ] Modules/_contextvarsmodule.c
- [ ] Modules/_csv.c
- [ ] Modules/_ctypes/_ctypes.c
- [ ] Modules/_ctypes/_ctypes_test.c
- [ ] Modules/_cursesmodule.c
- [ ] Modules/_curses_panel.c
- [ ] Modules/_datetimemodule.c
- [ ] Modules/_dbmmodule.c
- [ ] Modules/_decimal/_decimal.c
- [ ] Modules/_elementtree.c
- [x] Modules/errnomodule.c
- [ ] Modules/faulthandler.c
- [ ] Modules/fcntlmodule.c
- [ ] Modules/_functoolsmodule.c
- [x] Modules/gcmodule.c
- [ ] Modules/_gdbmmodule.c
- [ ] Modules/grpmodule.c
- [x] Modules/_hashopenssl.c
- [ ] Modules/_heapqmodule.c
- [ ] Modules/_io/_iomodule.c
- [ ] Modules/itertoolsmodule.c
- [ ] Modules/_json.c - https://github.com/python/cpython/pull/119438
- [ ] Modules/_localemodule.c
- [ ] Modules/_lsprof.c
- [ ] Modules/_lzmamodule.c
- [ ] Modules/mathmodule.c
- [x] Modules/md5module.c
- [ ] Modules/mmapmodule.c
- [ ] Modules/_multiprocessing/multiprocessing.c
- [ ] Modules/_multiprocessing/posixshmem.c
- [ ] Modules/_opcode.c
- [ ] Modules/_operator.c
- [ ] Modules/overlapped.c
- [ ] Modules/_pickle.c
- [ ] Modules/posixmodule.c
- [ ] Modules/_posixsubprocess.c
- [ ] Modules/pwdmodule.c
- [ ] Modules/pyexpat.c
- [x] Modules/_queuemodule.c
- [x] Modules/_randommodule.c
- [ ] Modules/readline.c
- [ ] Modules/resource.c
- [ ] Modules/_scproxy.c
- [ ] Modules/selectmodule.c
- [x] Modules/sha1module.c
- [x] Modules/sha2module.c
- [x] Modules/sha3module.c
- [ ] Modules/signalmodule.c
- [x] Modules/socketmodule.c
- [ ] Modules/_sqlite/module.c
- [ ] Modules/_sre/sre.c
- [x] Modules/_ssl.c
- [ ] Modules/_stat.c
- [ ] Modules/_statisticsmodule.c
- [x] Modules/_struct.c
- [ ] Modules/_suggestions.c
- [ ] Modules/symtablemodule.c
- [ ] Modules/_sysconfig.c
- [ ] Modules/syslogmodule.c
- [ ] Modules/termios.c
- [ ] Modules/_testbuffer.c
- [ ] Modules/_testcapimodule.c
- [ ] Modules/_testclinic.c
- [ ] Modules/_testclinic_limited.c
- [ ] Modules/_testexternalinspection.c
- [ ] Modules/_testimportmultiple.c
- [ ] Modules/_testinternalcapi.c
- [ ] Modules/_testlimitedcapi.c
- [ ] Modules/_testmultiphase.c
- [ ] Modules/_testsinglephase.c
- [x] Modules/_threadmodule.c
- [ ] Modules/timemodule.c
- [ ] Modules/_tkinter.c
- [ ] Modules/_tracemalloc.c
- [ ] Modules/_typingmodule.c
- [ ] Modules/unicodedata.c
- [ ] Modules/_uuidmodule.c
- [x] Modules/_weakref.c
- [ ] Modules/_winapi.c
- [ ] Modules/_xxinterpchannelsmodule.c
- [ ] Modules/_xxinterpqueuesmodule.c
- [ ] Modules/xxlimited_35.c
- [ ] Modules/xxlimited.c
- [ ] Modules/xxmodule.c
- [ ] Modules/_xxsubinterpretersmodule.c
- [ ] Modules/xxsubtype.c
- [ ] Modules/_xxtestfuzz/_xxtestfuzz.c
- [ ] Modules/zlibmodule.c
- [x] Modules/_zoneinfo.c
- [ ] Objects/genobject.c (including ag_running_async)
- [ ] PC/msvcrtmodule.c
- [ ] PC/python3dll.c
- [ ] PC/winreg.c
- [ ] PC/winsound.c
- [ ] Python/bltinmodule.c
- [ ] Python/import.c
- [x] Python/instrumentation.c
- [ ] Python/marshal.c
- [ ] Python/Python-ast.c
- [ ] Python/Python-tokenize.c
- [ ] Python/sysmodule.c
- [x] Python/_warnings.c
Completed Issues
### Completed Issues
- [ ] https://github.com/python/cpython/issues/116664
- [ ] https://github.com/python/cpython/issues/114271
- [ ] https://github.com/python/cpython/issues/112062
- [ ] https://github.com/python/cpython/issues/111916
- [ ] https://github.com/python/cpython/issues/116616

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions