[Python-checkins] cpython (3.4): Fix issue #6973: When we know a subprocess.Popen process has died, do

gregory.p.smith python-checkins at python.org
Sun Nov 15 21:31:55 EST 2015


https://hg.python.org/cpython/rev/6bd3c8623bb2
changeset:   99146:6bd3c8623bb2
branch:      3.4
parent:      99143:41ddf2c47a18
user:        Gregory P. Smith <greg at krypto.org>
date:        Sun Nov 15 18:19:10 2015 -0800
summary:
  Fix 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.

files:
  Lib/subprocess.py |  19 ++++++++++++-------
  Misc/NEWS         |   4 ++++
  2 files changed, 16 insertions(+), 7 deletions(-)


diff --git a/Lib/subprocess.py b/Lib/subprocess.py
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -1241,8 +1241,10 @@
             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 @@
                 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 @@
 
 
         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
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -106,6 +106,10 @@
 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

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list