Skip to content

Simplify GCDisabledGuard constructor - #529

Open
Georggi wants to merge 1 commit into
python-greenlet:masterfrom
Georggi:patch-1
Open

Simplify GCDisabledGuard constructor#529
Georggi wants to merge 1 commit into
python-greenlet:masterfrom
Georggi:patch-1

Conversation

@Georggi

@Georggi Georggi commented Aug 19, 2026

Copy link
Copy Markdown

Combine PyGC_IsEnabled and PyGC_Disable into PyGC_Disable as it already returns previous state of garbage collector.

A little bit of background: I was building an app using free-threaded python and sqlalchemy with asyncpg - 4-8 threads each with connection to postgres. What I found is very slow (although at this early stage data volume was also low) memory leak of ~100MB/day.
After checking it further with Claude - found by injecting that live app had disabled GC. Further check of what could be disabling GC led to this part of code in greenlet, due to race condition of 2 separate threads accessing GC state at a different time. I've build a patched version with this very change and now the leak is gone.

Can't say that there is no other problem in here with free-threading, but at the very least, this should make it less likely.

Combine PyGC_IsEnabled and PyGC_Disable into PyGC_Disable as it already returns previous state of garbage collector.
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