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 — PyThreadState_Swap() During Finalization Causes Immediate Exit (AKA Daemon Threads Are Still the Worst!) · Issue #109793 · python/cpython Closed Labels 3.12only security fixesonly security fixes3.13only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-subinterpreterstype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error DescriptionBug reporttl;dr Switching between interpreters while finalizing causes the main thread to exit. The fix should be simple. We use Thus, if we switch interpreters during finalization then the thread will exit. If we do this in the finalizing (main) thread then the process immediately exits with an exit code of 0. One notable consequence is that a Python process with an unhandled exception will print the traceback like normal but can end up with an exit code of 0 instead of 1 (and some of the runtime finalization code never gets executed). 1 Reproducer$ cat > script.py << EOF import _xxsubinterpreters as _interpreters interpid = _interpreters.create() raise Exception EOF $ ./python script.py Traceback (most recent call last): File ".../check-swapped-exitcode.py", line 3, in <module> raise Exception Exception $ echo $? 0 In this case, "interpid" is a Here's what happens in
At the point the following happens:
Notably, the rest of BackgroundRuntime finalization happens in whichever thread called Other threads may still be running when we start finalization, whether daemon threads or not, and each of those threads has a thread state corresponding to the interpreter that is active in that thread. 4 One of the first things we do during finalization is to wait for all non-daemon threads to finish running. Daemon threads are a different story. They must die! Back in 2011 we identified that daemon threads were interfering with finalization, sometimes causing crashes or making the Python executable hang. 5 At the time, we applied a best-effort solution where we kill the current thread if it isn't the one where However, that solution checked the tstate pointer rather than the thread ID, so swapping interpreters in the finalizing thread was broken, and here we are. History:
Related: gh-87135 (PRs: gh-105805, gh-28525) Linked PRs
Footnotes
Activity
MetadataMetadataAssigneesLabels3.12only security fixesonly security fixes3.13only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-subinterpreterstype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error Projects
MilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |