Skip to content

subprocess.run() hangs when nested in subprocess.Popen() context #151139

@hundeboll

Description

@hundeboll

Bug report

Bug description:

I'd expect this snippet to raise a subprocess.CalledProcessError exception, but it just hangs instead:

import subprocess

with subprocess.Popen(["sleep", "infinity"]) as process:
    subprocess.run(["false"], check=True)
    process.terminate()

The same functionality without the manged context does raise as expected:

import subprocess

process = subprocess.Popen(["sleep", "infinity"])
subprocess.run(["false"], check=True)
process.terminate()

The nested call to subprocess.run() returns as expected, when the called process doesn't fail:

import subprocess

with subprocess.Popen(["sleep", "infinity"]) as process:
    subprocess.run(["true"], check=True)
    process.terminate()

CPython versions tested on:

3.13.13, 3.14.5, 3.15.0b2

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedtopic-subprocessSubprocess issues.type-bugAn unexpected behavior, bug, or error
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions