`IO.Process.spawn` flushes the parent's stdout only when `stdin` (not `stdout`) is `.inherit`, so output comes out of order

オープン 初心者向け
#16,809 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 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 の本文から書いたものです。

説明

bug
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
  1. 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}"
  1. Compile: lean -c input.c input.lean && leanc -o prog input.c
  2. 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

環境構築

はじめの一歩

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

leanprover/lean4 のほかの issue

leanprover/lean4 の issue をすべて見る

似ている issue

Operating Systems の issue をもっと見る

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

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