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.
This commit is contained in:
Gregory P. Smith 2015-11-15 18:19:10 -08:00
parent 025a1fd990
commit a0c9caad66
2 changed files with 16 additions and 7 deletions

View file

@ -1241,8 +1241,10 @@ class Popen(object):
return (stdout, stderr) return (stdout, stderr)
def send_signal(self, sig): 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: if sig == signal.SIGTERM:
self.terminate() self.terminate()
elif sig == signal.CTRL_C_EVENT: elif sig == signal.CTRL_C_EVENT:
@ -1253,8 +1255,10 @@ class Popen(object):
raise ValueError("Unsupported signal: {}".format(sig)) raise ValueError("Unsupported signal: {}".format(sig))
def terminate(self): 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: try:
_winapi.TerminateProcess(self._handle, 1) _winapi.TerminateProcess(self._handle, 1)
except PermissionError: except PermissionError:
@ -1678,9 +1682,10 @@ class Popen(object):
def send_signal(self, sig): def send_signal(self, sig):
"""Send a signal to the process """Send a signal to the process."""
""" # Skip signalling a process that we know has already died.
os.kill(self.pid, sig) if self.returncode is None:
os.kill(self.pid, sig)
def terminate(self): def terminate(self):
"""Terminate the process with SIGTERM """Terminate the process with SIGTERM

View file

@ -106,6 +106,10 @@ Core and Builtins
Library 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 #25578: Fix (another) memory leak in SSLSocket.getpeercer().
- Issue #25590: In the Readline completer, only call getattr() once per - Issue #25590: In the Readline completer, only call getattr() once per