Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 34 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import textwrap
import traceback
import warnings
import ctypes

from unittest import mock
from test import lock_tests
Expand Down Expand Up @@ -412,6 +413,39 @@ def run(self):
t.join()
# else the thread is still running, and we have no way to kill it

@cpython_only
@unittest.skipIf(not hasattr(signal, "pthread_kill"), "requires pthread_kill (Unix only)")
def test_PyThreadState_SetAsyncExc_interrupts_sleep(self):
set_async_exc = ctypes.pythonapi.PyThreadState_SetAsyncExc
set_async_exc.argtypes = (ctypes.c_ulong, ctypes.py_object)

class AsyncExc(Exception):
pass

exception = ctypes.py_object(AsyncExc)
signal.signal(signal.SIGUSR1, lambda *_: None)
worker_started = threading.Event()
worker_finished = threading.Event()

def worker():
tid = threading.get_ident()
worker_started.set()
try:
time.sleep(10) # blocked in EINTR retry path
except AsyncExc:
worker_finished.set()

t = threading.Thread(target=worker)
t.start()
worker_started.wait()
time.sleep(0.2)
result = set_async_exc(t.ident, exception)
self.assertEqual(result, 1)
signal.pthread_kill(t.ident, signal.SIGUSR1)
worker_finished.wait(timeout=3)
self.assertTrue(worker_finished.is_set())
t.join(timeout=3)

def test_limbo_cleanup(self):
# Issue 7481: Failure to start thread should cleanup the limbo map.
def fail_new_thread(*args, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix PyThreadState_SetAsyncExc not interrupting threads blocked in EINTR
retry paths such as time.sleep().
9 changes: 9 additions & 0 deletions Python/ceval_gil.c
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,15 @@ _PyEval_MakePendingCalls(PyThreadState *tstate)
return res;
}

/* Check for async exceptions (PyThreadState_SetAsyncExc) */
if (tstate->async_exc != NULL) {
PyObject *exc = tstate->async_exc;
tstate->async_exc = NULL;
PyErr_SetNone(exc);
Py_DECREF(exc);
return -1;
}

return 0;
}

Expand Down
Loading