`IO.Process.spawn` flushes the parent's stdout only when `stdin` (not `stdout`) is `.inherit`, so output comes out of order
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 78/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
調査の方向性
src/runtime/uv/process.cpp の lean_io_process_spawn を読み、特に338~340行付近のstdioモードのチェックを確認してください。issueではstdoutフィールドがインデックス1として示されています。提供されたLeanプログラムとリダイレクトされたstdoutで再現し、その後、継承されたstdoutとnull stdinの場合の出力順を確認してください。その場合に親プロセスのバッファリングされた出力が子プロセスの出力より先に表示され、既存の対話型stdinの動作が維持されれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Prerequisites
- Check that your issue is not already filed:
https://github.com/leanprover/lean4/issues - Reduce the issue to a minimal, self-contained, reproducible test case.
Avoid dependencies to Mathlib or Batteries. - Test your test case against the latest nightly release, for example on
https://live.lean-lang.org/#project=lean-nightly
(You can also use the settings there to switch to “Lean nightly”) - When reporting a perceived soundness bug: Does it affect
lake check --paranoid?
If in doubt, please reach out on [email protected] for responsible disclosure.
(Not a soundness bug.)
Description
Before spawning, std::cout is flushed only when the child inherits stdin (src/runtime/uv/process.cpp:338-340). With stdout := .inherit and stdin := .null or .piped, output the parent has buffered (stdout is a pipe) can appear after the child's output. Keying on stdin is old and probably deliberate: it makes an interactive prompt visible before a child reads the terminal. Nothing documents that inherited stdout is ordered, so this is a missing case rather than a broken contract. Flushing when stdout (field 1) is inherited as well would fix it.
if (static_cast<stdio_mode>(cnstr_get_uint8(stdio_cfg.raw(), 0)) == stdio_mode::inherit) {
std::cout.flush();
}
The program writes "parent1 " to standard output (a pipe, so the parent's stdio buffer keeps it), then calls IO.Process.spawn { cmd := "echo", args := #["child"], stdout := .inherit, stdin := .null }, waits for the child and writes "parent2 0". Its standard output is "child\nparent1 parent2 0\n": the child's line, written straight to the inherited descriptor 1, comes before the output the parent had already written. The parent's pending output should have been flushed before the child could write to the same stream, as the spawn code itself intends: it calls std::cout.flush() before spawning, but it guards that flush with the stdin mode (cnstr_get_uint8(stdio_cfg.raw(), 0)) instead of the stdout mode (field 1), so with stdout := .inherit, stdin := .null no flush happens. The same program with only stdin := .inherit prints "parent1 child\nparent2 0\n", i.e. the parent's earlier output first. So a field that has nothing to do with standard output — the child's standard input mode — changes the program's standard output; the flush decision belongs to the stdout field, which is .inherit in both programs.
Context
- Affected function:
lean_io_process_spawn(src/runtime/uv/process.cpp) - Upstream: no existing issue found; the cited code is unchanged in nightly-2026-11-26. The stdin-keyed flush already existed in the pre-libuv src/runtime/process.cpp (v4.20.0, line 558).
Steps to Reproduce
- Save the following as
input.lean:
import Std
def main : IO Unit := do
IO.print "parent1 "
let child ← IO.Process.spawn { cmd := "echo", args := #["child"], stdout := .inherit, stdin := .null }
let c ← child.wait
IO.println s!"parent2 {c}"
- Compile:
lean -c input.c input.lean && leanc -o prog input.c - Run with stdout redirected to a pipe or file:
./prog | cat(or./prog > out.txt; cat out.txt)
For comparison, the same program with stdin := .inherit (nothing else changed) prints the lines in the right order.
Expected behavior:
parent1 child
parent2 0
Actual behavior:
child
parent1 parent2 0
(With stdin := .inherit the output is parent1 child\nparent2 0\n, as expected.)
Versions
Lean (version 4.38.0-nightly-2026-11-26, arm64-apple-darwin24.6.0, commit e1228b8b45918ece1a00d68e5fc5e6a685ca59ee, Release)
macOS 26.6.2 (arm64)
Additional Information
Root cause analysis. lean_io_process_spawn receives an ordinary configuration { cmd := "echo", args := #["child"], stdout := .inherit, stdin := .null }. Before spawning, it flushes the parent's pending standard output only when cnstr_get_uint8(stdio_cfg.raw(), 0) — the standard input mode — equals inherit; with stdin := .null it skips the flush although the stdout mode (field 1) is inherit, so the child's child\n reaches the shared descriptor before the parent's buffered parent1 . The modes are not produced by a callee: stdio_cfg is the toStdioConfig field of the argument object the caller passed, and the only callee that sees the modes, setup_stdio, just mirrors each mode into the libuv container and returns; the wrong index is selected in lean_io_process_spawn itself at src/runtime/uv/process.cpp:338.
Suggested fix: also flush when the stdout field (index 1, cnstr_get_uint8(stdio_cfg.raw(), 1)) is inherit (keeping the existing stdin case, which makes a prompt visible before an interactive child reads the terminal); consider the same for stderr (index 2) if std::cerr is ever buffered. With the decision made on the stdout field, both variants print parent1 child\nparent2 0\n.
Impact
Programs that print to a pipe or file and then spawn a child with inherited stdout but non-inherited stdin (CI logs, scripted runs) can get output out of order.
Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.
- 主要言語
- Lean
- スター
- 10.3k
- フォーク
- 1.1k
- 平均マージ
- 18時間 27分
- マージ済み PR(30日)
- 390
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
leanprover/lean4 のほかの issue
-
bug
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
leanprover/lean4#16820 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 1/5 1〜3時間 初心者へのやさしさ 90/100
leanprover/lean4#16816 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
leanprover/lean4#16813 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
leanprover/lean4#16810 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
leanprover/lean4#16805 ·
メンテナーはふだん 1 日以内に返信
leanprover/lean4 の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
libsdl-org/SDL#16837 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
dracut-ng/dracut#2843 · コメント 4 件 ·
メンテナーはふだん 1 日以内に返信
-
henvcfg.STCE and henvcfg.PBMTE are read-only when their extension is implemented対応中かも @youzi27 が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
riscv/riscv-unified-db#2879 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 3 日以内に返信
-
[Bug] jailer: misc device minors below 100 are never found in /proc/misc (/dev/nitro_enclaves, /dev/userfaultfd missing from the jail)対応中かも @Gaspardcode が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
firecracker-microvm/firecracker#6490 ·
メンテナーはふだん 1 日以内に返信