Skip to content

fix(io): flush buffered output before IO.exit - #1519

Merged
Lorenzobattistela merged 1 commit into
bendlang:mainfrom
vicmcorrea:fix/io-exit-flush
Oct 13, 2026
Merged

Lorenzobattistela merged 1 commit into
bendlang:mainfrom
vicmcorrea:fix/io-exit-flush

Conversation

@vicmcorrea

Copy link
Copy Markdown
Contributor

Problem and fix

Native IO.print("buffered") followed by IO.exit(0) can report success even when stdout's reader has closed. The short output stays buffered, and libc's implicit exit flush bypasses Bend's stream-error handling.

Call the existing io_sync() after exit-code validation and before native exit. This applies the accepted stream-error policy: EPIPE terminates quietly by SIGPIPE; other flush failures use the existing diagnostic/status 1. This intentionally changes the exit status when earlier buffered output fails. Healthy output, requested exit codes, quiet exit behavior and invalid-code validation remain intact.

Blast radius

Four files: one added line in bend2/comp.ts and an extension to the existing tests/io/stream_epipe.{bend,c,js} regression. The native Halt/Exit branch shares the flush; Halt already flushes through its error writer. No Base API, parser, device runtime, main-result semantics, gate or cap changes.

Regression evidence

The existing native pipe/fork fixture now resumes its first child into checked IO.print followed by IO.exit(7). It reports exit: 7 on main and exit: 141 with the fix, including under a PTY; removing only the flush restores the failure. Existing stdout/stderr cases remain. The JS twin continues to check its synchronous writer, which encounters EPIPE before exit.

Checks

  • Repository gate: 54/54; compiler 63,071/64,000 tokens with CI-pinned ttok 0.3.
  • Final regression passes source interpretation, emitted Bun JS and native execution. Focused native controls preserve healthy exit codes 0/7/255, reject 256, and preserve Halt behavior.
  • Nearby print, UTF-8, exit and main-result fixtures checked through source, emitted JS and available native lanes; Node controls also run.
  • git diff --check passes.

Validation limits

Local execution used macOS arm64, Bun 1.3.11, Node 26.8.2 and clang 23.1.3. Linux, Windows, GPU paths, installed compiled CLI and full cluster test/perf/ping/safe gates were not run; the cluster hostname is unavailable locally.

Node's stderr_failure fixture has the same pre-existing errno 2 versus No such file or directory output mismatch on main and this branch. Node stream_epipe was skipped because its existing twin requires bun:ffi; exit_status has no native twin. --check-only rejects these two foreign fixtures identically on main and this branch; their normal build and execution lanes pass.

@Lorenzobattistela Lorenzobattistela self-assigned this Oct 13, 2026
@Lorenzobattistela
Lorenzobattistela merged commit f60b6ee into bendlang:main Oct 13, 2026
1 check passed
@Lorenzobattistela Lorenzobattistela mentioned this pull request Oct 20, 2026
viridale added a commit to viridale/bend-cli that referenced this pull request Oct 20, 2026
Records lessons from commits since 9f7b840 that AGENTS.md lacked:
planting bugs against a new law; the kernel's missing eta and tab_cons;
ctorarms.py and charsplit.py for heads a wildcard arm refined;
chk-expected.tsv rows and CI cases for files reaching run.bend; #ifdef
CID guards in .c effects; consing then reversing over List.append;
Bool.or and Bool.and as eager; FID names from the harness path and _C
closures; peerbench.sh; test_tools.py; where the minimum Bend is pinned;
e2e's build/ inputs.

Updates what changed: PROOF.bend takes ~40 s and --verdict ~3 min; native
IO.exit on 2.0.37 exits with its code, not 141, after a print to a
closed pipe (checked today), so run.bend keeps Sys.exit until
bendlang/bend#1519 ships; kernel.sh retires with bendlang/bend#1536; 1.0
waits on bendlang/bend#1466, as the README now says.
viridale added a commit to viridale/bend-cli that referenced this pull request Oct 21, 2026
run.bend declared its own Sys.exit because Base's IO.exit, natively,
skipped the checked stdout flush: with stdout a closed pipe, `pkg run
exit 3` exited 3 where every other write ends the process by SIGPIPE.
bendlang/bend#1519, in Bend 2.0.38, flushes before the exit, so Run.exit
now calls IO.exit, and sys.c and sys.js keep only Sys.isatty and
Sys.columns.

exit_when, exit, finish, ended and stepped no longer rely on foreign
code, so run.bend and its importers list only Sys.isatty, Sys.columns,
host and main; CI's expected output and tools/chk-expected.tsv follow.
The README's platform note and AGENTS.md's IO.exit rule say so, and
tests/host.bend's header no longer claims to test an unreached effect.

A new CI step runs `pkg run exit 3`, native and under Bun, into a closed
pipe and requires status 141. With io_sync removed from the old
Sys.exit, the native build exits 3 there instead.

Checked on Bend 2.0.38, macOS arm64:
- tools/chk.sh: 20 ok, 0 with errors; CI's source-module check passes
- bend tests/e2e.bend: 121 runs passed
- Closed stdout: 141 native and under Bun; with stdout open, both exit 3
- Example apps native and under Bun, the TTY smoke test, the benchmark's
  equal stdout at one thread and all cores, and tests/complete/run.sh
  bash zsh fish man: pass
- No law, proof or benchmark imports run.bend.

Co-Authored-By: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants