Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the free-threaded build of CPython 3.13 #1456

Open
wants to merge 33 commits into
base: master
Choose a base branch
from

Conversation

lysnikolaou
Copy link
Contributor

@lysnikolaou lysnikolaou commented Dec 20, 2024

What do these changes do?

  • Add a matrix config for free-threaded Python to run tests with it.
  • Change configuration to correctly build wheels under the free-threaded build.
  • Fix thread-safety issues in the Cython extension module.

Are there changes in behavior for the user?

No.

Related issue number

Resolves #1455

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Dec 20, 2024
Copy link

codspeed-hq bot commented Jan 23, 2025

CodSpeed Performance Report

Merging #1456 will not alter performance

Comparing lysnikolaou:free-threading-support (dcb3ae1) with master (0db43c6)

Summary

✅ 99 untouched benchmarks

Copy link

codecov bot commented Jan 23, 2025

Codecov Report

Attention: Patch coverage is 95.55556% with 6 lines in your changes missing coverage. Please review.

Project coverage is 95.49%. Comparing base (0db43c6) to head (dcb3ae1).

Files with missing lines Patch % Lines
yarl/_quoting_c.pyx 76.19% 5 Missing ⚠️
packaging/pep517_backend/_backend.py 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1456      +/-   ##
==========================================
- Coverage   95.57%   95.49%   -0.09%     
==========================================
  Files          33       33              
  Lines        6010     6032      +22     
  Branches      364      364              
==========================================
+ Hits         5744     5760      +16     
- Misses        240      246       +6     
  Partials       26       26              
