Skip to content

fix(react-router): avoid unhandled rejections in turbo-stream decode on stream error - #15765

Merged
brophdawg11 merged 4 commits into
remix-run:mainfrom
EnesKartal:fix/turbo-stream-unhandled-rejection-15764
Nov 20, 2026
Merged

brophdawg11 merged 4 commits into
remix-run:mainfrom
EnesKartal:fix/turbo-stream-unhandled-rejection-15764

Conversation

@EnesKartal

Copy link
Copy Markdown
Contributor

Fixes #15764

Root Cause

In packages/react-router/vendor/turbo-stream-v2/turbo-stream.ts, when decode() encountered a stream error after the initial chunk:

  1. done.reject(reason) rejected done.promise, which had no rejection handler attached on the else branch because donePromise was reassigned to the .catch(...) chain.
  2. donePromise.then(() => reader.closed) was returned directly as done, so callers that only read decoded.value without awaiting decoded.done (such as single fetch) triggered a second unhandled rejection when reader.closed rejected.

Changes

  • Attach no-op .catch(() => {}) handlers to done.promise and the returned finished promise in decode() so stream errors do not trigger unhandled rejections when decoded.done is not awaited, while callers that await decoded.done still receive the rejection.
  • Add regression tests in packages/react-router/__tests__/vendor/turbo-stream-test.ts.

Verification

  • pnpm test packages/react-router/__tests__/vendor/turbo-stream-test.ts (240/240 passed)
  • pnpm exec eslint packages/react-router/vendor/turbo-stream-v2/turbo-stream.ts packages/react-router/__tests__/vendor/turbo-stream-test.ts (0 errors)
  • pnpm exec prettier --check packages/react-router/vendor/turbo-stream-v2/turbo-stream.ts packages/react-router/__tests__/vendor/turbo-stream-test.ts (passed)
  • pnpm --filter react-router typecheck (passed)

@brophdawg11

Copy link
Copy Markdown
Contributor

Pushed a follow-up in 0de8461 to simplify completion handling and cover an adjacent error path.

The original patch handles the reported unhandled completion rejections. The decoder also had a pre-existing problem when a deferred line was malformed or referenced an unknown ID: the catch handler rejected the private Deferred but returned normally, so the public done promise waited on reader.closed. If the stream stayed open, done never settled.

The follow-up removes that redundant private completion promise and uses the decodeDeferred chain directly. Errors still reject outstanding deferred values and now propagate through done immediately. A separate catch observes the returned completion promise without replacing it, so callers can ignore done safely while callers awaiting it still receive the original rejection. Existing deferred-value settlement observers remain intact.

I replaced the timing-based rejection tests with eight isolated subprocess regressions covering network errors, aborts, unknown deferred IDs, and malformed JSON, with completion both ignored and observed. All eight failed against the original main decoder and pass with this fix; the relevant vendor, <Await>, and server-runtime suites pass all 308 tests. Package build, typecheck, and formatting also passed.

Authored by an AI agent on behalf of @brophdawg11

@brophdawg11 brophdawg11 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the decoder completion change and the updated main integration. Decoder failures remain visible to callers awaiting done or deferred values, while ignored completion is handled. The isolated regressions cover network, abort, unknown deferred ID, and malformed JSON failures. All 308 relevant tests, package build, and typecheck passed locally on the updated branch.

Authored by an AI agent on behalf of @brophdawg11

@brophdawg11
brophdawg11 merged commit 9916920 into remix-run:main Nov 20, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Single fetch: a .data body cut after its first line gives two unhandled promise rejections (turbo-stream v2 decode)

2 participants