You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
subprocess.run() timeout not working with grandchildren and stdout=PIPE #76116
bpo-30154: subprocess.run with stderr connected to a pipe won't timeout when killing a never-ending shell commanad
Files
test_killsub.py: Minimal example to reproduce timeout problem
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2018-07-22.08:38:36.212>created_at=<Date2017-11-03.15:01:31.986>labels= ['type-bug', 'library']
title='subprocess.run() timeout not working with grandchildren and stdout=PIPE'updated_at=<Date2018-07-22.08:38:36.210>user='https://bugs.python.org/MartinRitter'
This works as expected with simple scripts. However if the script itself creates other children which write to stdout then subprocess.run() seems to wait for all of the children to finish. I've attached a minimal example.
I looked into subprocess.py and subprocess.run() calls process.communicate() again without timeout when handling the TimeoutExpired exception which then in turn waits for the pipes to be closed by all children.
If communicate() would check if the process is still alive while waiting for output and close the pipes once the process has finished the timeout feature should work as expected and descendants would get a SIGPIPE when writing to stdout/stderr.
This proposal sounds like a race condition. Closing the output pipe as a child exits means you risk missing recent output. On the other hand, if you don’t care about the output any more, close the pipe first and then wait for the child.
Related discussions:
bpo-30154: Similar problem, but the grandchild does not write any output bpo-26534: kill_group=True option for timeout bpo-31447: Windows-specific (?)
Closing in faviour of bpo-30154, which suggests documentation or adjusting the timeout implementation. There is also bpo-26534 proposing a new “kill_group” option when using the timeout feature.
Activity
MartinRitter commentedon Nov 3, 2017
Hi,
I tried to use
to run some test scripts with a given timeout.
This works as expected with simple scripts. However if the script itself creates other children which write to stdout then
subprocess.run()seems to wait for all of the children to finish. I've attached a minimal example.I looked into subprocess.py and
subprocess.run()callsprocess.communicate()again without timeout when handling the TimeoutExpired exception which then in turn waits for the pipes to be closed by all children.If communicate() would check if the process is still alive while waiting for output and close the pipes once the process has finished the timeout feature should work as expected and descendants would get a SIGPIPE when writing to stdout/stderr.
vadmium commentedon Nov 4, 2017
This proposal sounds like a race condition. Closing the output pipe as a child exits means you risk missing recent output. On the other hand, if you don’t care about the output any more, close the pipe first and then wait for the child.
Related discussions:
bpo-30154: Similar problem, but the grandchild does not write any output
bpo-26534: kill_group=True option for timeout
bpo-31447: Windows-specific (?)
vadmium commentedon Jul 22, 2018
Closing in faviour of bpo-30154, which suggests documentation or adjusting the timeout implementation. There is also bpo-26534 proposing a new “kill_group” option when using the timeout feature.