python / python/cpython

subprocess.run timeout does not function if shell=True and capture_output=True

Open
#121,157 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-subprocess type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

I am using python 3.12.4 on windows 10 and when calling subprocess.run("powershell", shell=True, capture_output=True, timeout=1) code execution seems to suspend indefinetly as the process now accepts the user input and interprets it.

This behaviour matches the one described in #81605 despite it having supposedly been fixed 5 years ago.
For example when calling subprocess.run("cmd", shell=True, capture_output=True, timeout=1) and then entering echo o>f.txt a new file called "f.txt" gets created with "o" as its content.

When using the IDLE Shell the process completes nearly instandly and returns this:

CompletedProcess(args='powershell', returncode=0, stdout=b'Windows PowerShell\r\nCopyright (C) Microsoft Corporation. Alle Rechte vorbehalten.\r\n\r\nLernen Sie das neue plattform\x81bergreifende PowerShell kennen - https://aka.ms/pscore6\r\n\r\nPS C:\Users\Mabeeck\Desktop\Python> ', stderr=b'')

The same applies for Python 3.8.9, 3.9.2, 3.10.6, 3.11.5 and 3.13.0b3
I'll apoligise in advance, if my impression of the bug beeing supposed to be fixed is wrong.

CPython versions tested on:

3.8, 3.9, 3.10, 3.11, 3.12, 3.13

Operating systems tested on:

Windows

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the subprocess.run entry point using shell=True, capture_output=True, and timeout=1 on Windows, reproducing the reported powershell and cmd cases. Compare the behavior across the listed Python versions and determine what timeout behavior should occur when the child process accepts input; the issue is complete when the indefinite suspension is resolved and the behavior is covered by an appropriate regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell, python, shell
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.