Skip to content

[kyo-system] a broken pipe fails the write, not the process - #1970

Merged
fwbrasil merged 1 commit into
mainfrom
kyo-system-broken-pipe
Sep 20, 2026
Merged

fwbrasil merged 1 commit into
mainfrom
kyo-system-broken-pipe

Conversation

@fwbrasil

Copy link
Copy Markdown
Collaborator

Problem

Process gives a child's piped stdin the contract java.io gives any stream: a write that cannot be delivered fails with an IOException. Two platforms broke it, in opposite directions.

On Scala Native nothing installs a SIGPIPE disposition, so its default action applies: a write to a pipe whose reader is gone ends the whole writing program. A caller that wrote to a child which had already exited lost its own process, with no exception to catch.

On Node the failure is asynchronous. A write to a torn-down stream is discarded silently, and the stream emits an 'error' event afterwards; an 'error' event with no listener is thrown as an uncaught exception, which ends the Node process. So the same write either vanished or killed the runtime, and never reported anything to the caller.

Node also misread a process that a signal killed. It records that death as a null exitCode with the signal in signalCode, so code consulting exitCode alone treated a dead process as still running: exitCode() answered Absent, and waitFor(timeout) attached an 'exit' listener to a process whose 'exit' event had already fired, so the wait hung until the timeout elapsed.

Solution

ProcessSignalPlatform carries the per-platform disposition, consulted once before the first child is spawned. The Native implementation ignores SIGPIPE, so the write fails with EPIPE and java.io reports it as an IOException. A handler the application installed itself is left in place, since SIGPIPE is ignored only where its disposition is still the default. The JVM implementation does nothing, because the JVM already ignores SIGPIPE from startup. A pipe has no per-descriptor opt-out the way a socket has SO_NOSIGPIPE, so the disposition is set for the process, as the JVM sets it.

On Node every spawned child's stdin gets an 'error' listener, which keeps a failed write from ending the process and leaves the stream merely destroyed. NodeOutputStream checks that state before each write and raises IOException, which is the contract the other platforms give.

exitCode() consults signalCode alongside exitCode, as isAlive already did, and waitFor(timeout) answers from that recorded state before attaching a listener.

Notes

New ProcessTest leaves cover a signalled process reporting its signal, a timed wait over an already-killed process, a piped stdin delivering bytes, a write after the child exited, and a write to a pipe the running child closed which must leave the parent alive.

Verified on JVM, JS and Native, 37 leaves each, and compiled on Wasm.

### Problem

`Process` gives a child's piped stdin the contract `java.io` gives any
stream: a write that cannot be delivered fails with an `IOException`.
Two platforms broke it, in opposite directions.

On Scala Native nothing installs a SIGPIPE disposition, so its default
action applies: a write to a pipe whose reader is gone ends the whole
writing program. A caller that wrote to a child which had already
exited lost its own process, with no exception to catch.

On Node the failure is asynchronous. A write to a torn-down stream is
discarded silently, and the stream emits an 'error' event afterwards;
an 'error' event with no listener is thrown as an uncaught exception,
which ends the Node process. So the same write either vanished or
killed the runtime, and never reported anything to the caller.

Node also misread a process that a signal killed. It records that death
as a null `exitCode` with the signal in `signalCode`, so code consulting
`exitCode` alone treated a dead process as still running: `exitCode()`
answered `Absent`, and `waitFor(timeout)` attached an 'exit' listener to
a process whose 'exit' event had already fired, so the wait hung until
the timeout elapsed.

### Solution

`ProcessSignalPlatform` carries the per-platform disposition, consulted
once before the first child is spawned. The Native implementation
ignores SIGPIPE, so the write fails with EPIPE and `java.io` reports it
as an `IOException`. A handler the application installed itself is left
in place, since SIGPIPE is ignored only where its disposition is still
the default. The JVM implementation does nothing, because the JVM
already ignores SIGPIPE from startup. A pipe has no per-descriptor
opt-out the way a socket has `SO_NOSIGPIPE`, so the disposition is set
for the process, as the JVM sets it.

On Node every spawned child's stdin gets an 'error' listener, which
keeps a failed write from ending the process and leaves the stream
merely destroyed. `NodeOutputStream` checks that state before each
write and raises `IOException`, which is the contract the other
platforms give.

`exitCode()` consults `signalCode` alongside `exitCode`, as `isAlive`
already did, and `waitFor(timeout)` answers from that recorded state
before attaching a listener.

### Notes

New `ProcessTest` leaves cover a signalled process reporting its signal,
a timed wait over an already-killed process, a piped stdin delivering
bytes, a write after the child exited, and a write to a pipe the running
child closed which must leave the parent alive.

Verified on JVM, JS and Native, 37 leaves each, and compiled on Wasm.
@fwbrasil
fwbrasil merged commit 93b02e9 into main Sep 20, 2026
13 of 14 checks passed
@fwbrasil
fwbrasil deleted the kyo-system-broken-pipe branch September 20, 2026 20:30
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.

1 participant