Skip to content

Support free-threaded Python build #8796

Closed

Description

@ngoldbaum

Is your feature request related to a problem?

Python 3.13 will be available as an experimental free-threaded build that has the GIL disabled. It should hopefully be possible to build aiohttp on the free-threaded build with an updated Cython. I just did so (with some makefile hacking so the build uses a prerelease cython):

Python 3.13.0rc1 experimental free-threading build (main, Aug 14 2024, 13:20:46) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import aiohttp
<frozen importlib._bootstrap>:488: RuntimeWarning: The global interpreter lock (GIL) has been enabled to load module 'aiohttp._helpers', which has not declared that it can run safely without the GIL. To override this behavior and keep the GIL disabled (at your own risk), run with PYTHON_GIL=0 or -Xgil=0.

Describe the solution you'd like

I don't know what thread safety issues exist in aiohttp, but assuming any C/C++ code is thread safe and the cython code is thread safe, you can declare the modules support free-threading by following https://py-free-threading.github.io/porting/.

Describe alternatives you've considered

Not supporting it?

Related component

Server, Client

Additional context

No response

Code of Conduct

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

Activity

  1. Dreamsorcerer commented on Aug 20, 2024

    @Dreamsorcerer
    Member

    If you'd like to help, I think the first thing would be to add a new test run to the CI, so we have some test coverage over this new approach. But, I'd assume the Cython code is likely thread-safe as it's just used for parsers.

    Probably need the current 3.13 tests passing first though, which will hopefully happen next week after uvloop publish a 3.13 wheel.

  2. webknjaz commented on Aug 21, 2024

    @webknjaz
    Member

    I think that getting our in-org hosted deps to run CI / make builds might be another blocker. I think that aiohttp's CI should make use of those wheels rather than compiling them.

  3. Dreamsorcerer commented on Sep 27, 2024

    @Dreamsorcerer
    Member

    3.13 is fully tested and passing now, if anyone wants to look at adding the free-threading testing.

  4. webknjaz commented on Oct 18, 2024

    @webknjaz
    Member

    @Yushu2606 what's your point? That doesn't seem related to the discussion here.

  5. ngoldbaum commented on Oct 18, 2024

    @ngoldbaum
    Author

    @Yushu2606 you probably aren't building with a nightly cython. Right now there isn't a tagged cython version or release that supports free-threading.

  6. patrizok commented on Nov 13, 2024

    @patrizok
  7. mshakery commented on Mar 7, 2025

    @mshakery

    Is this forgotten?

  8. Dreamsorcerer commented on Mar 7, 2025

    @Dreamsorcerer
    Member

    No, plenty of work has been done on aiohttp dependencies, so looks like it's shaping up for a release in the near future. Plenty of the ecosystem is still not ready for free-threaded, so it's not been a trivial change (e.g. needing to use an alpha release of Cython).

  9. electroglyph commented on Apr 2, 2025

    @electroglyph

    i'm interested, too. is there a list of free-threaded blockers that need to be addressed?

  10. electroglyph commented on Apr 2, 2025

    @electroglyph

    welp, i just built it for free-threaded on Windows. here's how I did it, i'm sure there will be opinions =)
    there's surely a less hacky way to go with the macros...

    i've only tested the http client, which works, but i get this msg, any pointers on how to remove this warning would be appreciated:

    <frozen importlib._bootstrap>:488: RuntimeWarning: The global interpreter lock (GIL) has been enabled to load module 'aiohttp._http_writer', which has not declared that it can run safely without the GIL. To override this behavior and keep the GIL disabled (at your own risk), run with PYTHON_GIL=0 or -Xgil=0.

    here's what i changed:

    setup.py:

    reason: cython's maze of #defs and #undefs

    macros = [("GRAALVM_PYTHON", "1"), ("Py_GIL_DISABLED", "1")]
    extensions = [
        Extension("aiohttp._websocket.mask", ["aiohttp/_websocket/mask.c"], define_macros=macros),
        Extension(
            "aiohttp._http_parser",
            [
                "aiohttp/_http_parser.c",
                "aiohttp/_find_header.c",
                "vendor/llhttp/build/c/llhttp.c",
                "vendor/llhttp/src/native/api.c",
                "vendor/llhttp/src/native/http.c",
            ],
            define_macros=[("LLHTTP_STRICT_MODE", 0),("GRAALVM_PYTHON", "1"), ("Py_GIL_DISABLED", "1")],
            include_dirs=["vendor/llhttp/build"],
        ),
        Extension("aiohttp._http_writer", ["aiohttp/_http_writer.c"], define_macros=macros),
        Extension("aiohttp._websocket.reader_c", ["aiohttp/_websocket/reader_c.c"], define_macros=macros),
    ]

    _http_parser.c, line 30742:
    reason: aiohttp/_http_parser.c(30744): error C2039: 'ob_refcnt': is not a member of '_object'

    #if !CYTHON_USE_TP_FINALIZE
        assert(Py_REFCNT(self) > 0);
        // if (likely(--self->ob_refcnt == 0)) {
        //     return;
        // }
        {
            Py_ssize_t refcnt = Py_REFCNT(self);
            _Py_NewReference(self);
            __Pyx_SET_REFCNT(self, refcnt);
        }

    websocket\reader_c.c line 18485:
    reason: aiohttp/_websocket/reader_c.c(18487): error C2039: 'ob_refcnt': is not a member of '_object'

    #if !CYTHON_USE_TP_FINALIZE
        assert(Py_REFCNT(self) > 0);
        // if (likely(--self->ob_refcnt == 0)) {
        //     return;
        // }
        {
            Py_ssize_t refcnt = Py_REFCNT(self);
            _Py_NewReference(self);
            __Pyx_SET_REFCNT(self, refcnt);
        }
  11. Dreamsorcerer commented on Apr 2, 2025

    @Dreamsorcerer
    Member

    Well, the warning is because it doesn't support free-threading. Fixing the warning is done by supporting free-threading, i.e. what this issue tracks.
    I think multidict and frozenlist have free-threading now, and yarl I believe is ready to merge and release. That'll probably be all the dependencies sorted, then it's just aiohttp itself that needs a similar update, so seems like it's getting close now.

  12. webknjaz commented on Apr 2, 2025

    @webknjaz
    Member

    Isn't there some optional DNS/speedup stuff on top of the mandatory deps?

  13. Dreamsorcerer commented on Apr 2, 2025

    @Dreamsorcerer
    Member

    I would assume an optional dependency is not a blocker to us. Maybe pycares will need an update? But, people can use it without aiodns until that becomes supported.

  14. ngoldbaum commented on Apr 2, 2025

    @ngoldbaum
    Author

    It looks like pycares uses CFFI, so that might require using our fork of CFFI to get that working. See python-cffi/cffi#143 (comment). I'll open an issue over at the pycares repo.

  15. 9 remaining items

  16. Dreamsorcerer commented on Jul 17, 2025

    @Dreamsorcerer
    Member

    [XPASS(strict)] pytest-dev/pytest#13546

    You're running a much older version of pytest.

      with f.open("rb") as fd:
    

    E ValueError: I/O operation on closed file.

    This one looks weird. How can it be a closed file when we're attempting to open it?

  17. ngoldbaum commented on Jul 17, 2025

    @ngoldbaum
    Author

    You're running a much older version of pytest.

    Thanks! This is because of the hacked-together, manually constructed environment.

    This one looks weird. How can it be a closed file when we're attempting to open it?

    On a second look, I agree. Maybe there's something tricky going on in the pathlib implementation along the lines of python/cpython#136248, which seems similar. I'll try to reproduce this outside of aiohttp.

  18. ngoldbaum commented on Jul 23, 2025

    @ngoldbaum
    Author

    I just re-did this exercise with a Python 3.14t interpreter. This required even more hacking of test dependencies, since a few of the dependencies in the test requirements file are pinned to versions that don't support 3.14.

    With 3.14t, I no longer see the pathlib error I saw before. It's possible the fix for the issue I saw has been fixed in CPython main for a while and the fix was never backported to 3.13 - I'll try to pin that down so we can make sure there's a backport.

    With 3.14t I see the following test failures and errors in the aiohttp tests:

    ERROR tests/test_proxy_functional.py::test_uvloop_secure_https_proxy - AttributeError: 'NoneType' object has no attribute 'new_event_loop'
    FAILED tests/test_helpers.py::test_when_timeout_smaller_second[pyloop] - assert 0.005357708083465695 == 0 ± 0.001
    FAILED tests/test_cookiejar.py::test_pickle_format - AssertionError: assert defaultdict(<...'fifteenth'>}) == defaultdict(<...'fifteenth'>})
    FAILED tests/test_circular_imports.py::test_no_warnings[aiohttp._http_parser] - subprocess.CalledProcessError: Command '('/Users/goldbaum/.pyenv/versions/3.14.0rc1t/bin/python3.14', '-W', 'error', '-W',...
    FAILED tests/test_circular_imports.py::test_no_warnings[aiohttp._http_writer] - subprocess.CalledProcessError: Command '('/Users/goldbaum/.pyenv/versions/3.14.0rc1t/bin/python3.14', '-W', 'error', '-W',...
    FAILED tests/test_circular_imports.py::test_no_warnings[aiohttp._websocket.mask] - subprocess.CalledProcessError: Command '('/Users/goldbaum/.pyenv/versions/3.14.0rc1t/bin/python3.14', '-W', 'error', '-W',...
    

    I think these are probably all 3.14 compatibility issues? I would need to look closer.

  19. Dreamsorcerer commented on Jul 24, 2025

    @Dreamsorcerer
    Member

    Possibly. With 3.14 in the release candidate stage, we should be able to start preparing 3.14 releases now.

  20. Dreamsorcerer commented on Jul 24, 2025

    @Dreamsorcerer
    Member

    I've added 3.14 back to the testing matrix. Looks like initial blockers are isal and pydantic-core. I think pydantic is already resolved, but Dependabot seems to think it's not upgradeable, so may need a manual version bump.

  21. ngoldbaum commented on Jul 24, 2025

    @ngoldbaum
    Author

    When I tried yesterday the incompatible constraint on pydantic-core was coming in from python-on-whales

  22. kumaraditya303 commented on Jul 26, 2025

    @kumaraditya303
    Contributor

    I've added 3.14 back to the testing matrix. Looks like initial blockers are isal and pydantic-core. I think pydantic is already resolved, but Dependabot seems to think it's not upgradeable, so may need a manual version bump.

    I have updated my PR at #10872 to avoid installing isal and python-on-whales on 3.14 as they are test are only dependencies and skipped tests if not present, with that most tests are now passing except one in cookie jar which was changed in 3.14.

    I think this is a good starting point to merge that than waiting for all such non-required dependencies to ship 3.14 wheels.

    cc @Dreamsorcerer

  23. kumaraditya303 commented on Jul 26, 2025

    @kumaraditya303
    Contributor

    When I tried yesterday the incompatible constraint on pydantic-core was coming in from python-on-whales

    The incompaible constraint comes from pydantic which is a dependency of python-on-whales, see https://github.com/pydantic/pydantic/blob/5f033e46c54fea1b59b6894d6527daf49475e690/pyproject.toml#L49

  24. Dreamsorcerer commented on Jul 26, 2025

    @Dreamsorcerer
    Member

    Looks like Pydantic should have a release in a few days, so we can wait that one out. If isal doesn't respond, then we'll have to consider your workarounds.

  25. Dreamsorcerer commented on Jul 26, 2025

    @Dreamsorcerer
    Member

    In the meantime, we can use your branch to test any fixes needed for 3.14 and merge those fixes in separately.

  26. Dreamsorcerer commented on Jul 26, 2025

    @Dreamsorcerer
    Member

    The failing test is unusual. My best guess is that Python 3.14 has changed how equality works, which seems like quite a surprising change...

  27. kumaraditya303 commented on Oct 7, 2025

    @kumaraditya303
    Contributor

    aiohttp version 3.13 is released with free-threading support, this is complete now.

  28. Dreamsorcerer commented on Oct 7, 2025

    @Dreamsorcerer
    Member

    Yes, thanks. I think we're only testing and officially supporting Python 3.14+.

  29. changed the title [-]Support free-threaded Python 3.13 build[/-] [+]Support free-threaded Python build[/+] on Oct 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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