Skip to content

Commit

Permalink
[3.7] bpo-39847: EnterNonRecursiveMutex() uses GetTickCount64() (pyth…
Browse files Browse the repository at this point in the history
…onGH-18780) (pythonGH-18959)

The 32-bit (49-day) TickCount relied on in EnterNonRecursiveMutex can overflow
in the gap between the 'target' time and the 'now' time WaitForSingleObjectEx
returns, causing the loop to think it needs to wait another 49 days. This is
most likely to happen when the machine is hibernated during
WaitForSingleObjectEx.

This makes acquiring a lock/event/etc from the _thread or threading module
appear to never timeout.

Replace with GetTickCount64 - this is OK now Python no longer supports XP which
lacks it, and is in use for time.monotonic().

Co-authored-by: And Clover <[email protected]>
(cherry picked from commit 64838ce)

Co-authored-by: bobince <[email protected]>
  • Loading branch information
bobince authored Mar 12, 2020
1 parent 6a12676 commit feaf0c3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Avoid hang when computer is hibernated whilst waiting for a mutex (for
lock-related objects from :mod:`threading`) around 49-day uptime.
6 changes: 3 additions & 3 deletions Python/thread_nt.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,16 @@ EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds)
}
} else if (milliseconds != 0) {
/* wait at least until the target */
DWORD now, target = GetTickCount() + milliseconds;
ULONGLONG now, target = GetTickCount64() + milliseconds;
while (mutex->locked) {
if (PyCOND_TIMEDWAIT(&mutex->cv, &mutex->cs, (long long)milliseconds*1000) < 0) {
result = WAIT_FAILED;
break;
}
now = GetTickCount();
now = GetTickCount64();
if (target <= now)
break;
milliseconds = target-now;
milliseconds = (DWORD)(target-now);
}
}
if (!mutex->locked) {
Expand Down

0 comments on commit feaf0c3

Please sign in to comment.