Repository navigation
[kyo-system] a broken pipe fails the write, not the process - #1970
Merged
Merged
Conversation
### 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Processgives a child's piped stdin the contractjava.iogives any stream: a write that cannot be delivered fails with anIOException. 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
exitCodewith the signal insignalCode, so code consultingexitCodealone treated a dead process as still running:exitCode()answeredAbsent, andwaitFor(timeout)attached an 'exit' listener to a process whose 'exit' event had already fired, so the wait hung until the timeout elapsed.Solution
ProcessSignalPlatformcarries the per-platform disposition, consulted once before the first child is spawned. The Native implementation ignores SIGPIPE, so the write fails with EPIPE andjava.ioreports it as anIOException. 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 hasSO_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.
NodeOutputStreamchecks that state before each write and raisesIOException, which is the contract the other platforms give.exitCode()consultssignalCodealongsideexitCode, asisAlivealready did, andwaitFor(timeout)answers from that recorded state before attaching a listener.Notes
New
ProcessTestleaves 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.