Skip to content

Commit 91d94d9

Browse files
committed
Fix daemon thread shutdown.
1 parent b14aa73 commit 91d94d9

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

Lib/test/test_interpreters/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ def test_remaining_daemon_threads(self):
691691
import time
692692
693693
def task():
694-
time.sleep(100)
694+
time.sleep(3)
695695
696696
threads = [threading.Thread(target=task, daemon=True) for _ in range(3)]
697697
for t in threads:

Python/pylifecycle.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,14 +2449,17 @@ Py_EndInterpreter(PyThreadState *tstate)
24492449
_Py_FinishPendingCalls(tstate);
24502450

24512451
_PyAtExit_Call(tstate->interp);
2452-
_PyRuntimeState *runtime = interp->runtime;
2453-
_PyEval_StopTheWorldAll(runtime);
2454-
PyThreadState *list = _PyThreadState_RemoveExcept(tstate);
2455-
2452+
_PyEval_StopTheWorld(interp);
24562453
/* Remaining daemon threads will automatically exit
24572454
when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
24582455
_PyInterpreterState_SetFinalizing(interp, tstate);
2459-
_PyEval_StartTheWorldAll(runtime);
2456+
2457+
PyThreadState *list = _PyThreadState_RemoveExcept(tstate);
2458+
for (PyThreadState *p = list; p != NULL; p = p->next) {
2459+
_PyThreadState_SetShuttingDown(p);
2460+
}
2461+
2462+
_PyEval_StartTheWorld(interp);
24602463
_PyThreadState_DeleteList(list, /*is_after_fork=*/0);
24612464

24622465
// XXX Call something like _PyImport_Disable() here?

0 commit comments

Comments
 (0)