Skip to content

Fix free-threaded Python 3.14t compilation and thread safety#89

Merged
jensens merged 4 commits intozopefoundation:masterfrom
bluedynamics:fix-freethreaded
Mar 12, 2026
Merged

Fix free-threaded Python 3.14t compilation and thread safety#89
jensens merged 4 commits intozopefoundation:masterfrom
bluedynamics:fix-freethreaded

Conversation

@jensens
Copy link
Member

@jensens jensens commented Feb 16, 2026

  • Replace callable->ob_refcnt with Py_REFCNT(callable) macro
  • Guard im_self mutation optimization with #ifndef Py_GIL_DISABLED (not thread-safe without GIL)
  • Add 3.14t to CI matrix (Linux), tox envlist, and .meta.toml

Fixes #88

Replace direct ob_refcnt struct access with Py_REFCNT() macro.
Guard the im_self mutation optimization with #ifndef Py_GIL_DISABLED
since mutating a shared method object is not thread-safe.
@icemac icemac requested review from davisagli and tseaver February 18, 2026 07:26
Copy link
Member

@davisagli davisagli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The C change LGTM.

Still needs to be updated with zopefoundation/meta#389

@jensens
Copy link
Member Author

jensens commented Mar 9, 2026

The C change LGTM.

Still needs to be updated with zopefoundation/meta#389

updated

@jensens
Copy link
Member Author

jensens commented Mar 11, 2026

updated zope meta again

@jensens jensens enabled auto-merge March 11, 2026 14:24
@jensens jensens requested a review from davisagli March 11, 2026 14:24
Copy link
Member

@dataflake dataflake left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll just add this as a comment: I have zero knowledge of those C bits. But the rest LGTM...

@jensens
Copy link
Member Author

jensens commented Mar 11, 2026

I'll just add this as a comment: I have zero knowledge of those C bits. But the rest LGTM...

Making it work for FT is mostly about using macros/apis for specific GIL related tasks, like reference counting and avoiding to rely on the GIL for thread safety, most chnages are straight forward:

  • Py_REFCNT() / Py_SET_REFCNT() instead of direct ob_refcnt access
  • Py_TYPE() / Py_SET_TYPE() instead of direct ob_type access
  • Py_BEGIN_CRITICAL_SECTION / Py_END_CRITICAL_SECTION to protect mutable state
  • PyMutex for custom locking

Full Info: https://docs.python.org/3.14/howto/free-threading-extensions.html

Copy link
Member

@dataflake dataflake left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to approve this now because there seems no progress at all from the other reviewers.

I looked at the C changes and from eyeballing it those seem to make sense.

@jensens jensens merged commit c38a276 into zopefoundation:master Mar 12, 2026
49 checks passed
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.

Free-threaded Python 3.14t: compilation failure and thread-unsafe optimization

4 participants