[Python-checkins] bpo-40550: Fix time-of-check/time-of-action issue in subprocess.Popen.send_signal. (GH-20010)

miss-islington webhook-mailer at python.org
Sat Nov 21 04:46:26 EST 2020


https://github.com/python/cpython/commit/713b4bbd97392acc492a4fefb2d07ae61fb7b75d
commit: 713b4bbd97392acc492a4fefb2d07ae61fb7b75d
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2020-11-21T01:46:21-08:00
summary:

bpo-40550: Fix time-of-check/time-of-action issue in subprocess.Popen.send_signal. (GH-20010)


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 at krypto.org>
(cherry picked from commit 01a202ab6b0ded546e47073db6498262086c52e9)

Co-authored-by: Filipe Laíns <lains at archlinux.org>

files:
A Misc/NEWS.d/next/Library/2020-05-08-21-30-54.bpo-40550.i7GWkb.rst
M Lib/subprocess.py
M Lib/test/test_subprocess.py

diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index 13600c28cf711..f1d829a6f1724 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -2061,7 +2061,11 @@ def send_signal(self, sig):
             # 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 373542463f2e3..7373fe29c47d8 100644
--- a/Lib/test/test_subprocess.py
+++ b/Lib/test/test_subprocess.py
@@ -3160,6 +3160,19 @@ def test_send_signal_race(self):
         # 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
index 0000000000000..b0f3f03c34bbc
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-05-08-21-30-54.bpo-40550.i7GWkb.rst
@@ -0,0 +1 @@
+Fix time-of-check/time-of-action issue in subprocess.Popen.send_signal.



More information about the Python-checkins mailing list