GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — gh-113964: Restore the ability to start threads from atexit handler functions. by gpshead · Pull Request #116982 · python/cpython Conversation
|
| after allocation. */ | ||
| int _initialized; | ||
| int finalizing; | ||
| int started_finalization; /* set early, used to block some actions. */ |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Can you give some examples here of actions which are blocked?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Depending upon implementation i'm not sure we even want this flag anymore... If we allow threading and forking to happen up until the modern _finalizing API returns False there'd be no need for it.
Otherwise "some actions" needing describing is basically those two and an odd debug thing in unicodeobject.c as far the users of this go. Every place that touches it can be seen in this PR.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| after allocation. */ | ||
| int _initialized; | ||
| int finalizing; | ||
| int started_finalization; /* set early, used to block some actions. */ |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This seemed to be a more accurate name given what it gets used for and the below more comprehensive _finalizing with an API around it.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| # dubious, but some code does it. We can't wait for it to be marked as done | ||
| # normally - that won't happen until the interpreter is nearly dead. So | ||
| # mark it done here. | ||
| if _main_thread._handle.is_done() and _is_main_interpreter(): |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This early return logic started in ee84a60 but no longer seems relevant in 3.13 given the other refactoring in threading.py cleaning up the locking mess.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| # Call registered threading atexit functions before threads are joined. | ||
| # Order is reversed, similar to atexit. | ||
| for atexit_call in reversed(_threading_atexits): | ||
| while _threading_atexits and (atexit_call := _threading_atexits.pop()): |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
_shutdown can be called twice given the re-join after atexit handlers if other threads are found. removing them as they're called prevents future calls. I should turn this into a comment. test_atexit already has tests that cover this situation.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } | ||
|
|
||
| struct atexit_state *state = get_atexit_state(); | ||
| if (_Py_atomic_load_int(&state->handler_calling_started)) { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
arguably all of the logic around blocking new atexit.registers from happening could be left out of this PR. it felt relevant though. we never really defined or documented what happened before (registrations made from a handler were never called). This makes it explicit and gives some indication as to when a registration is being ignored.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| { | ||
| assert(!PyErr_Occurred()); | ||
|
|
||
| if (!from_python_module) { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
because Lib/_test_atexit.py calls atexit._run_callbacks() which shouldn't set the flag.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
There's a lot of duplication between Py_FinalizeEx and PyEnd_Interpreter but this PR is not the place to reconcile any of that.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I think people who start threads from at-exit handlers are asking for trouble, but I would lean towards not waiting on any threads started by at-exit handlers. In Py_FinalizeEx we starting reaping thread states immediately after calling at exit handlers.
ctrl-cs! (Try out https://gist.github.com/colesbury/4093e5675d988ceea10fb7eda4c8c864)I think Py_EndInterpreter needs a bit of work (but like you said, not in this PR). Pressing ctrl-c with something that calls Py_EndInterpreter can lead to Py_FatalError("not the last thread").
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| _PyAtExit_Call(tstate->interp); | ||
|
|
||
| if (tstate != tstate->interp->threads.head || tstate->next != NULL) { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I think if you are accessing tstate->interp->threads.head or tstate->next you need to be within a HEAD_LOCK(&_PyRuntime);.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
If we don't wait on them, that's basically turning them into daemon threads which are running despite us tearing down the interpreter underneath them. That also feels wrong (the entire concept of daemon threads having been a mistake). I'll go back to the user reported issues and try to pick out which actual use case feel supportable. ... Sleeping on this change I think even waiting once isn't enough if we're going to wait again: So long as thread creation is allowed, threads could spawn more threads which might not be in the set There might be another compromise? Don't re-wait if an exception was set after the initial more to ponder. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This PR is stale because it has been open for 30 days with no activity. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.