[Python-checkins] [3.7] bpo-39847: EnterNonRecursiveMutex() uses GetTickCount64() (GH-18780) (GH-18959)

bobince webhook-mailer at python.org
Thu Mar 12 10:28:39 EDT 2020


https://github.com/python/cpython/commit/feaf0c37891dfe8f0f3e643c3711af3af23bf805
commit: feaf0c37891dfe8f0f3e643c3711af3af23bf805
branch: 3.7
author: bobince <and+github at doxdesk.com>
committer: GitHub <noreply at github.com>
date: 2020-03-12T07:28:31-07:00
summary:

[3.7] bpo-39847: EnterNonRecursiveMutex() uses GetTickCount64() (GH-18780) (GH-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 <and.clover at bromium.com>
(cherry picked from commit 64838ce)

Co-authored-by: bobince <and+github at doxdesk.com>

files:
A Misc/NEWS.d/next/Windows/2020-03-04-17-05-11.bpo-39847.C3N2m3.rst
M Python/thread_nt.h

diff --git a/Misc/NEWS.d/next/Windows/2020-03-04-17-05-11.bpo-39847.C3N2m3.rst b/Misc/NEWS.d/next/Windows/2020-03-04-17-05-11.bpo-39847.C3N2m3.rst
new file mode 100644
index 0000000000000..acfbce53eb399
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2020-03-04-17-05-11.bpo-39847.C3N2m3.rst
@@ -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.
diff --git a/Python/thread_nt.h b/Python/thread_nt.h
index 56295d0e8c0f4..85e1f9d402ac5 100644
--- a/Python/thread_nt.h
+++ b/Python/thread_nt.h
@@ -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) {



More information about the Python-checkins mailing list