Skip to content

Commit 6af359b

Browse files
[3.12] gh-109888: Fix test_os _kill_with_event() on Windows (GH-110421) (#110442)
gh-109888: Fix test_os _kill_with_event() on Windows (GH-110421) Replace os.kill() with proc.kill() which catchs PermissionError. Rewrite _kill_with_event(): * Use subprocess context manager ("with proc:"). * Use sleeping_retry() to wait until the child process is ready. * Replace SIGINT with proc.kill() on error. * Replace 10 seconds with SHORT_TIMEOUT to wait until the process is ready. * Replace 0.5 seconds with SHORT_TIMEOUT to wait for the process exit. (cherry picked from commit aaf297c) Co-authored-by: Victor Stinner <[email protected]>
1 parent 1b58985 commit 6af359b

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

Lib/test/test_os.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,30 +2559,34 @@ def _kill_with_event(self, event, name):
25592559
tagname = "test_os_%s" % uuid.uuid1()
25602560
m = mmap.mmap(-1, 1, tagname)
25612561
m[0] = 0
2562+
25622563
# Run a script which has console control handling enabled.
2563-
proc = subprocess.Popen([sys.executable,
2564-
os.path.join(os.path.dirname(__file__),
2565-
"win_console_handler.py"), tagname],
2566-
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
2567-
# Let the interpreter startup before we send signals. See #3137.
2568-
count, max = 0, 100
2569-
while count < max and proc.poll() is None:
2570-
if m[0] == 1:
2571-
break
2572-
time.sleep(0.1)
2573-
count += 1
2574-
else:
2575-
# Forcefully kill the process if we weren't able to signal it.
2576-
os.kill(proc.pid, signal.SIGINT)
2577-
self.fail("Subprocess didn't finish initialization")
2578-
os.kill(proc.pid, event)
2579-
# proc.send_signal(event) could also be done here.
2580-
# Allow time for the signal to be passed and the process to exit.
2581-
time.sleep(0.5)
2582-
if not proc.poll():
2583-
# Forcefully kill the process if we weren't able to signal it.
2584-
os.kill(proc.pid, signal.SIGINT)
2585-
self.fail("subprocess did not stop on {}".format(name))
2564+
script = os.path.join(os.path.dirname(__file__),
2565+
"win_console_handler.py")
2566+
cmd = [sys.executable, script, tagname]
2567+
proc = subprocess.Popen(cmd,
2568+
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
2569+
2570+
with proc:
2571+
# Let the interpreter startup before we send signals. See #3137.
2572+
for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
2573+
if proc.poll() is None:
2574+
break
2575+
else:
2576+
# Forcefully kill the process if we weren't able to signal it.
2577+
proc.kill()
2578+
self.fail("Subprocess didn't finish initialization")
2579+
2580+
os.kill(proc.pid, event)
2581+
2582+
try:
2583+
# proc.send_signal(event) could also be done here.
2584+
# Allow time for the signal to be passed and the process to exit.
2585+
proc.wait(timeout=support.SHORT_TIMEOUT)
2586+
except subprocess.TimeoutExpired:
2587+
# Forcefully kill the process if we weren't able to signal it.
2588+
proc.kill()
2589+
self.fail("subprocess did not stop on {}".format(name))
25862590

25872591
@unittest.skip("subprocesses aren't inheriting Ctrl+C property")
25882592
@support.requires_subprocess()

0 commit comments

Comments
 (0)