]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40550: Fix time-of-check/time-of-action issue in subprocess.Popen.send_signal...
authorFilipe Laíns <lains@archlinux.org>
Sat, 21 Nov 2020 09:22:08 +0000 (09:22 +0000)
committerGitHub <noreply@github.com>
Sat, 21 Nov 2020 09:22:08 +0000 (01:22 -0800)
send_signal() now swallows the exception if the process it thought was still alive winds up not to exist anymore (always a plausible race condition despite the checks).

Co-authored-by: Gregory P. Smith <greg@krypto.org>
Lib/subprocess.py
Lib/test/test_subprocess.py
Misc/NEWS.d/next/Library/2020-05-08-21-30-54.bpo-40550.i7GWkb.rst [new file with mode: 0644]

index 6a6c2fc98e83f3c527a07911ae4dcd9c4b4f2d92..e259dc3a8e538af49a5a4dd4d62426f596c6a759 100644 (file)
@@ -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
index e25474abed4b78df2829ff9cb7f8b87a4426c79a..2a4c47530e6a1bd3c29f83f3c21364e704254f71 100644 (file)
@@ -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)'],
diff --git a/Misc/NEWS.d/next/Library/2020-05-08-21-30-54.bpo-40550.i7GWkb.rst b/Misc/NEWS.d/next/Library/2020-05-08-21-30-54.bpo-40550.i7GWkb.rst
new file mode 100644 (file)
index 0000000..b0f3f03
--- /dev/null
@@ -0,0 +1 @@
+Fix time-of-check/time-of-action issue in subprocess.Popen.send_signal.