Skip to content

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

Open
kumaraditya303 wants to merge 13 commits into
python-greenlet:masterfrom
kumaraditya303:ft-fix
Open

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

Conversation

@kumaraditya303
Copy link
Copy Markdown

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
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.

1 participant