]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix issue #6973: When we know a subprocess.Popen process has died, do
authorGregory P. Smith <greg@krypto.org>
Mon, 16 Nov 2015 02:19:10 +0000 (18:19 -0800)
committerGregory P. Smith <greg@krypto.org>
Mon, 16 Nov 2015 02:19:10 +0000 (18:19 -0800)
not allow the send_signal(), terminate(), or kill() methods to do
anything as they could potentially signal a different process.

Lib/subprocess.py
Misc/NEWS

index f11e538925bc0f8b99c022257fea21a34c49316e..abf43e55861a3ef2ee640e3e75062e59e4a466be 100644 (file)
@@ -1241,8 +1241,10 @@ class Popen(object):
             return (stdout, stderr)
 
         def send_signal(self, sig):
-            """Send a signal to the process
-            """
+            """Send a signal to the process."""
+            # Don't signal a process that we know has already died.
+            if self.returncode is not None:
+                return
             if sig == signal.SIGTERM:
                 self.terminate()
             elif sig == signal.CTRL_C_EVENT:
@@ -1253,8 +1255,10 @@ class Popen(object):
                 raise ValueError("Unsupported signal: {}".format(sig))
 
         def terminate(self):
-            """Terminates the process
-            """
+            """Terminates the process."""
+            # Don't terminate a process that we know has already died.
+            if self.returncode is not None:
+                return
             try:
                 _winapi.TerminateProcess(self._handle, 1)
             except PermissionError:
@@ -1678,9 +1682,10 @@ class Popen(object):
 
 
         def send_signal(self, sig):
-            """Send a signal to the process
-            """
-            os.kill(self.pid, sig)
+            """Send a signal to the process."""
+            # Skip signalling a process that we know has already died.
+            if self.returncode is None:
+                os.kill(self.pid, sig)
 
         def terminate(self):
             """Terminate the process with SIGTERM
index 881f0351e83025dc9e028e3404d3368884afe06a..2d0755f931d598478434541edc35cf3d4b815ba8 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -106,6 +106,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #6973: When we know a subprocess.Popen process has died, do
+  not allow the send_signal(), terminate(), or kill() methods to do
+  anything as they could potentially signal a different process.
+
 - Issue #25578: Fix (another) memory leak in SSLSocket.getpeercer().
 
 - Issue #25590: In the Readline completer, only call getattr() once per