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

Make ffi_init_once thread-safe in free threaded build #143

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

colesbury
Copy link

@colesbury colesbury commented Nov 13, 2024

Lock the FFI object when initializing the init_once_cache dictionary and use PyDict_GetItemRef in places where the dictionary's value may be concurrently modified.

See #126

Lock the FFI object when initializing the init_once_cache dictionary and
use PyDict_GetItemRef in places where the dictionary's value may be
concurrently modified.
@colesbury
Copy link
Author

Hi @nitzmahone - I'm still working through the CFFI free-threading issues, but I figured it'd be helpful to get some feedback on a small change.

On that note:

  • I added a "polyfill" for PyDict_GetItemRef, but that sort of thing is nicely provided. by https://github.com/python/pythoncapi-compat/. Would you be interested in using that?
  • It's probably cleaner and simpler to make init_once_cache have the same lifetime as its FFIObject instead of lazily initializing it. I went with the smaller code change for now, but let me know what you think.

To find thread-safety issues, in addition to reading through the code, I'm using https://github.com/Quansight-Labs/pytest-run-parallel, which I've found to be very effective.

@colesbury
Copy link
Author

Hey @nitzmahone - gentle ping. I'd really like to see python-cffi work in the free threading build as a lot of libraries depend on it. I'm happy to do most of the implementation work.

@arigo
Copy link
Contributor

arigo commented Jan 9, 2025

Hi! Just in case you want an alternative approach, you can make a fork of cffi and implement and test free-threading in it. Ideally you'd advertise it for pip usage too (I think it can be done with a direct github url if you don't want to make a new cffi-xyz package name). Once such a thing exists, is complete, and is reasonably well-tested (either as tests or, better in this case, tested by being in use in various other projects), I'd be up to reviewing a single big pull request. What I won't do is review many small incremental patches---but I'm not speaking for nitzmahone here, only for me.

@colesbury
Copy link
Author

Hi Armin - thanks for your suggestion and I appreciate your offer to review a single, big, well-tested PR.

A forked, free threading pacakge is a bit awkward because pip doesn't support "free threading" as a constraint: a project can't easily depend on a forked version only for the free threading build. I think there are workarounds, but they require more substantial changes to the build process of packages that depend on cffi.

@arigo
Copy link
Contributor

arigo commented Jan 9, 2025

I think for now it would be fine if you get enough people to test various projects after changing the line in requirement.txt to point to the github clone. Note also that you shouldn't expect many changes coming from the official cffi repo (there are very few updates nowadays), so keeping up-to-date should be easy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants