Author Daniel Shaulov
Recipients Daniel Shaulov, astrand
Date 2016-03-10.23:45:30
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1457653531.44.0.556796452495.issue26534@psf.upfronthosting.co.za>
In-reply-to
Content
Basically -
subprocess.check_output("ping localhost", shell=True, timeout=5)
Will hang forever.

What happens is that subprocess.run times out on communicate, sends SIGKILL *to the shell* and then calls communicate again without the timeout. But nothing actually closes the "ping" command so the second communicate will never exit.

Even if we put a timeout on the second communicate, it won't be good enough because that "ping" will still be "leaked".

This SO question is about the fact that kill doesn't work when shell=True, and might be relevant for this issue:
http://stackoverflow.com/questions/4789837/how-to-terminate-a-python-subprocess-launched-with-shell-true

As a possible fix I propose doing the following:
1. Add killpg to the Popen class.
2. Add an argument to run - "kill_group" that makes run use killpg instead of kill
3. Users can all:
subprocess.check_output("ping localhost", shell=True, start_new_session=True, kill_group=True, timeout=5)

And have it work fine. This is the best I could come up with, without breaking any existing behavior.

Other options to consider:
1. Not have kill_group argument and to implicitly kill by group when start_new_session is True (but this is not really backwards compatible).
2. Having kill_group as an argument for Popen and not run, and when kill is called, it will call killpg if the option was specified.

A patch is attached with my proposed solution.
History
Date User Action Args
2016-03-10 23:45:31Daniel Shaulovsetrecipients: + Daniel Shaulov, astrand
2016-03-10 23:45:31Daniel Shaulovsetmessageid: <1457653531.44.0.556796452495.issue26534@psf.upfronthosting.co.za>
2016-03-10 23:45:31Daniel Shaulovlinkissue26534 messages
2016-03-10 23:45:31Daniel Shaulovcreate