gh-153946: Add API to defer automatic garbage collection#154188
gh-153946: Add API to defer automatic garbage collection#154188pablogsal wants to merge 3 commits into
Conversation
|
I ran this a few times on release builds pinned to one CPU, comparing
I also ran I tried scheduling a collection directly from |
| // Close the race with a deferral that started before the world stopped. | ||
| if (state->reason == _Py_GC_REASON_HEAP && | ||
| _Py_atomic_load_int( | ||
| &state->gcstate->automatic_collection_pause_count)) { | ||
| _PyEval_StartTheWorld(interp); | ||
| return false; | ||
| } |
There was a problem hiding this comment.
Do we need a similar check before this?
Lines 1486 to 1497 in fe0cc2b
This is a very shy question, so please treat it with a huge grain of salt. My hunch is that GC callbacks may still cause race, since they're just regular Python code and may release GIL:
Lines 1467 to 1472 in fe0cc2b
I interrogated Claude Opus to show me what is the actual place for invoking and returning from the callbacks, and that's how I arrived at 1486.
| struct _gc_runtime_state { | ||
| /* Is automatic collection enabled? */ | ||
| int enabled; | ||
| int automatic_collection_pause_count; |
There was a problem hiding this comment.
@pablogsal Another odd question: what about a fork() in a free-threading build?
Let's say that there are two threads. There's a fork(), and the children inherit automatic_collection_pause_count. Since these are separate processes, there's no way to recover, unless there's a clean up after fork? (I would have to think, but I'm not sold on naive zeroing, similar to reset_asyncio_state or reset_remotedebug_data)
Truth be told, this is likely a problem with collecting too...
If that's the case, I can take a stab.
cc @nascheme
No description provided.