Skip to content

Support for free-threaded CPython (3.14t or newer) #503

@nascheme

Description

@nascheme

This issue is intended to track the efforts of adding support for the free-threaded build for CPython (3.14t and potentially 3.13t). The Python free-threading guide contains some helpful advice on approaching this task. Based on what has been done for pybind11, I think the following steps make sense:

  • Add mechanism to set the Py_MOD_GIL_NOT_USED. I have a patch that does something similar to what pybind11 does: add a variadic macro with an optional flag so extensions can opt-in to free-threaded support
  • Replace borrow based APIs with strong reference ones (e.g. PyDict_GetItemRef)
  • Use a mutex to protect global state (e.g. the registry)
  • Fix references to ob_refcnt, don't use Py_REFCNT() as an l-value.
  • Modify tests so we can run them in parallel, run with the TSAN build to find races
  • Fixes to "faber" to work with new versions of Python. The most recent release of faber fixes most things, I believe.

I have code that does most of this, I will create a PR. I'm still working on some crash bugs when running with 3.14t. If you run the Boost.Python tests with a single thread, they pass. So that's encouraging.

Related issues:

Related PRs:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions