Skip to content

Simplify GCDisabledGuard constructor - #529

Merged
jamadden merged 1 commit into
python-greenlet:masterfrom
Georggi:patch-1
Sep 14, 2026
Merged

jamadden merged 1 commit into
python-greenlet:masterfrom
Georggi:patch-1

Conversation

@Georggi

@Georggi Georggi commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

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.
@jamadden
jamadden merged commit 8de830f into python-greenlet:master Sep 14, 2026
39 checks passed
@jamadden

Copy link
Copy Markdown
Contributor

Thanks!

jamadden added a commit that referenced this pull request Sep 14, 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.

2 participants