Skip to content

ENH: Free-Threading (No-GIL) Support for Python 3.13+ #47

Description

@HaoZeke

From a quick look it seems like the main issue will be synchronizing access to the Pool state. So perhaps:

  • A per-instance std::mutex (from libcpp.mutex) within the cymem.Pool class.
  • alloc etc. will be protected by the C++ mutex using lock_guard and with nogil: before acquiring the mutex (so we don't deadlock with the GIL).
  • Pool.realloc might need a different approach / refactor, since we can't recursively have lock acquisition.
  • Py_INCREF/Py_DECREF is imported by unused, might be needed for own_pyref and __dealloc__.

The standard TODOs for adding free-threading support are:

  • Audit Python bindings and declare them free-threading compatible (xref https://py-free-threading.github.io/porting/#updating-extension-modules).
  • Run the test suite with pytest-run-parallel to find potential issues, and fix them.
  • Run the test suite under ThreadSanitizer. If possible, depends on how many dependencies there are and if they run under TSan.
  • Add cp313t-* to CI to build free-threading wheels.

For more details, please see the suggested plan of attack in the py-free-threading guide.

Note that this is the first time I've looked at this repo, so I might be
missing known issues or code that needs closer inspection. Any suggestions here
will be very useful.

Let me know if I ought to hold off for any reason. One thing which is slightly concerning is that there are few in-project tests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions