Skip to content

Support the free-threaded build of CPython 3.13 #1455

@lysnikolaou

Description

@lysnikolaou

Is your feature request related to a problem?

No response

Describe the solution you'd like

Hey all! 👋

I took the liberty of looking into adding support for the 3.13 free-threaded build of CPython after multidict (I also plan to look at other libraries under the aio-libs organization). This aims to be a tracking issue for all work necessary to support it, for which I'm going to open a PR shortly.

  • Set up CI for free-threading
  • Audit the Cython extension module for thread-safety issues
    • The only issues seems to be the usage of a statically allocated buffer for the Writer struct. That's fairly straightforward to solve.
  • Audit the Python code for thread-safety issues
    • One fairly complicated thing to figure out was that there's some thread safety issues in the Python code as well. This stems from havily using non-thread-local caches when creating URL instances. Instantiating a URL with a string url for example calls encode_url which uses an lru_cache. This means that multiple threads might end up sharing the same object, even if the URL object appears to have been created locally. There's a couple possible solution to this:
      • Disable lru_cache under the free-threaded build so that object sharing between threads is more straightfoward.
      • Instead of using lru_cache to cache the URL object itself, it could instead cache the immutable parts that make up a URL.
  • Mark the Cython extension module as thread-safe with setting freethreading_compatible in the cython directives (can be easily done through pyproject.toml).
  • Build wheels for the free-threaded build.

Hope this all sounds good! Let me know in case there's something that doesn't seem right.

Describe alternatives you've considered

  • We could say that people testing on the free-threaded build should only install the pure-Python wheels, but that doesn't seem like the best say forward, since the required changes are not that intrusive.

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions