forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pythongh-126434: Detect when .set() is called by a thread already .wa…
…it()-ing. Raise an exception if that is the case. Fix race condition in multiprocessing.Event.wait() as described in python#95826
- Loading branch information
Showing
5 changed files
with
106 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import multiprocessing | ||
import sys | ||
|
||
|
||
# Reproduction code copied and modified from https://github.com/python/cpython/issues/95826 | ||
class SimpleRepro: | ||
def __init__(self): | ||
self.heartbeat_event = multiprocessing.Event() | ||
self.shutdown_event = multiprocessing.Event() | ||
self.child_proc = multiprocessing.Process(target=self.child_process, daemon=True) | ||
self.child_proc.start() | ||
|
||
def child_process(self): | ||
while True: | ||
if self.shutdown_event.is_set(): | ||
return | ||
self.heartbeat_event.set() | ||
self.heartbeat_event.clear() | ||
|
||
def test_heartbeat(self): | ||
exit_code = 0 | ||
for i in range(2000): | ||
success = self.heartbeat_event.wait(100) | ||
if not success: | ||
exit_code = 1 | ||
break | ||
self.shutdown_event.set() | ||
self.child_proc.join() | ||
sys.exit(exit_code) | ||
|
||
|
||
if __name__ == '__main__': | ||
foo = SimpleRepro() | ||
foo.test_heartbeat() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import multiprocessing | ||
import signal | ||
import concurrent.futures | ||
import time | ||
import sys | ||
import os | ||
|
||
|
||
def send_sigint(pid): | ||
time.sleep(1) | ||
os.kill(pid, signal.SIGINT) | ||
|
||
|
||
def run_signal_handler_wait_set_test(): | ||
shutdown_event = multiprocessing.Event() | ||
|
||
def sigterm_handler(_signo, _stack_frame): | ||
shutdown_event.set() | ||
|
||
signal.signal(signal.SIGINT, sigterm_handler) | ||
|
||
with concurrent.futures.ProcessPoolExecutor() as executor: | ||
f = executor.submit(send_sigint, os.getpid()) | ||
shutdown_event.wait() | ||
f.result() | ||
|
||
|
||
if __name__ == '__main__': | ||
try: | ||
run_signal_handler_wait_set_test() | ||
sys.exit(1) | ||
except AssertionError as e: | ||
assert 'multiprocessing.Event.set() cannot be called from a thread that is already wait()-ing' in str(e) | ||
sys.exit(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters