Repository navigation
Description
Reproduction: https://github.com/luchsamapparat/writable-closed-reproduction
Summary
Since the writable-backpressure fix (#15071, shipped in @react-router/node >= 8.0.0), a client disconnecting while a response body is still streaming can crash the entire Node process (exit code 1). We hit this in production on 8.1.0 roughly every 20 minutes — single-machine Express app, SSE endpoints plus tablet clients that cancel in-flight .data navigations constantly. The same app on 7.15.1 never crashes.
Error: Writable closed before stream finished
at ServerResponse.onClose (@react-router/node/dist/index.js:160)
at Object.onceWrapper (node:events:622:28)
at ServerResponse.emit (node:events:520:35)
at emitCloseNT (node:_http_server:1066:10)
at Socket.onServerResponseClose (node:_http_server:285:5)
Environment: @react-router/node 8.1.0, @react-router/express 8.1.0, Node 24.11.1, Express 4.
Mechanism (from reading the 8.1.0 dist)
writeReadableStreamToWritable / writeAsyncIterableToWritable share monitorWritableError(writable):
- Client aborts →
'close'fires on theServerResponse→onClose()→reject(new Error('Writable closed before stream finished')). reject()setssettled, then callscleanup()— removing the monitor's'error'and'close'listeners from the writable — then rejects the (internally.catch-guarded) race promise.- The pump loop's
await writableError.race(...)rejects; the catch block callswritable.destroy(error). stream.destroy(err)emits'error'on the writable. The monitor's'error'listener was already removed in step 2, and neither Node's http stack nor Express attaches an'error'listener to aServerResponse— so this is an unhandled'error'event, which Node raises as anuncaughtExceptioncarrying the writable-close error. Default policy: print stack, exit 1.
The @react-router/express adapter's sendRemixResponse catch (if (isResponseClosed(res)) return) cannot help — the emission happens on the EventEmitter path, not the awaited promise chain.
Secondary escape, observed as unhandledRejection in some interleavings: when the writable-error promise wins the race, the losing reader.read() promise is orphaned; if the source stream subsequently errors (e.g. the request signal aborts the render), that orphaned promise rejects with no handler attached.
Reproduction
Express template + any streaming response (an SSE resource route, or a large document/.data response), then abruptly kill the client socket mid-stream, e.g.:
for i in $(seq 1 60); do
curl -sN http://localhost:3000/resources/events-stream & sleep 0.2; kill -9 $!
doneUnder repetition the server process exits 1 with the stack above. Version bisection: identical app code on 7.15.1 (pre-#15071) never crashes; 8.0.0/8.1.0 crash.
Suggested fix
In monitorWritableError, keep an 'error' listener attached for the writable's lifetime (or don't pass the error to destroy()), so writable.destroy(error) after cleanup can never emit an unhandled 'error'; and .catch(() => {}) the orphaned race-loser read. A client abort during streaming is normal operation for SSE/HTTP — it should never reach process-level fatality.
Workaround we ship meanwhile
process.on('unhandledRejection') / process.on('uncaughtException') handlers that survive exactly these two known error shapes (message match) and preserve fail-fast behavior for everything else. An app-level alternative is attaching a no-op 'error' listener to every ServerResponse.
Activity
github-actions commentedon Jul 6, 2026
👋 @mkromann, to align with our new Open Governance model, we are now requiring that all issues have a minimal and runnable reproduction. This should help us to focus on actionable issues and be more responsive to newly filed issues.
To get this re-opened, please add a reproduction to the Issue description and tag
@brophdawg11or@brookslybrandin a comment so we can re-open.Or, if this was closed by mistake and there is a valid reproduction, please ensure that it is linked in the Issue description and tag
@brophdawg11or@brookslybrandin a comment so we can re-open.If you have any questions, you can always reach out on Discord. Thanks again for providing feedback and helping us make React Router even better!
marilari88 commentedon Jul 7, 2026
We also detected the same error and have rolled back while awaiting a resolution to the issue.
Stack trace from Sentry:
luchsamapparat commentedon Jul 16, 2026
We noticed the same issue when our Kubernetes readiness check caused our app to crash after updating React Router.
@brophdawg11 @brookslybrand I've created a reproduction for this issue:
luchsamapparat/writable-closed-reproduction
github-actions commentedon Aug 28, 2026
🤖 Hello there,
We recently published version
8.3.1which involves this issue. If you'd like to take it for a test run please try it out and let us know what you think!Thanks!