Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix daemon thread shutdown.
  • Loading branch information
ZeroIntensity committed May 24, 2025
commit 91d94d9ee736795c7cc2d8ab3412be7ba9f8a29b
2 changes: 1 addition & 1 deletion Lib/test/test_interpreters/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ def test_remaining_daemon_threads(self):
import time

def task():
time.sleep(100)
time.sleep(3)

threads = [threading.Thread(target=task, daemon=True) for _ in range(3)]
for t in threads:
Expand Down
13 changes: 8 additions & 5 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2449,14 +2449,17 @@ Py_EndInterpreter(PyThreadState *tstate)
_Py_FinishPendingCalls(tstate);

_PyAtExit_Call(tstate->interp);
_PyRuntimeState *runtime = interp->runtime;
_PyEval_StopTheWorldAll(runtime);
PyThreadState *list = _PyThreadState_RemoveExcept(tstate);

_PyEval_StopTheWorld(interp);
/* Remaining daemon threads will automatically exit
when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
_PyInterpreterState_SetFinalizing(interp, tstate);
_PyEval_StartTheWorldAll(runtime);

PyThreadState *list = _PyThreadState_RemoveExcept(tstate);
for (PyThreadState *p = list; p != NULL; p = p->next) {
_PyThreadState_SetShuttingDown(p);
}

_PyEval_StartTheWorld(interp);
_PyThreadState_DeleteList(list, /*is_after_fork=*/0);

// XXX Call something like _PyImport_Disable() here?
Comment thread
ericsnowcurrently marked this conversation as resolved.
Expand Down
Loading