# HG changeset patch # Parent ebb1a16de89b712010cd195a1e2331673836040a Issue #25942: Give child a chance to handle SIGINT before using SIGKILL This fixes a regression introduced with revision 86b7f14485c9. diff -r ebb1a16de89b Lib/subprocess.py --- a/Lib/subprocess.py Sat Apr 16 11:51:31 2016 +0000 +++ b/Lib/subprocess.py Sat Apr 16 12:40:18 2016 +0000 @@ -568,8 +568,7 @@ try: return p.wait(timeout=timeout) except: - p.kill() - p.wait() + _abort_run(p, timeout) raise @@ -709,8 +708,7 @@ raise TimeoutExpired(process.args, timeout, output=stdout, stderr=stderr) except: - process.kill() - process.wait() + _abort_run(process, timeout) raise retcode = process.poll() if check and retcode: @@ -719,6 +717,18 @@ return CompletedProcess(process.args, retcode, stdout, stderr) +def _abort_run(process, timeout): + if timeout is None: + try: + with process: + pass # Give the child a chance to handle SIGINT + except: + process.kill() # Last resort to avoid a zombie + raise + else: + process.kill() + + def list2cmdline(seq): """ Translate a sequence of arguments into a command line diff -r ebb1a16de89b Misc/NEWS --- a/Misc/NEWS Sat Apr 16 11:51:31 2016 +0000 +++ b/Misc/NEWS Sat Apr 16 12:40:18 2016 +0000 @@ -243,6 +243,11 @@ Library ------- +- Issue #25942: Stop sending SIGKILL straight away when the + subprocess.call(), run(), etc functions are interrupted. These functions + now try waiting a second time to give the child process a chance to handle + a SIGINT from the terminal. This fixes a regression introduced in 3.3a1. + - Issue #26404: Add context manager to socketserver. Patch by Aviv Palivoda. - Issue #26735: Fix :func:`os.urandom` on Solaris 11.3 and newer when reading