leonida: a closed stdout or stderr drops the line instead of panicking (one write per line) #3447

Merged
leonida/mandy
MoonEyes merged 5 commits from fix/run-stdout-closed into main 2026-12-04 11:21:23 +00:00

Pull request successfully merged and closed

Member

Change

A closed stdout or stderr (a parent that stopped reading, a pipe closed early, a console gone) made leonida panic on the next println!/eprintln! and exit with 101. The 167 println!/eprintln! of apps/leonida now go through say!/say_err! (apps/leonida/src/out.rs), which drop the line when the stream is gone instead of panicking.

  • One line is one write: the line and its newline are formatted into a String and sent with a single write_all, so another writer cannot cut a line in two (measured: write_fmt made 5 to 6 writes per line). Output is byte for byte the same as println!. Cost: one String per line; none of the call sites is in a per-frame loop.
  • writeln! calls that already handle a write error on purpose (warm.rs, exit 1) are unchanged.
  • No println!/eprintln! left in apps/leonida/src (those in build.rs are cargo instructions).

Tests

  • cargo test -p leonida --locked: 58 passed, 0 failed (rerun by the reviewer side too). cargo clippy -p leonida --locked --all-targets: no diagnostic. check_no_game_data.py on the touched files: pass.
  • apps/leonida/tests/closed_stdout.rs (Windows only, #![cfg(windows)]): runs a leonida command with stdout bound to a file opened read-only; without the change the child panics and exits 101 (checked by putting an .unwrap() back for one try). On Linux std turns that case (EBADF) into Ok, so the test proves nothing there; a closed pipe (EPIPE) still reaches println! on Linux and is handled by the same macros.
  • Not a rendering change: no in-game gate needed.

AI-assisted

Written with Freebuff (its "ultra" mode; the tool does not say which underlying model it runs), driven by Moon. Reviewed twice by separate Claude Opus 5.5 reviewer instances (Claude Code): accepted, the two requested changes (single write, Windows-only test) are in. Commit history kept as written; one early commit message still has the wrong Linux note, fixed in the next commit, so a squash merge is planned.

🤖 PR opened with Claude Code

## Change A closed stdout or stderr (a parent that stopped reading, a pipe closed early, a console gone) made `leonida` panic on the next `println!`/`eprintln!` and exit with 101. The 167 `println!`/`eprintln!` of `apps/leonida` now go through `say!`/`say_err!` (`apps/leonida/src/out.rs`), which drop the line when the stream is gone instead of panicking. - One line is one write: the line and its newline are formatted into a `String` and sent with a single `write_all`, so another writer cannot cut a line in two (measured: `write_fmt` made 5 to 6 writes per line). Output is byte for byte the same as `println!`. Cost: one `String` per line; none of the call sites is in a per-frame loop. - `writeln!` calls that already handle a write error on purpose (`warm.rs`, exit 1) are unchanged. - No `println!`/`eprintln!` left in `apps/leonida/src` (those in `build.rs` are cargo instructions). ## Tests - `cargo test -p leonida --locked`: 58 passed, 0 failed (rerun by the reviewer side too). `cargo clippy -p leonida --locked --all-targets`: no diagnostic. `check_no_game_data.py` on the touched files: pass. - `apps/leonida/tests/closed_stdout.rs` (Windows only, `#![cfg(windows)]`): runs a `leonida` command with stdout bound to a file opened read-only; without the change the child panics and exits 101 (checked by putting an `.unwrap()` back for one try). On Linux std turns that case (EBADF) into Ok, so the test proves nothing there; a closed pipe (EPIPE) still reaches `println!` on Linux and is handled by the same macros. - Not a rendering change: no in-game gate needed. ## AI-assisted Written with **Freebuff** (its "ultra" mode; the tool does not say which underlying model it runs), driven by Moon. Reviewed twice by separate Claude Opus 5.5 reviewer instances (Claude Code): accepted, the two requested changes (single write, Windows-only test) are in. Commit history kept as written; one early commit message still has the wrong Linux note, fixed in the next commit, so a squash merge is planned. 🤖 PR opened with [Claude Code](https://claude.com/claude-code)
`println!` and `eprintln!` panic when their write fails, and a write fails for
good once the pipe's reader is gone: a launcher that closes the pipe it started
`leonida run` with would end the run with a panic on its next line of progress,
the title still running behind it.

Every line the app writes now goes through `out::say!` or `out::say_err!`,
which write the same text to stdout or stderr and drop a failed write, as
`run::report_watch` already did for its watched values. The 167 call sites in
apps/leonida keep their format strings unchanged. The two writes that must know
whether anyone still reads them (a warm child tells its parent, `warm.rs`) keep
their own checked write and exit.

Two tests: a line written to a pipe whose reader has gone is dropped, and a
command whose stdout cannot be written (`census` with a read-only handle as its
stdout) ends cleanly instead of with 101. Both fail when the write is made to
panic again.

cargo test -p leonida --locked: 58 passed, 0 failed (63 with --features
diagnostics); cargo clippy -p leonida --locked --all-targets: no findings;
check_no_game_data.py on the touched files: clean.

AI-assisted (Freebuff, Buffy)
Two review points.

`out::line_to` wrote the text with `write_fmt` and then wrote the newline with a
second call, so the text and its newline were two writes. It now does one
`write_fmt` of `"{args}\n"`: the same text, one write.

`tests/closed_stdout.rs` only proves anything on Windows: on Linux, `std` treats
a failed write to stdout as success (a read-only descriptor gives `EBADF`, a
closed pipe `EPIPE`, and `Stdout::write` returns the buffer's length either
way), so `println!` would not panic and the command would end with 0 whether or
not the fix is in place. The file is `#![cfg(windows)]`, with the reason in its
doc comment.

`cargo test -p leonida --locked`: 58 passed, 0 failed. `cargo clippy -p leonida
--locked --all-targets`: no findings. With `line_to` made to panic again, both
tests fail: the unit test at `out.rs:19`, and `census` ends with 101.

AI-assisted (Freebuff, Buffy)
`out::line_to` formatted the text and its newline into one `write_fmt`; that is
one call, but `write_fmt` still writes a piece per literal and per argument (and
one for the newline), so a writer outside the stream's lock could land inside a
line. The line is now formatted into a buffer and written with one `write_all`.

The `#![cfg(windows)]` note in `tests/closed_stdout.rs` said `std` swallows
both `EBADF` and `EPIPE` on Linux; it swallows `EBADF` only (`StdoutRaw::write`
maps a bad descriptor to `Ok`), and a closed pipe still reaches `println!`,
which panics there. The note now says so.

`cargo test -p leonida --locked`: 58 passed, 0 failed. `cargo clippy -p leonida
--locked --all-targets`: no findings. `check_no_game_data.py` on the two files:
clean. With the write made to panic again, both tests fail (`out.rs:23`, and
`census` ends with 101).

AI-assisted (Freebuff, Buffy)
The doc comment on `line_to` said `write_fmt` writes a piece per literal and
per argument plus one for the newline. A counting `Write` says: a line with
three literals and two arguments takes 5 writes through `write_fmt`, the merged
`"{args}\n"` form also takes 5 (the newline joins the last literal), and the
buffer with one `write_all` takes 1. The comment now states the rule without the
off-by-one story.

No behaviour change. `cargo test -p leonida --locked`: 58 passed, 0 failed.
`cargo clippy -p leonida --locked --all-targets`: no findings.

AI-assisted (Freebuff, Buffy)
Author
Member

Merging (squash). Not a rendering change. Branch updated with main 93db515363; the merged tree (326f1d15) is the one tested locally: cargo test --locked -p leonida-gpu -p leonida -p leonida-hle all passed, 0 failed; tests/test_d3d12_switches.py OK. Reviewed twice by separate reviewer instances (accepted). Thanks to Freebuff and Moon. AI-assisted (Claude Opus 5.5 for the review and merge).

Merging (squash). Not a rendering change. Branch updated with main `93db515363`; the merged tree (`326f1d15`) is the one tested locally: `cargo test --locked -p leonida-gpu -p leonida -p leonida-hle` all passed, 0 failed; `tests/test_d3d12_switches.py` OK. Reviewed twice by separate reviewer instances (accepted). Thanks to Freebuff and Moon. AI-assisted (Claude Opus 5.5 for the review and merge).
Sign in to join this conversation.
No description provided.