Skip to content

subprocess.run() timeout not working with grandchildren and stdout=PIPE #76116

Closed

Description

@MartinRitter
BPO 31935
Nosy @vstinner, @vadmium
Superseder
  • 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 = None
    closed_at = <Date 2018-07-22.08:38:36.212>
    created_at = <Date 2017-11-03.15:01:31.986>
    labels = ['type-bug', 'library']
    title = 'subprocess.run() timeout not working with grandchildren and stdout=PIPE'
    updated_at = <Date 2018-07-22.08:38:36.210>
    user = 'https://bugs.python.org/MartinRitter'

    bugs.python.org fields:

    activity = <Date 2018-07-22.08:38:36.210>
    actor = 'martin.panter'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-07-22.08:38:36.212>
    closer = 'martin.panter'
    components = ['Library (Lib)']
    creation = <Date 2017-11-03.15:01:31.986>
    creator = 'Martin Ritter'
    dependencies = []
    files = ['47251']
    hgrepos = []
    issue_num = 31935
    keywords = []
    message_count = 3.0
    messages = ['305487', '305529', '322137']
    nosy_count = 3.0
    nosy_names = ['vstinner', 'martin.panter', 'Martin Ritter']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '30154'
    type = 'behavior'
    url = 'https://bugs.python.org/issue31935'
    versions = ['Python 3.6']

    Activity

    1. MartinRitter commented on Nov 3, 2017

      MartinRittermannequin
      MannequinAuthor

      Hi,

      I tried to use

          subprocess.run(..., stdout=subprocess.PIPE, timeout=N)

      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() 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.

    2. added
      stdlib
      type-bug
      on Nov 3, 2017
    3. vadmium commented on Nov 4, 2017

      @vadmium
      Member

      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 (?)

    4. vadmium commented on Jul 22, 2018

      @vadmium
      Member

      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.

    5. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibtype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions