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

ctypes thread safety auditing (and fixing) #127945

Open
4 of 12 tasks
ZeroIntensity opened this issue Dec 14, 2024 · 7 comments
Open
4 of 12 tasks

ctypes thread safety auditing (and fixing) #127945

ZeroIntensity opened this issue Dec 14, 2024 · 7 comments
Assignees
Labels
extension-modules C modules in the Modules dir topic-ctypes topic-free-threading type-feature A feature request or enhancement

Comments

@ZeroIntensity
Copy link
Member

ZeroIntensity commented Dec 14, 2024

Feature or enhancement

This is a tracking issue for all thread safety problems related to ctypes. I'll be working on this, but others can feel free to give me some help.

First of all, we need to find where the thread safety problems are.

Auditing

Preview Give feedback

I'll be tracking the issues that get found when auditing here. The plan is to just create a new issue and link to it for each new problem instead of flooding this issue with PRs.

Generally, the workflow for fixes should follow most of the rules from #116738, but I suspect we'll need recursive mutexes for quite a few things related to callbacks, because it's difficult to tell what might be re-entrant, and we can't use critical sections for arbitrary function pointers.

Known Issues

Preview Give feedback
  1. 3.13 3.14 extension-modules topic-ctypes topic-free-threading type-crash
    ZeroIntensity
  2. 3.13 3.14 extension-modules topic-ctypes topic-free-threading type-bug
  3. 3.13 3.14 extension-modules topic-ctypes topic-free-threading type-bug
    kumaraditya303
  4. 3.13 3.14 extension-modules topic-ctypes topic-free-threading type-bug
    kumaraditya303
  5. 3.13 3.14 extension-modules topic-ctypes topic-free-threading type-bug
    kumaraditya303

cc @encukou, as the ctypes genius, and @colesbury as the free-threading mastermind.

Linked PRs

@encukou
Copy link
Member

encukou commented Jan 10, 2025

Thank you! I can review the ctypes parts :)

Heads-up: I'm currently working in cfield.c/PyCField*; focus elsewhere to avoid conflicts.

@ZeroIntensity
Copy link
Member Author

Got it, thanks! I'll probably just move to callbacks.c next.

@kumaraditya303
Copy link
Contributor

I discussed this with Peter and moving forward I'll be taking over the thread safety work and continue it.

@kumaraditya303
Copy link
Contributor

I investigated the thread safety of ctypes and here's the (incomplete) list of issues:

@colesbury
Copy link
Contributor

I think there are also races on StgInfo.flags, in particular with the DICTFLAG_FINAL bit.

info->flags &= ~DICTFLAG_FINAL; /* clear the 'final' flag in the subclass info */
baseinfo->flags |= DICTFLAG_FINAL; /* set the 'final' flag in the baseclass info */

@kumaraditya303
Copy link
Contributor

I think there are also races on StgInfo.flags, in particular with the DICTFLAG_FINAL bit.

Yes, I am covering that under "the initialization and concurrent modifications of stginfo is not thread safe".

@colesbury
Copy link
Contributor

This is different and more important than concurrent modification or initialization of stginfo, and I think it should be one of the early things we should focus on. Concurrent modifications of ctypes Structures is not an important use case.

However, creating separate instances of the same structure is an important use case:

from ctypes import Structure, c_int
from concurrent.futures import ThreadPoolExecutor

class POINT(Structure):
    _fields_ = [("x", c_int), ("y", c_int)]

with ThreadPoolExecutor() as executor:
    futures = [executor.submit(POINT, i, i) for i in range(1000)]
    results = [future.result() for future in futures]

This needs to be both efficient and thread-safe.

kumaraditya303 added a commit that referenced this issue Mar 25, 2025
The freelist is not thread safe in free-threading so this adds lock around it make it thread safe in free-threading.
kumaraditya303 added a commit that referenced this issue Mar 25, 2025
This fixes thread safety of `array_cache` and `swapped_suffix` by initializing them in module exec to make it thread safety.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir topic-ctypes topic-free-threading type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants