Skip to content

Commit 67129c3

Browse files
[3.11] gh-109888: Fix test_os _kill_with_event() on Windows (GH-110421) (#110443)
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 4134036 commit 67129c3

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
@@ -2505,30 +2505,34 @@ def _kill_with_event(self, event, name):
25052505
tagname = "test_os_%s" % uuid.uuid1()
25062506
m = mmap.mmap(-1, 1, tagname)
25072507
m[0] = 0
2508+
25082509
# Run a script which has console control handling enabled.
2509-
proc = subprocess.Popen([sys.executable,
2510-
os.path.join(os.path.dirname(__file__),
2511-
"win_console_handler.py"), tagname],
2512-
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
2513-
# Let the interpreter startup before we send signals. See #3137.
2514-
count, max = 0, 100
2515-
while count < max and proc.poll() is None:
2516-
if m[0] == 1:
2517-
break
2518-
time.sleep(0.1)
2519-
count += 1
2520-
else:
2521-
# Forcefully kill the process if we weren't able to signal it.
2522-
os.kill(proc.pid, signal.SIGINT)
2523-
self.fail("Subprocess didn't finish initialization")
2524-
os.kill(proc.pid, event)
2525-
# proc.send_signal(event) could also be done here.
2526-
# Allow time for the signal to be passed and the process to exit.
2527-
time.sleep(0.5)
2528-
if not proc.poll():
2529-
# Forcefully kill the process if we weren't able to signal it.
2530-
os.kill(proc.pid, signal.SIGINT)
2531-
self.fail("subprocess did not stop on {}".format(name))
2510+
script = os.path.join(os.path.dirname(__file__),
2511+
"win_console_handler.py")
2512+
cmd = [sys.executable, script, tagname]
2513+
proc = subprocess.Popen(cmd,
2514+
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
2515+
2516+
with proc:
2517+
# Let the interpreter startup before we send signals. See #3137.
2518+
for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
2519+
if proc.poll() is None:
2520+
break
2521+
else:
2522+
# Forcefully kill the process if we weren't able to signal it.
2523+
proc.kill()
2524+
self.fail("Subprocess didn't finish initialization")
2525+
2526+
os.kill(proc.pid, event)
2527+
2528+
try:
2529+
# proc.send_signal(event) could also be done here.
2530+
# Allow time for the signal to be passed and the process to exit.
2531+
proc.wait(timeout=support.SHORT_TIMEOUT)
2532+
except subprocess.TimeoutExpired:
2533+
# Forcefully kill the process if we weren't able to signal it.
2534+
proc.kill()
2535+
self.fail("subprocess did not stop on {}".format(name))
25322536

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

0 commit comments

Comments
 (0)