Skip to content

A closed stdout or stderr reader ends a program by SIGPIPE, not with an error (#1467) - #1470

Merged
Lorenzobattistela merged 1 commit into
bendlang:mainfrom
viridale:fix-1467-epipe-sigpipe
Oct 10, 2026
Merged

Lorenzobattistela merged 1 commit into
bendlang:mainfrom
viridale:fix-1467-epipe-sigpipe

Conversation

@viridale

@viridale viridale commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1467.

A program whose stdout or stderr reader exits early (./flood | head -1, bun flood.js | head -1, bend flood.bend | head -1) printed bend: a short write on a standard stream and exited 1. Now a write to a standard stream that fails with EPIPE ends the program by SIGPIPE, silently, the way yes | head -1 does: status 141 in a shell, still non-zero for pipefail. Any other write error (EBADF, ENOSPC, EIO) keeps the message and exit 1. Files and sockets keep answering Fail, because io_loop's signal(SIGPIPE, SIG_IGN) is untouched.

  • C: io_out and io_sync share a new io_lost. On EPIPE it restores SIGPIPE's default disposition and raises it; otherwise it calls err_fail as before. It is host-only (inside #if !DEVICE), like the rest of the IO runtime.
  • JS: Bun ignores SIGPIPE too, and JS has no signal(). Adding and then removing a listener restores SIG_DFL (onDidChangeListeners in BunProcess.cpp calls signal(n, SIG_DFL) when the count drops to 0; removeAllListeners takes that path, the same in Bun 1.2.23 and 1.4.0), so io_out does that and sends SIGPIPE to itself. The in-process bend f.bend run uses the same io_out.

tests/io/stream_epipe.bend has a .c and a .js twin. The C twin forks a child that moves fd 1, then fd 2, onto a pipe whose read end is already closed and writes a line through io_out/io_sync. The JS twin spawns a child bun that runs the runtime's own io_out (${io_out}) the same way. Each answers the child's status as a shell shows it, and expects 141. On main all three lanes print the message and 1 instead. Closing the read end before the write keeps it deterministic.

Verification (macOS arm64, Apple clang 21, Bun 1.2.23)

  • The issue's flood.bend: ./flood | head -1, bun flood.js | head -1, bun bend2/main.ts flood.bend | head -1 and ./flood 2>&1 | head -1 give 141 0 with nothing on stderr (main: 1 0 and the message). A full run still exits 0.
  • ./flood >&- (EBADF) still prints the message and exits 1.
  • tests/io and tests/halt, run locally the way one gates/test.ts shard runs them (checkup, then the C and JS lanes): PASS 196 / 196, the new test included.
  • bun gates/repo.ts: PASS 54 / 54. comp.ts is at 63,997 ttok of 64,000 (main: 63,905), so there are only 3 tokens of room. I shortened the comments to fit, and the merge queue will recount.

Not run: the cluster gates (test.ts on the minis, perf.ts) and Linux. The Bun 1.4.0 listener-removal path was checked in Bun's source, not run.

…an error (bendlang#1467)

A write to a standard stream whose reader has left (EPIPE) printed
"bend: a short write on a standard stream" and exited 1. It now ends the
program by SIGPIPE, silently, as `yes | head -1` does (status 141 in a
shell). Any other write error keeps the message and exit 1, and files and
sockets keep answering Fail: io_loop still ignores SIGPIPE.

C: io_out and io_sync share io_lost, which on EPIPE restores SIGPIPE's
default disposition and raises it. JS: Bun ignores SIGPIPE too, and
adding then removing a listener restores the default (Bun 1.2 and 1.4), so
io_out does that and signals itself.

tests/io/stream_epipe has each lane's child write through the runtime's
own io_out to a pipe whose read end is closed and print its status.
@viridale
viridale force-pushed the fix-1467-epipe-sigpipe branch from 14ddd00 to f730331 Compare October 10, 2026 03:04
@Lorenzobattistela
Lorenzobattistela merged commit 2343aec into bendlang:main Oct 10, 2026
1 check passed
@viridale
viridale deleted the fix-1467-epipe-sigpipe branch October 10, 2026 18:26
@Lorenzobattistela Lorenzobattistela mentioned this pull request Oct 13, 2026
viridale added a commit to viridale/bend-cli that referenced this pull request Oct 20, 2026
2.0.37, the minimum Bend, includes bendlang/bend#1470, so a closed
stdout pipe ends the program by SIGPIPE (status 141, nothing on stderr),
natively and under Bun, including through io_sync before exit. The
README's note for older releases goes.

It also adds Base's IO.exit (#1492), which exits silently with 0..255,
while main's U32 result stays ignored by design; AGENTS.md now names it.
bendlang/bend#1463 was closed by #1558, which keeps the unused-foreign-
import failure by design, so AGENTS.md cites that instead.
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.

A program whose stdout reader exits early prints bend: a short write on a standard stream and exits 1

2 participants