mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
bpo-25942: make subprocess more graceful on ^C (GH-5026)
Do not allow receiving a SIGINT to cause the subprocess module to trigger an immediate SIGKILL of the child process. SIGINT is normally sent to all child processes by the OS at the same time already as was the established normal behavior in 2.7 and 3.2. This behavior change was introduced during the fix to https://bugs.python.org/issue12494 and is generally surprising to command line tool users who expect other tools launched in child processes to get their own SIGINT and do their own cleanup. In Python 3.3-3.6 subprocess.call and subprocess.run would immediately SIGKILL the child process upon receiving a SIGINT (which raises a KeyboardInterrupt). We now give the child a small amount of time to exit gracefully before resorting to a SIGKILL. This is also the case for subprocess.Popen.__exit__ which would previously block indefinitely waiting for the child to die. This was hidden from many users by virtue of subprocess.call and subprocess.run sending the signal immediately. Behavior change: subprocess.Popen.__exit__ will not block indefinitely when the exiting exception is a KeyboardInterrupt. This is done for user friendliness as people expect their ^C to actually happen. This could cause occasional orphaned Popen objects when not using `call` or `run` with a child process that hasn't exited. Refactoring involved: The Popen.wait method deals with the KeyboardInterrupt second chance, existing platform specific internals have been renamed to _wait(). Also fixes comment typos.
This commit is contained in:
parent
83e64c8a54
commit
f4d644f36f
3 changed files with 143 additions and 13 deletions
|
@ -2921,6 +2921,71 @@ class Win32ProcessTestCase(BaseTestCase):
|
|||
self._kill_dead_process('terminate')
|
||||
|
||||
class MiscTests(unittest.TestCase):
|
||||
|
||||
class RecordingPopen(subprocess.Popen):
|
||||
"""A Popen that saves a reference to each instance for testing."""
|
||||
instances_created = []
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.instances_created.append(self)
|
||||
|
||||
@mock.patch.object(subprocess.Popen, "_communicate")
|
||||
def _test_keyboardinterrupt_no_kill(self, popener, mock__communicate,
|
||||
**kwargs):
|
||||
"""Fake a SIGINT happening during Popen._communicate() and ._wait().
|
||||
|
||||
This avoids the need to actually try and get test environments to send
|
||||
and receive signals reliably across platforms. The net effect of a ^C
|
||||
happening during a blocking subprocess execution which we want to clean
|
||||
up from is a KeyboardInterrupt coming out of communicate() or wait().
|
||||
"""
|
||||
|
||||
mock__communicate.side_effect = KeyboardInterrupt
|
||||
try:
|
||||
with mock.patch.object(subprocess.Popen, "_wait") as mock__wait:
|
||||
# We patch out _wait() as no signal was involved so the
|
||||
# child process isn't actually going to exit rapidly.
|
||||
mock__wait.side_effect = KeyboardInterrupt
|
||||
with mock.patch.object(subprocess, "Popen",
|
||||
self.RecordingPopen):
|
||||
with self.assertRaises(KeyboardInterrupt):
|
||||
popener([sys.executable, "-c",
|
||||
"import time\ntime.sleep(9)\nimport sys\n"
|
||||
"sys.stderr.write('\\n!runaway child!\\n')"],
|
||||
stdout=subprocess.DEVNULL, **kwargs)
|
||||
for call in mock__wait.call_args_list[1:]:
|
||||
self.assertNotEqual(
|
||||
call, mock.call(timeout=None),
|
||||
"no open-ended wait() after the first allowed: "
|
||||
f"{mock__wait.call_args_list}")
|
||||
sigint_calls = []
|
||||
for call in mock__wait.call_args_list:
|
||||
if call == mock.call(timeout=0.25): # from Popen.__init__
|
||||
sigint_calls.append(call)
|
||||
self.assertLessEqual(mock__wait.call_count, 2,
|
||||
msg=mock__wait.call_args_list)
|
||||
self.assertEqual(len(sigint_calls), 1,
|
||||
msg=mock__wait.call_args_list)
|
||||
finally:
|
||||
# cleanup the forgotten (due to our mocks) child process
|
||||
process = self.RecordingPopen.instances_created.pop()
|
||||
process.kill()
|
||||
process.wait()
|
||||
self.assertEqual([], self.RecordingPopen.instances_created)
|
||||
|
||||
def test_call_keyboardinterrupt_no_kill(self):
|
||||
self._test_keyboardinterrupt_no_kill(subprocess.call, timeout=6.282)
|
||||
|
||||
def test_run_keyboardinterrupt_no_kill(self):
|
||||
self._test_keyboardinterrupt_no_kill(subprocess.run, timeout=6.282)
|
||||
|
||||
def test_context_manager_keyboardinterrupt_no_kill(self):
|
||||
def popen_via_context_manager(*args, **kwargs):
|
||||
with subprocess.Popen(*args, **kwargs) as unused_process:
|
||||
raise KeyboardInterrupt # Test how __exit__ handles ^C.
|
||||
self._test_keyboardinterrupt_no_kill(popen_via_context_manager)
|
||||
|
||||
def test_getoutput(self):
|
||||
self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy')
|
||||
self.assertEqual(subprocess.getstatusoutput('echo xyzzy'),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue