Skip to content

Fix free-threading crash from untraversed deferred references in suspended greenlets#511

Merged
jamadden merged 13 commits into
python-greenlet:masterfrom
kumaraditya303:ft-fix
Jun 16, 2026
Merged

Fix free-threading crash from untraversed deferred references in suspended greenlets#511
jamadden merged 13 commits into
python-greenlet:masterfrom
kumaraditya303:ft-fix

Conversation

@kumaraditya303

Copy link
Copy Markdown
Contributor

Hi — I'm a CPython core developer working on free-threading and maintainer of asyncio.

This PR fixes the crash on free-threading caused by deference reference counted objects not traversed by greenlet during GC traversal.

In free-threading, some of the refs to objects held by the interpreter uses a special reference type called "deferred reference". Such references are used on some objects like functions and classes to reduce reference contention on the objects and they must be traversed by the free-threaded GC so that it doesn't free these objects prematurely.
Greenlet currently crashes on free-threading because greenlets hold suspended frames which on free-threading can contain objects with deferred reference count. The suspended frames needs to be GC traversed so that the GC doesn't free objects early causing a crash at runtime exit.

In 3.15+, I exposed _PyGC_VisitStackRef and _PyGC_VisitFrameStack in python/cpython#150767 which is used in this PR so that suspended frames of greenlets can be traversed by the GC. This fixes the crash and as a side effect also fixes the "A Cycle Of Greenlets Is A Leak" because suspended frames are now traversed so they can now be collected by the GC instead of being leaked. I have added tests on 3.15+ to ensure that suspended frames are gc collected and finalizers of locals are triggered properly.

@kumaraditya303 kumaraditya303 changed the title fix crash on free-threading because of deferred reference counting Fix free-threading crash from untraversed deferred references in suspended greenlets Jun 4, 2026
@kumaraditya303

Copy link
Copy Markdown
Contributor Author

ping @jamadden for review. I will create a followup PR to fix some of the known memory leaks of greenlet but that depends on this PR.

@jamadden

Copy link
Copy Markdown
Contributor

Thanks for this! Today is the first time I've had a chance to look at it, and it LGTM. I can confirm it fixes the crashes on 3.15b2, both official from python.org and built locally (from MacPorts). I had a moment of panic due to a runtime linking error, but it turns out the needed symbol was only added in 3.15b2 and I was using 3.15b1.

I'm happy to merge this and add some appropriate notes to CHANGES.rst.

@jamadden jamadden merged commit f5a728e into python-greenlet:master Jun 16, 2026
39 checks passed
jamadden added a commit that referenced this pull request Jun 16, 2026
@kumaraditya303 kumaraditya303 deleted the ft-fix branch June 16, 2026 19:07
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