Skip to content

Audit all built-in modules for thread safety #116738

Open

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 a call to PyModule_Create(), an array of PyModuleDef_Slots, or a PyModExport_* function definition. 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.

For modules split across multiple files, the audit should include their implementation helpers, not just the file defining the module.

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

Checked items record completed initial audits or conversions; follow-up issues are listed below. Unchecked items may have partial fixes; their links include completed work and outstanding issues.

Follow-up issues

Cross-module tracking 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

https://peps.python.org/pep-0779/ and its Steering Council acceptance.

Further reading

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. 152 remaining items

  11. added a commit that references this issue on Dec 15, 2025
  12. added a commit that references this issue on Dec 15, 2025
  13. added a commit that references this issue on Dec 16, 2025
  14. added a commit that references this issue on Dec 18, 2025
  15. 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

  16. added a commit that references this issue on Jan 6, 2026
  17. added a commit that references this issue on Jan 13, 2026
  18. added 2 commits that reference this issue on Feb 15, 2026
  19. added a commit that references this issue on Mar 7, 2026
  20. added a commit that references this issue on Apr 11, 2026
  21. added 2 commits that reference this issue on Apr 25, 2026
  22. ngoldbaum commented on Oct 30, 2026

    @ngoldbaum
    Contributor

    I just did a (clanker-assisted) update pass on the issue.

    I updated the source-file listing to reflect CPython main. I also clarified the scope a little in the intro, noting that PyModExport_* definitions are a thing now and that some modules span several compilation units.

    I added links to completed and outstanding audit work, and grouped later fixes and bugs under an expanded “Follow-up issues” section. That gives the initial audit a clear completion point while acknowledging that we are imperfect and bugs happen: checked boxes mean the initial audit or conversion is complete.

    I also gave _io separate helper-file checkboxes (ping @cmaloney @KowalskiThomas), listed cross-module trackers separately, and removed the stale “Completed Issues” block. Finally, I added links to PEP 779, the free-threaded extension porting HOWTO, the community free-threading guide, and @nascheme's guidance.

  23. added a commit that references this issue on Nov 2, 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