Skip to content

subprocess.check_output() with timeout does not exit if child process does not generate output after timeout #76976

Closed

Description

@MikeLewis
mannequin
BPO 32795
Nosy @eryksun, @fangyi-zhou
PRs
  • bpo-32795: Subprocess setpgid #5684
  • Superseder
  • bpo-37424: subprocess.run timeout does not function if shell=True and capture_output=True
  • Files
  • timeout_examples.tgz: Python 2.7 and Python 3 examples recreating the issue
  • 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 2021-02-27.19:32:06.090>
    created_at = <Date 2018-02-08.09:11:07.043>
    labels = ['type-bug', 'library']
    title = 'subprocess.check_output() with timeout does not exit if child process does not generate output after timeout'
    updated_at = <Date 2021-02-27.19:32:06.090>
    user = 'https://bugs.python.org/MikeLewis'

    bugs.python.org fields:

    activity = <Date 2021-02-27.19:32:06.090>
    actor = 'eryksun'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-02-27.19:32:06.090>
    closer = 'eryksun'
    components = ['Library (Lib)']
    creation = <Date 2018-02-08.09:11:07.043>
    creator = 'Mike Lewis'
    dependencies = []
    files = ['47429']
    hgrepos = []
    issue_num = 32795
    keywords = ['patch']
    message_count = 7.0
    messages = ['311820', '311821', '312117', '312135', '312172', '312175', '387784']
    nosy_count = 3.0
    nosy_names = ['eryksun', 'Mike Lewis', 'fangyizhou']
    pr_nums = ['5684']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = '37424'
    type = 'behavior'
    url = 'https://bugs.python.org/issue32795'
    versions = ['Python 2.7', 'Python 3.5']

    Activity

    1. MikeLewis commented on Feb 8, 2018

      MikeLewismannequin
      MannequinAuthor

      When using a timeout with check_output(), the call does not terminate unless the child process generates output after the timeout. Looking at the code, it appears there is a second call to communicate() after the timeout has happened, presumably to retrieve any remaining output. This call appears to hang until the child process generates output.

      I have two test cases (for Python 2.7 / subprocess32 and Python 3 / subprocess respectively). They show the same behaviour, the Python 2.7 version has been reproduced on Ubuntu 16.04.3 and Centos 7 and the Python 3 version on Ubuntu 16.043.

      Each test case has a first example where bash executes a long sleep before generating output and where the timeout is not respected, and a second example that generates output at intervals and the timeout is respected.

      Relevant code also attached below for reference:

      then = time.time()
      print("Subprocess with idle stdout at timeout: start at {}".format(then))
      try:
          output = subprocess.check_output(["bash", "-c",
                                              "echo Subcall; sleep 5; echo Done;"],
                                             stderr=subprocess.STDOUT, timeout=1)
          now = time.time()
          print("Finish at: {}, {:.0f} seconds".format(now, now-then))
          print(output)
      except subprocess.TimeoutExpired as te:
          now = time.time()
          print("Timed out at: {}, {:.0f} seconds".format(now, now-then))
      
      then = time.time()
      print("Generating stdout from subprocess: start at ", then)
      try:
          output = subprocess.check_output(["bash", "-c",
                                              "echo Subcall; for i in 1 2 3 4 5; do sleep 1; echo $i; done; echo Done;"],
                                             stderr=subprocess.STDOUT, timeout=1)
          now = time.time()
          print("Finish at: {}, {:.0f} seconds".format(now, now-then))
          print(output)
      except subprocess.TimeoutExpired as te:
          now = time.time()
          print("Timed out at: {}, {:.0f} seconds".format(now, now-then))
    2. added
      stdlib
      type-bug
      on Feb 8, 2018
    3. MikeLewis commented on Feb 8, 2018

      MikeLewismannequin
      MannequinAuthor

      Example of the output:

      $ ./timeout.py
      Subprocess with idle stdout at timeout: start at  1518081130.44
      Timed out at: 1518081135.45, 5 seconds
      Generating stdout from subprocess: start at  1518081135.45
      Timed out at: 1518081136.47, 1 seconds

      As you see, the 1 second timeout is respected in the second case where the child process is generating output.

    4. fangyi-zhou commented on Feb 13, 2018

      fangyi-zhoumannequin
      Mannequin

      I think the issue is that the process is not terminated.

      If you increase the sleep duration, you would see that the sleep process is not killed by SIGKILL.

    5. fangyi-zhou commented on Feb 13, 2018

      fangyi-zhoumannequin
      Mannequin

      As far as I have discovered, the bash process is killed by after the timeout. Presumably the pipe is still open due to the sleep process, so in order to retrieve any output from stdout/stderr, the read waits on the pipe, hence leading to timeout not respected.

      We can fix this issue in 2 ways
      (1) kill process tree when timeout happens
      (2) force closing the pipes after timeout, but this will leave the sub-processes detached and it will be kept running

      I don't know which way is better, hopefully someone with more knowledge can engage in the discussion.

    6. fangyi-zhou commented on Feb 14, 2018

      fangyi-zhoumannequin
      Mannequin

      I choose to implement the first way. I believe this is in favour in prevention of resource leaks and zombie processes.

      #5684

    7. eryksun commented on Feb 14, 2018

      @eryksun
      Contributor

      Work on related issues extends back 9 years to bpo-5115. Also see bpo-26534.

    8. eryksun commented on Feb 27, 2021

      @eryksun
      Contributor

      bpo-37424 fixed this for Python 3.7+ in POSIX. It's still broken in Windows, for which I'm leaving bpo-31447 open.

    9. 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