summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2023-10-05 21:40:49 (GMT)
committerGitHub <noreply@github.com>2023-10-05 21:40:49 (GMT)
commitaaf297c048694cd9652790f8b74e69f7ddadfbde (patch)
treef42eb8c344c7080fdac985389e97357723d2c76d
parentfb6c4ed2bbb2a867d5f0b9a94656e4714be5d9c2 (diff)
downloadcpython-aaf297c048694cd9652790f8b74e69f7ddadfbde.zip
cpython-aaf297c048694cd9652790f8b74e69f7ddadfbde.tar.gz
cpython-aaf297c048694cd9652790f8b74e69f7ddadfbde.tar.bz2
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.
-rw-r--r--Lib/test/test_os.py50
1 files changed, 27 insertions, 23 deletions
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index c1a78a7..669e27c 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -2566,30 +2566,34 @@ class Win32KillTests(unittest.TestCase):
tagname = "test_os_%s" % uuid.uuid1()
m = mmap.mmap(-1, 1, tagname)
m[0] = 0
+
# Run a script which has console control handling enabled.
- proc = subprocess.Popen([sys.executable,
- os.path.join(os.path.dirname(__file__),
- "win_console_handler.py"), tagname],
- creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
- # Let the interpreter startup before we send signals. See #3137.
- count, max = 0, 100
- while count < max and proc.poll() is None:
- if m[0] == 1:
- break
- time.sleep(0.1)
- count += 1
- else:
- # Forcefully kill the process if we weren't able to signal it.
- os.kill(proc.pid, signal.SIGINT)
- self.fail("Subprocess didn't finish initialization")
- os.kill(proc.pid, event)
- # proc.send_signal(event) could also be done here.
- # Allow time for the signal to be passed and the process to exit.
- time.sleep(0.5)
- if not proc.poll():
- # Forcefully kill the process if we weren't able to signal it.
- os.kill(proc.pid, signal.SIGINT)
- self.fail("subprocess did not stop on {}".format(name))
+ script = os.path.join(os.path.dirname(__file__),
+ "win_console_handler.py")
+ cmd = [sys.executable, script, tagname]
+ proc = subprocess.Popen(cmd,
+ creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
+
+ with proc:
+ # Let the interpreter startup before we send signals. See #3137.
+ for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
+ if proc.poll() is None:
+ break
+ else:
+ # Forcefully kill the process if we weren't able to signal it.
+ proc.kill()
+ self.fail("Subprocess didn't finish initialization")
+
+ os.kill(proc.pid, event)
+
+ try:
+ # proc.send_signal(event) could also be done here.
+ # Allow time for the signal to be passed and the process to exit.
+ proc.wait(timeout=support.SHORT_TIMEOUT)
+ except subprocess.TimeoutExpired:
+ # Forcefully kill the process if we weren't able to signal it.
+ proc.kill()
+ self.fail("subprocess did not stop on {}".format(name))
@unittest.skip("subprocesses aren't inheriting Ctrl+C property")
@support.requires_subprocess()