Uh oh!
There was an error while loading. Please reload this page.
bpo-44032: Defer clearing last thread state until last GC has been run. - #26285
Conversation
markshannon
commented
May 21, 2021
@erlend-aasland Can you confirm that this fixes the issue? |
| /* Last garbage collection on this interpreter */ | ||
| _PyGC_CollectNoFail(tstate); | ||
| PyThreadState_Clear(tstate); |
There was a problem hiding this comment.
For safety, I would prefer to ensure that the tstate belongs to the interpreter if PyInterpreterState_Clear() is called from a different interpreter (even if I don't know if it's currently possible):
| PyThreadState_Clear(tstate); | |
| // Don't clear tstate if it belongs to another interpreter | |
| if (tstate->interp==interp) { | |
| PyThreadState_Clear(tstate); | |
| } |
vstinner
left a comment
There was a problem hiding this comment.
A thread state should remain more or less usable after PyInterpreterState_Clear() is called. I don't think that this PR fix the root issue of https://bugs.python.org/issue44032#msg394104
IMO it would be safer to only release datastack_chunk memory in tstate_delete_common(), rather than in PyThreadState_Clear().
This change remains interesting ;-)
I'll do as soon as I get back on my computer. UPDATE: This PR does not fix the issue. I've used @pablogsal's reproducer from #26274 (comment). Here's an excerpt of the dump (complete dump attached): |
@vstinner I don't see how a thread state can remain usable after the interpreter has been cleared. |
erlend-aasland
commented
May 22, 2021
FYI, freeing datastack chunks in |
| @@ -900,13 +897,6 @@ PyThreadState_Clear(PyThreadState *tstate) | |||
| if (tstate->on_delete != NULL) { | |||
| tstate->on_delete(tstate->on_delete_data); | |||
There was a problem hiding this comment.
Should this also be moved to tstate_delete_common, or is it unrelated to deleting the data stack?
There was a problem hiding this comment.
I have no idea what it does, so I'm not touching it 🙂
There was a problem hiding this comment.
Me neither :) I tried moving it, and it seems to work fine either place. BTW, I found a clue in Include:
cpython/Include/cpython/pystate.h
Lines 134 to 143 in a9e4361
There was a problem hiding this comment.
Also: https://docs.python.org/3/c-api/init.html#c.PyThreadState_Clear:
"Changed in version 3.9: This function now calls the PyThreadState.on_delete callback. Previously, that happened in PyThreadState_Delete()."
There was a problem hiding this comment.
... and here:
cpython/Modules/_threadmodule.c
Lines 1280 to 1329 in a9e4361
bedevere-bot
commented
May 22, 2021
🤖 New build scheduled with the buildbot fleet by @markshannon for commit ddad17e 🤖 If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again. |
vstinner
left a comment
There was a problem hiding this comment.
LGTM.
Calling _PyObject_VirtualFree() in tstate_delete_common() sounds like the good thing to do. I doesn't mean that a tstate must remain usable after calling PyThreadState_Clear(state), it's just about consistency.
vstinner
commented
May 22, 2021
test_asyncio hangs, unrelated error: https://bugs.python.org/issue44112 |
markshannon
commented
May 23, 2021
I'm not worried about asyncio, but test_multiprocessing also fails for one and test_posix for another. There doesn't seem to a coherent model of how threads and interpreters are related, or what memory belongs to the interpreter or thread. |
erlend-aasland
commented
May 23, 2021
Sounds like an issue should be opened for this, if there's not already one on bpo. |
vstinner
commented
May 24, 2021
AMD64 RHEL8 Refleaks PR: I can be a random error which doesn't fail with re-run in verbose mode, we don't know since the job hangs before test_posix can be re-run. |
vstinner
commented
May 24, 2021
I fail to see the test_multiprocessing error. multiprocessing tests are full of race conditions. Some are legit bugs, some are bugs in the tests. I fixed tons of bugs in multiprocessing and in its test suite last years, but if you search for "multiprocessing" in the bug tracker, you will see a long list of issues open for several weeks/months. IMO it's perfectly safe to merge this PR. All issues that you saw are very likely known (not regression of your change). Clearing the current thread state would be another can of worm, but your PR no longer does that ;-) |
markshannon
commented
May 24, 2021
Ok, I'll merge this then. |
vstinner
commented
May 24, 2021
Yeah, it's fine, in the really worst case, we can just revert the change to revisit the options. But again, IMO this function is perfectly safe. |
https://bugs.python.org/issue44032