Skip to content

Client abort mid-stream can crash the Node process: unhandled 'error' from writable.destroy() after monitor cleanup (@react-router/node >= 8.0.0) #15287

Description

@mkromann

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):

  1. Client aborts → 'close' fires on the ServerResponse → onClose() → reject(new Error('Writable closed before stream finished')).
  2. reject() sets settled, then calls cleanup() — removing the monitor's 'error' and 'close' listeners from the writable — then rejects the (internally .catch-guarded) race promise.
  3. The pump loop's await writableError.race(...) rejects; the catch block calls writable.destroy(error).
  4. 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 a ServerResponse — so this is an unhandled 'error' event, which Node raises as an uncaughtException carrying 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 $!
done

Under 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

  1. added
    no-reproduction
    on Jul 6, 2026
  2. github-actions commented on Jul 6, 2026

    @github-actions
    Contributor

    👋 @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 @brophdawg11 or @brookslybrand in 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 @brophdawg11 or @brookslybrand in 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!

  3. removed
    no-reproduction
    on Jul 6, 2026
  4. marilari88 commented on Jul 7, 2026

    @marilari88

    We also detected the same error and have rolled back while awaiting a resolution to the issue.
    Stack trace from Sentry:

    Error: Writable closed before stream finished
        at ServerResponse.onClose (/app/node_modules/.pnpm/@react-router+node@8.1.0_react-router@8.1.0_react-dom@19.2.7_react@19.2.7__react@19.2.7__typescript@5.9.3/node_modules/@react-router/node/dist/index.js:160:26)
        at ? (/app/node_modules/.pnpm/@sentry+opentelemetry@10.63.0_@opentelemetry+api@1.9.1_@opentelemetry+core@2.6.1_@opent_8e67a417a9c005e269c59929f2bac8ba/node_modules/@sentry/opentelemetry/src/asyncLocalStorageContextManager.ts:110:48)
        at AsyncLocalStorage.run (node:internal/async_local_storage/async_context_frame:63:14)
        at SentryAsyncLocalStorageContextManager.with (/app/node_modules/.pnpm/@sentry+opentelemetry@10.63.0_@opentelemetry+api@1.9.1_@opentelemetry+core@2.6.1_@opent_8e67a417a9c005e269c59929f2bac8ba/node_modules/@sentry/opentelemetry/src/asyncLocalStorageContextManager.ts:74:36)
        at ServerResponse.contextWrapper (/app/node_modules/.pnpm/@sentry+opentelemetry@10.63.0_@opentelemetry+api@1.9.1_@opentelemetry+core@2.6.1_@opent_8e67a417a9c005e269c59929f2bac8ba/node_modules/@sentry/opentelemetry/src/asyncLocalStorageContextManager.ts:110:14)
        at Object.onceWrapper (node:events:630:28)
        at ServerResponse.emit (node:events:521:24)
        at emitCloseNT (node:_http_server:1076:10)
        at Socket.onServerResponseClose (node:_http_server:289:5)
        at Socket.emit (node:events:521:24)
    
  5. luchsamapparat commented on Jul 16, 2026

    @luchsamapparat
    Contributor

    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

  6. github-actions commented on Aug 28, 2026

    @github-actions
    Contributor

    🤖 Hello there,

    We recently published version 8.3.1 which 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions