mirror of
https://github.com/python/cpython.git
synced 2025-11-24 20:30:18 +00:00
gh-136003: Skip non-daemon threads when exceptions occur during finalization (GH-139129)
During finalization, we need to mark all non-daemon threads as daemon to quickly shut down threads when sending CTRL^C to the process. This was a minor regression from GH-136004.
This commit is contained in:
parent
293b05c09b
commit
3eec897752
3 changed files with 51 additions and 6 deletions
|
|
@ -6,7 +6,7 @@ import test.support
|
||||||
from test.support import threading_helper, requires_subprocess, requires_gil_enabled
|
from test.support import threading_helper, requires_subprocess, requires_gil_enabled
|
||||||
from test.support import verbose, cpython_only, os_helper
|
from test.support import verbose, cpython_only, os_helper
|
||||||
from test.support.import_helper import ensure_lazy_imports, import_module
|
from test.support.import_helper import ensure_lazy_imports, import_module
|
||||||
from test.support.script_helper import assert_python_ok, assert_python_failure
|
from test.support.script_helper import assert_python_ok, assert_python_failure, spawn_python
|
||||||
from test.support import force_not_colorized
|
from test.support import force_not_colorized
|
||||||
|
|
||||||
import random
|
import random
|
||||||
|
|
@ -2083,6 +2083,32 @@ class ThreadingExceptionTests(BaseTestCase):
|
||||||
self.assertEqual(out, b"")
|
self.assertEqual(out, b"")
|
||||||
self.assertEqual(err, b"")
|
self.assertEqual(err, b"")
|
||||||
|
|
||||||
|
@requires_subprocess()
|
||||||
|
@unittest.skipIf(os.name == 'nt', "signals don't work well on windows")
|
||||||
|
def test_keyboard_interrupt_during_threading_shutdown(self):
|
||||||
|
import subprocess
|
||||||
|
source = f"""
|
||||||
|
from threading import Thread
|
||||||
|
import time
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def test():
|
||||||
|
print('a', flush=True, end='')
|
||||||
|
time.sleep(10)
|
||||||
|
|
||||||
|
|
||||||
|
for _ in range(3):
|
||||||
|
Thread(target=test).start()
|
||||||
|
"""
|
||||||
|
|
||||||
|
with spawn_python("-c", source, stderr=subprocess.PIPE) as proc:
|
||||||
|
self.assertEqual(proc.stdout.read(3), b'aaa')
|
||||||
|
proc.send_signal(signal.SIGINT)
|
||||||
|
proc.stderr.flush()
|
||||||
|
error = proc.stderr.read()
|
||||||
|
self.assertIn(b"KeyboardInterrupt", error)
|
||||||
|
|
||||||
|
|
||||||
class ThreadRunFail(threading.Thread):
|
class ThreadRunFail(threading.Thread):
|
||||||
def run(self):
|
def run(self):
|
||||||
|
|
|
||||||
|
|
@ -2429,10 +2429,8 @@ thread_shutdown(PyObject *self, PyObject *args)
|
||||||
// Wait for the thread to finish. If we're interrupted, such
|
// Wait for the thread to finish. If we're interrupted, such
|
||||||
// as by a ctrl-c we print the error and exit early.
|
// as by a ctrl-c we print the error and exit early.
|
||||||
if (ThreadHandle_join(handle, -1) < 0) {
|
if (ThreadHandle_join(handle, -1) < 0) {
|
||||||
PyErr_FormatUnraisable("Exception ignored while joining a thread "
|
|
||||||
"in _thread._shutdown()");
|
|
||||||
ThreadHandle_decref(handle);
|
ThreadHandle_decref(handle);
|
||||||
Py_RETURN_NONE;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ThreadHandle_decref(handle);
|
ThreadHandle_decref(handle);
|
||||||
|
|
|
||||||
|
|
@ -3548,6 +3548,27 @@ Py_ExitStatusException(PyStatus status)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
handle_thread_shutdown_exception(PyThreadState *tstate)
|
||||||
|
{
|
||||||
|
assert(tstate != NULL);
|
||||||
|
assert(_PyErr_Occurred(tstate));
|
||||||
|
PyInterpreterState *interp = tstate->interp;
|
||||||
|
assert(interp->threads.head != NULL);
|
||||||
|
_PyEval_StopTheWorld(interp);
|
||||||
|
|
||||||
|
// We don't have to worry about locking this because the
|
||||||
|
// world is stopped.
|
||||||
|
_Py_FOR_EACH_TSTATE_UNLOCKED(interp, tstate) {
|
||||||
|
if (tstate->_whence == _PyThreadState_WHENCE_THREADING) {
|
||||||
|
tstate->_whence = _PyThreadState_WHENCE_THREADING_DAEMON;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_PyEval_StartTheWorld(interp);
|
||||||
|
PyErr_FormatUnraisable("Exception ignored on threading shutdown");
|
||||||
|
}
|
||||||
|
|
||||||
/* Wait until threading._shutdown completes, provided
|
/* Wait until threading._shutdown completes, provided
|
||||||
the threading module was imported in the first place.
|
the threading module was imported in the first place.
|
||||||
The shutdown routine will wait until all non-daemon
|
The shutdown routine will wait until all non-daemon
|
||||||
|
|
@ -3559,14 +3580,14 @@ wait_for_thread_shutdown(PyThreadState *tstate)
|
||||||
PyObject *threading = PyImport_GetModule(&_Py_ID(threading));
|
PyObject *threading = PyImport_GetModule(&_Py_ID(threading));
|
||||||
if (threading == NULL) {
|
if (threading == NULL) {
|
||||||
if (_PyErr_Occurred(tstate)) {
|
if (_PyErr_Occurred(tstate)) {
|
||||||
PyErr_FormatUnraisable("Exception ignored on threading shutdown");
|
handle_thread_shutdown_exception(tstate);
|
||||||
}
|
}
|
||||||
/* else: threading not imported */
|
/* else: threading not imported */
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
result = PyObject_CallMethodNoArgs(threading, &_Py_ID(_shutdown));
|
result = PyObject_CallMethodNoArgs(threading, &_Py_ID(_shutdown));
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
PyErr_FormatUnraisable("Exception ignored on threading shutdown");
|
handle_thread_shutdown_exception(tstate);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Py_DECREF(result);
|
Py_DECREF(result);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue