diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/subprocess.py | 6 | ||||
-rw-r--r-- | Lib/test/test_subprocess.py | 13 |
2 files changed, 18 insertions, 1 deletions
diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 6a6c2fc..e259dc3 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -2078,7 +2078,11 @@ class Popen(object): # The race condition can still happen if the race condition # described above happens between the returncode test # and the kill() call. - os.kill(self.pid, sig) + try: + os.kill(self.pid, sig) + except ProcessLookupError: + # Supress the race condition error; bpo-40550. + pass def terminate(self): """Terminate the process with SIGTERM diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index e25474a..2a4c475 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -3229,6 +3229,19 @@ class POSIXProcessTestCase(BaseTestCase): # so Popen failed to read it and uses a default returncode instead. self.assertIsNotNone(proc.returncode) + def test_send_signal_race2(self): + # bpo-40550: the process might exist between the returncode check and + # the kill operation + p = subprocess.Popen([sys.executable, '-c', 'exit(1)']) + + # wait for process to exit + while not p.returncode: + p.poll() + + with mock.patch.object(p, 'poll', new=lambda: None): + p.returncode = None + p.send_signal(signal.SIGTERM) + def test_communicate_repeated_call_after_stdout_close(self): proc = subprocess.Popen([sys.executable, '-c', 'import os, time; os.close(1), time.sleep(2)'], |