8000 gh-109888: Fix test_os _kill_with_event() on Windows (#110421) · python/cpython@aaf297c · GitHub
[go: up one dir, main page]

Skip to content

Commit aaf297c

Browse files
authored
gh-109888: Fix test_os _kill_with_event() on Windows (#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.
1 parent fb6c4ed commit aaf297c

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
@@ -2566,30 +2566,34 @@ def _kill_with_event(self, event, name):
25662566
tagname = "test_os_%s" % uuid.uuid1()
25672567
m = mmap.mmap(-1, 1, tagname)
25682568
m[0] = 0
2569+
25692570
# Run a script which has console control handling enabled.
2570-
proc = subprocess.Popen([sys.executable,
2571-
os.path.join(os.path.dirname(__file__),
2572-
"win_console_handler.py"), tagname],
2573-
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
2574-
# Let the interpreter startup before we send signals. See #3137.
2575-
count, max = 0, 100
2576-
while count < max and proc.poll() is None:
2577-
if m[0] == 1:
2578-
break
2579-
time.sleep(0.1)
2580-
count += 1
2581-
else:
2582-
# Forcefully kill the process if we weren't able to signal it.
2583-
os.kill(proc.pid, signal.SIGINT)
2584-
self.fail("Subprocess didn't finish initialization")
2585-
os.kill(proc.pid, event)
2586-
# proc.send_signal(event) could also be done here.
2587-
# Allow time for the signal to be passed and the process to exit.
2588-
time.sleep(0.5)
2589-
if not proc.poll():
2590-
# Forcefully kill the process if we weren't able to signal it.
2591-
os.kill(proc.pid, signal.SIGINT)
2592-
self.fail("subprocess did not stop on {}".format(name))
2571+
script = os.path.join(os.path.dirname(__file__),
2572+
"win_console_handler.py")
2573+
cmd = [sys.executable, script, tagname]
2574+
proc = subprocess.Popen(cmd,
2575+
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
2576+
2577+
with proc:
2578+
# Let the interpreter startup before we send signals. See #3137.
2579+
for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
2580+
if proc.poll() is None:
2581+
break
2582+
else:
2583+
# Forcefully kill the process if we weren't able to signal it.
2584+
proc.kill()
2585+
self.fail("Subprocess didn't finish initialization")
2586+
2587+
os.kill(proc.pid, event)
2588+
2589+
try:
2590+
# proc.send_signal(event) could also be done here.
2591+
# Allow time for the signal to be passed and the process to exit.
2592+
proc.wait(timeout=support.SHORT_TIMEOUT)
2593+
except subprocess.TimeoutExpired:
2594+
# Forcefully kill the process if we weren't able to signal it.
2595+
proc.kill()
2596+
self.fail("subprocess did not stop on {}".format(name))
25932597

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

0 commit comments

Comments
 (0)
0