-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
build freethreading wheels #2491
Conversation
Signed-off-by: mayeut <[email protected]>
Does nothing on Darwin / macOS but that's supported.
On macOS, at least on macOS arm64>=14, python3.13t, there's an existing UNIX socket on syslog. Rework the test to ignore pre-existing sockets & just search for the newly created one.
The macOS-14 failure is just a flaky test. Nothing to do with this PR. |
Sorry for the probably noob questions, but here goes:
|
This generates 1 additional wheel per axis in the build matrix so 7 additional wheels (2 for macOS, 2 for Windows, 3 for Linux). It only needed tweaking cibuildwheel configuration in order to build & tests those wheels.
The existing set of tests is ran for those new wheels, except on Windows where the tests are not run pending pywin32 availability for free-threading python (see the comment in |
Happy to help out with adding support for free-threaded Python. I've been actively working on adding support in many projects. Also see https://py-free-threading.github.io with lots more resources and suggestions for porting code. That said, because psutil uses We're also hopeful that we'll be able to define a new "abi4" or similar mechanism that will support the free-threaded build and allow generating a single wheel that supports e.g. Python 3.8 or newer with both the free-threaded and "normal" ABI. That will hopefully arrive in time for either Python 3.14 or 3.15. |
See python/cpython#127945. It looks there are lots of thread safety issues in ctypes under free-threading and this is being actively worked on upstream. The tricky aspect of this is that the "normal" way people get warnings about the lack of thread safety in C code wrapped by Python is they'll see a
This is a little unfortunate. ping @ZeroIntensity since it looks like you're working on the ctypes code in CPython. Do you have any idea what psutil should do to support free-threaded Python 3.13? Report thread safety bugs upstream? Or assume ctypes in 3.13 is unsafe and add lots of |
It would be great if they reported bugs upstream, but right now 3.13 ctypes will stay thread-unsafe, because the changes are too complex to backport. You'll have to wait until 3.14 before it becomes properly thread safe. It might be worth marking ctypes as requiring the GIL for 3.13, so it gets re-enabled at runtime. Would that be preferable compared to adding locks everywhere? |
Unfortunately the ship might have sailed on that one. Many projects that are already shipping free-threaded wheels for 3.13 implicitly import ctypes, so making that change would cause those modules to also re-enable the GIL all of the sudden in a bugfix release of Python. |
Hi
psutil does not use ctypes. It was only used in one place on Linux for Python 2.7, but it was removed after I dropped support for Python 2.7. The rest of the code is a mix of Python and C extension modules, which use
Considering what @mayeut stated above, I'm afraid it's better to wait for abi4. Despite numerous efforts over the years (e.g. the retry_on_failure decorator or the tolerances), I've never managed to make the psutil test suite matrix really "stable". The OS often interferes with the unit tests by spawing new PIDs behind your back, or increasing the used memory for its own purposes, opening connections, etc. Things you just can't control. To get an idea, see how many red crosses we have. Adding 7 additional test runs to the matrix would mean getting even more failures, which is a problem especially when I have to make a new release. E.g. I never added a CI job for PYPY for this reason. |
No. In fact, they're quite important for free-threading. The critical section API is heavily reliant on |
Oops! Clearly I need to look closer at the code. I'll try to understand this more and get back to you with more ideas about the CI issues you raised. |
Question: what happens now when you |
Also, do tests pass ( |
Yup! This is on my Mac with developer tools installed:
I see four test failures on the same Mac:
Along with lots of warnings like:
Ah and now I understand why I don't see RuntimeWarnings - it looks like Sam Gross already did a pass over |
Does it mean we have to replace this: Py_BEGIN_ALLOW_THREADS
blocking_syscall();
Py_END_ALLOW_THREADS ...with this? Py_BEGIN_CRITICAL_SECTION(self);
Py_BEGIN_ALLOW_THREADS
blocking_syscall();
Py_END_ALLOW_THREADS
Py_END_CRITICAL_SECTION(); |
No, you only need to lock For example, if you locked |
Instead of thinking about when it's legal to use the C API in terms of acquiring and releasing the GIL, I find it better to think about attaching to and detaching from the runtime. It's only legal to call into the C API if a thread is attached to the runtime, but Python is only able to do global synchronization events like running the garbage collector when all threads are detached. So just like how on the GIL-enabled build you call The main difference is it's no longer necessary to call |
If Let's provide wheels later when we have abi4. |
Summary
Description
build freethreading wheels
fix tests issues discovered while adding those