Skip to content

Commit c15a45a

Browse files
committed
pythongh-126434: Document 64 bit example of how many times reentrant set() call must be made to reach race condition possibility.
1 parent bec9070 commit c15a45a

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

‎Lib/multiprocessing/synchronize.py‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -343,26 +343,28 @@ def is_set(self):
343343
return self._flag.value == 1
344344

345345
def set(self):
346-
# If `set` is called from a signal handler, this is fine as the lock is recursive (i.e. it won't deadlock).
346+
# If set() is called from a signal handler, this is fine as the lock is recursive (i.e. it won't deadlock).
347347
# If the thread interrupted by the signal handler is wait()-ing and the signal handler calls set(),
348348
# this is fine as wait() spins on the value. Thus, after the signal handler is done, the thread will
349349
# return from wait()
350350
# Fixes https://github.com/python/cpython/issues/85772
351351
with self._flag:
352352
with self._set_id:
353353
if self._flag.value == 0:
354-
# There is a theoretical chance of race here. It requires the following conditions:
354+
# There is a theoretical chance of a race here. It requires the following conditions:
355355
# The interrupted thread must be wait()ing.
356-
# Then set must be called reentrant for the maximum value of c_ulonglong times,
357-
# and all interruptions must happen exactly after `if self._flag.value == 0:`.
356+
# Then set() must be called reentrant for the maximum value of c_ulonglong + 1 times, i.e.
357+
# ./python.exe -c 'from ctypes import *; print(256**sizeof(c_ulonglong()))', which
358+
# evaluates to 18446744073709551616 on a 64 bit machine.
359+
# All interruptions must happen exactly after `if self._flag.value == 0:`.
358360
# The _set_id value will then wrap around. Then clear() must be called
359361
# before the original wait() code continues. The wait() code will then continue
360-
# to (incorrectly) wait. I think this case is safe to ignore. The stack
361-
# will grow too large before there is any chance of this actually happening.
362+
# to (incorrectly) wait. This case should be safe to ignore. The stack
363+
# will probably grow too large before there is any chance of this actually happening.
362364

363365
self._flag.value = 1
364366
self._set_id.value += 1
365-
# There is no race here by reentrant set when reaching the maximum value for `self._set_id.value`.
367+
# There is no race here by reentrant set() when reaching the maximum value for `self._set_id.value`.
366368
# ctypes.c_ulonglong will overflow without any exception:
367369
# https://docs.python.org/3/library/ctypes.html#ctypes.c_ulonglong
368370
# > no overflow checking is done.

0 commit comments

Comments
 (0)