Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subprocess.run keeps waiting after the process termination #97000

Open
piwicode opened this issue Sep 21, 2022 · 1 comment
Open

subprocess.run keeps waiting after the process termination #97000

piwicode opened this issue Sep 21, 2022 · 1 comment
Labels
type-bug An unexpected behavior, bug, or error

Comments

@piwicode
Copy link

piwicode commented Sep 21, 2022

subprocess.run keeps waiting after the process termination.
This happens when stdout or stderr is redirected, and the started process pass that stream to a background process.

Reproduction:
Create count.sh

function count {
    for i in 1 2 3 4 5;
    do
        sleep 1
        echo "$i" >&2
    done
}
count &
echo "Done" >&2

Reproduce with:

import subprocess
subprocess.run(["/bin/bash", "./count.sh"], stdout=subprocess.PIPE, timeout=1)

Result:
subprocess.TimeoutExpired: Command '['/bin/bash', './count.sh']' timed out after 1 seconds
This is not correct as the subprocess has terminated and produces a return code.
The expected behavior for run is to return as soon as the instanciated terminates, regardless of the streams lifecycles.

Variants:
Calling subprocess.run(["/bin/bash", "./count.sh"], timeout=1) works as expected, that is to say run returns immediately.

Environment:

Python 3.8.9:

  • CPython versions tested on:
  • Operating system MacOs, Linux
@piwicode piwicode added the type-bug An unexpected behavior, bug, or error label Sep 21, 2022
@piwicode
Copy link
Author

piwicode commented Sep 22, 2022

For information it can be workaround with a temporary file.

    stdout = tempfile.NamedTemporaryFile()
    stderr = tempfile.NamedTemporaryFile()
    completed_process = subprocess.run(args, timeout=timeout, stdout=stdout, stderr=stderr)
    completed_process.stdout = open(stdout.name, 'rt').read()
    completed_process.stderr = open(stderr.name, 'rt').read()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant