Skip to content
This repository was archived by the owner on Feb 4, 2020. It is now read-only.

Commit 650cf8f

Browse files
author
Hubert Hesse
committed
Fix race condition between CreateMutex and WaitForSingleObject
- Create handles to mutexes shortly before usage
1 parent 76332b3 commit 650cf8f

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

clcache.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,14 @@ class CacheLock(object):
258258

259259
def __init__(self, mutexName, timeoutMs):
260260
self._mutexName = 'Local\\' + mutexName
261+
self._mutex = None
262+
self._timeoutMs = timeoutMs
263+
264+
def createMutex(self):
261265
self._mutex = windll.kernel32.CreateMutexW(
262-
wintypes.INT(0),
263-
wintypes.INT(0),
266+
None,
267+
wintypes.BOOL(False),
264268
self._mutexName)
265-
self._timeoutMs = timeoutMs
266269
assert self._mutex
267270

268271
def __enter__(self):
@@ -272,9 +275,12 @@ def __exit__(self, typ, value, traceback):
272275
self.release()
273276

274277
def __del__(self):
275-
windll.kernel32.CloseHandle(self._mutex)
278+
if self._mutex:
279+
windll.kernel32.CloseHandle(self._mutex)
276280

277281
def acquire(self):
282+
if not self._mutex:
283+
self.createMutex()
278284
result = windll.kernel32.WaitForSingleObject(
279285
self._mutex, wintypes.INT(self._timeoutMs))
280286
if result not in [0, self.WAIT_ABANDONED_CODE]:

0 commit comments

Comments
 (0)