Flag Coverage Δ
CI-GHA 95.49% <95.55%> (-0.09%) ⬇️
MyPy 49.78% <99.10%> (+0.06%) ⬆️
OS-Linux 98.65% <93.02%> (-0.18%) ⬇️
OS-Windows 98.76% <96.29%> (-0.09%) ⬇️
OS-macOS 98.46% <93.02%> (-0.12%) ⬇️
Py-3.10.11 98.38% <93.02%> (-0.18%) ⬇️
Py-3.10.16 98.61% <93.02%> (-0.18%) ⬇️
Py-3.11.11 98.61% <93.02%> (-0.18%) ⬇️
Py-3.11.9 98.38% <93.02%> (-0.18%) ⬇️
Py-3.12.8 98.38% <93.02%> (-0.18%) ⬇️
Py-3.12.9 98.61% <93.02%> (-0.18%) ⬇️
Py-3.13.1 98.61% <93.02%> (+0.04%) ⬆️
Py-3.13.2 98.61% <93.02%> (-0.18%) ⬇️
Py-3.9.13 98.34% <93.02%> (-0.18%) ⬇️
Py-3.9.21 98.57% <93.02%> (-0.18%) ⬇️
Py-pypy7.3.16 98.66% <96.29%> (-0.09%) ⬇️
Py-pypy7.3.18 98.68% <96.29%> (-0.09%) ⬇️
VM-macos-latest 98.46% <93.02%> (-0.12%) ⬇️
VM-ubuntu-latest 98.65% <93.02%> (-0.18%) ⬇️
VM-windows-latest 98.76% <96.29%> (-0.09%) ⬇️
pytest 98.65% <93.02%> (-0.18%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lysnikolaou lysnikolaou force-pushed the free-threading-support branch 3 times, most recently from 78f84fd to 1c857ca Compare February 17, 2025 13:04
@lysnikolaou lysnikolaou force-pushed the free-threading-support branch from 9bfa0d3 to 44ee894 Compare February 17, 2025 13:06
lysnikolaou and others added 3 commits February 17, 2025 15:30
Co-authored-by: 🇺🇦 Sviatoslav Sydorenko (Святослав Сидоренко) <[email protected]>
@webknjaz
Copy link
Member

FTR the CI is failing due to actions/setup-python#1034 and the following fix will need to be implemented here: aio-libs/frozenlist#622 + aio-libs/frozenlist#633.

@lysnikolaou
Copy link
Contributor Author

Out of curiosity, have you checked the importorskip() idea? Does it not work?

Yes. mypy fails if we use importorskip, because it cannot find the BenchmarkFixture attribute. Not sure why that is.

@Dreamsorcerer
Copy link
Member

Out of curiosity, have you checked the importorskip() idea? Does it not work?

Yes. mypy fails if we use importorskip, because it cannot find the BenchmarkFixture attribute. Not sure why that is.

The old code referenced the result of importorskip() instead of importing the module, no type checker is going to know what that is. If you wanted to use both, you could use importorskip for the runtime behaviour, then use if TYPE_CHECKING to import the module for type checkers.

@Dreamsorcerer
Copy link
Member

i.e.

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from pytest_codspeed import BenchmarkFixture

def test...(benchmark: "BenchmarkFixture"):

@lysnikolaou
Copy link
Contributor Author

If you wanted to use both, you could use importorskip for the runtime behaviour, then use if TYPE_CHECKING to import the module for type checkers.

Thanks for the pointer. Do you feel that this is a better approach than the one currently implemented? The current one does both in a single try/except, so it feels a bit clener to me.

@Dreamsorcerer
Copy link
Member

If you wanted to use both, you could use importorskip for the runtime behaviour, then use if TYPE_CHECKING to import the module for type checkers.

Thanks for the pointer. Do you feel that this is a better approach than the one currently implemented? The current one does both in a single try/except, so it feels a bit clener to me.

I don't mind either way. @webknjaz?

@lysnikolaou lysnikolaou force-pushed the free-threading-support branch from 4c8fe2f to 3527edc Compare February 17, 2025 18:00
@lysnikolaou
Copy link
Contributor Author

FTR the CI is failing due to actions/setup-python#1034 and the following fix will need to be implemented here: aio-libs/frozenlist#622 + aio-libs/frozenlist#633.

One alternative to this would be to use setup-uv instead of setup-python, which could also speed up dependency installation etc. If you feel okay about this, I can try it out in a separate PR. Otherwise, I can also move the custom actions to this repo as well.

@webknjaz
Copy link
Member

FTR the CI is failing due to actions/setup-python#1034 and the following fix will need to be implemented here: aio-libs/frozenlist#622 + aio-libs/frozenlist#633.

One alternative to this would be to use setup-uv instead of setup-python, which could also speed up dependency installation etc. If you feel okay about this, I can try it out in a separate PR. Otherwise, I can also move the custom actions to this repo as well.

I'm still looking into the uv integration, I'm not sure it's ready for prime time just yet. I'll be looking into that separately. But feel free to send a PR with the caching fix separately.

@bdraco
Copy link
Member

bdraco commented Feb 20, 2025

The performance regression in the writer looks real. I can't come up with a better solution though that is thread-safe.

@lysnikolaou
Copy link
Contributor Author

The performance regression in the writer looks real. I can't come up with a better solution though that is thread-safe.

We could try to make it so that the global buffer is used in the non-free-threaded build, but not in the free-threaded one. Cython, however, makes that particularly hard. There's two possible solutions:

  • Rewrite the buffer handling in C, where we can use #ifdef Py_GIL_DISABLED.
  • Use the Cython compile-time DEF and IF features, that have been deprecated and have no good alternatives.

Both of the solutions are sub-optimal. Thoughts?

@bdraco
Copy link
Member

bdraco commented Feb 21, 2025

The C solution sounds better because it will work long term vs the DEF and IF are guaranteed to break sooner

@Dreamsorcerer
Copy link
Member

Are there any plans to expose Py_GIL_DISABLED in Cython? If so, might make more sense to come back and fix it when that happens.

If we do want to write it without Cython, should we consider Rust instead of C?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided There is a change note present in this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support the free-threaded build of CPython 3.13
4 participants