diff options
author | Sam Gross <colesbury@gmail.com> | 2024-10-17 14:10:55 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-17 20:10:55 +0200 |
commit | d8c864816121547338efa43c56e3f75ead98a924 (patch) | |
tree | 6930c118c844cd4050a5d1c025974d98f4dbc734 /Python | |
parent | b454662921fd3a1fc27169e91aca03aadea08817 (diff) | |
download | cpython-d8c864816121547338efa43c56e3f75ead98a924.tar.gz cpython-d8c864816121547338efa43c56e3f75ead98a924.zip |
gh-125541: Make Ctrl-C interrupt `threading.Lock.acquire()` on Windows (#125546)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/parking_lot.c | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/Python/parking_lot.c b/Python/parking_lot.c index a7e9760e35d..bffc959e5d0 100644 --- a/Python/parking_lot.c +++ b/Python/parking_lot.c @@ -111,15 +111,28 @@ _PySemaphore_PlatformWait(_PySemaphore *sema, PyTime_t timeout) millis = (DWORD) div; } } - wait = WaitForSingleObjectEx(sema->platform_sem, millis, FALSE); + + // NOTE: we wait on the sigint event even in non-main threads to match the + // behavior of the other platforms. Non-main threads will ignore the + // Py_PARK_INTR result. + HANDLE sigint_event = _PyOS_SigintEvent(); + HANDLE handles[2] = { sema->platform_sem, sigint_event }; + DWORD count = sigint_event != NULL ? 2 : 1; + wait = WaitForMultipleObjects(count, handles, FALSE, millis); if (wait == WAIT_OBJECT_0) { res = Py_PARK_OK; } + else if (wait == WAIT_OBJECT_0 + 1) { + ResetEvent(sigint_event); + res = Py_PARK_INTR; + } else if (wait == WAIT_TIMEOUT) { res = Py_PARK_TIMEOUT; } else { - res = Py_PARK_INTR; + _Py_FatalErrorFormat(__func__, + "unexpected error from semaphore: %u (error: %u)", + wait, GetLastError()); } #elif defined(_Py_USE_SEMAPHORES) int err; |