Skip to content

Audit all built-in modules for thread safety #116738

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

  • Modules/_abc.c
  • Modules/arraymodule.c - gh-128942: make arraymodule.c free-thread safe (lock-free) #130771
  • Modules/_asynciomodule.c
  • Modules/atexitmodule.c
  • Modules/binascii.c
  • Modules/_bisectmodule.c
  • Modules/_blake2/blake2module.c
  • Modules/_bz2module.c
  • Modules/cjkcodecs/cjkcodecs.h
  • Modules/cjkcodecs/multibytecodec.c
  • Modules/cmathmodule.c
  • Modules/_codecsmodule.c
  • 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
  • Modules/errnomodule.c
  • Modules/faulthandler.c
  • Modules/fcntlmodule.c
  • Modules/_functoolsmodule.c
  • Modules/gcmodule.c
  • Modules/_gdbmmodule.c
  • Modules/grpmodule.c
  • Modules/_hashopenssl.c
  • Modules/_heapqmodule.c
  • Modules/_io/_iomodule.c
  • Modules/itertoolsmodule.c
  • Modules/_json.c - gh-116738: Make _json module safe in the free-threading build #119438
  • Modules/_localemodule.c
  • Modules/_lsprof.c
  • Modules/_lzmamodule.c
  • Modules/mathmodule.c
  • 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
  • Modules/_queuemodule.c
  • Modules/_randommodule.c
  • Modules/readline.c
  • Modules/resource.c
  • Modules/_scproxy.c
  • Modules/selectmodule.c
  • Modules/sha1module.c
  • Modules/sha2module.c
  • Modules/sha3module.c
  • Modules/signalmodule.c
  • Modules/socketmodule.c
  • Modules/_sqlite/module.c
  • Modules/_sre/sre.c
  • Modules/_ssl.c
  • Modules/_stat.c
  • Modules/_statisticsmodule.c
  • 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
  • Modules/_threadmodule.c
  • Modules/timemodule.c
  • Modules/_tkinter.c
  • Modules/_tracemalloc.c
  • Modules/_typingmodule.c
  • Modules/unicodedata.c
  • Modules/_uuidmodule.c
  • 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
  • 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
  • Python/instrumentation.c
  • Python/marshal.c
  • Python/Python-ast.c
  • Python/Python-tokenize.c
  • Python/sysmodule.c
  • Python/_warnings.c
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

Activity

  1. self-assigned this
    on Mar 13, 2024
  2. added a commit that references this issue on Apr 11, 2024
  3. added a commit that references this issue on Apr 17, 2024
  4. added a commit that references this issue on May 2, 2024
  5. added a commit that references this issue on May 8, 2024
  6. mpage commented on May 28, 2024

    @mpage
    Contributor

    @swtaarrs - Is it OK if I remove you as the owner?

  7. removed their assignment
    on May 29, 2024
  8. swtaarrs commented on May 29, 2024

    @swtaarrs
    MemberAuthor

    Yep, I won't be doing any more work on this 😞

  9. added 4 commits that reference this issue on Jun 5, 2024
  10. 150 remaining items

  11. added a commit that references this issue on Dec 8, 2025
  12. added a commit that references this issue on Dec 12, 2025
  13. added a commit that references this issue on Dec 15, 2025
  14. added a commit that references this issue on Dec 15, 2025
  15. added a commit that references this issue on Dec 16, 2025
  16. added a commit that references this issue on Dec 18, 2025
  17. raven-dev-ops commented on Dec 28, 2025

    @raven-dev-ops

    Audited Modules/binascii.c for free-threaded builds (gh-116738) and added regression coverage to ensure importing binascii with -X gil=0 does not enable the GIL, plus a multi-thread stress test.

    PR: #143234

  18. added a commit that references this issue on Jan 6, 2026
  19. added a commit that references this issue on Jan 13, 2026
  20. added 2 commits that reference this issue on Feb 15, 2026
  21. added a commit that references this issue on Mar 7, 2026
  22. added a commit that references this issue on Apr 11, 2026
  23. added 2 commits that reference this issue on Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions