python / python/cpython

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

オープン
#121,157 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

stdlib topic-subprocess type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Windows で shell=True、capture_output=True、timeout=1 を指定した subprocess.run エントリポイントから始め、報告された powershell と cmd のケースを再現します。記載されている Python バージョン間で挙動を比較し、子プロセスが入力を受け付ける場合にどのような timeout の挙動になるべきかを判断します。無期限の停止が解消され、その挙動が適切な回帰テストでカバーされた時点で issue は完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
powershell, python, shell
領域
operating-systems
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。