fix(query-broadcast-client-experimental): stop leaking postMessage rejections as unhandled errors #10569
main fromrefs/pull/10569/head·opened View on GitHubchanges#
fixes #10543 (also closes duplicate #10542).
BroadcastChannel.postMessage returns a Promise<void> that rejects when the payload cannot be structured-cloned - for example when a query's state.data, state.error, or queryKey contains a ReadableStream, File, Request/Response, function, or framework proxy (vue reactive, mobx observables). the three channel.postMessage(...) call sites inside the queryCache.subscribe callback never attached a .catch handler, so every non-cloneable update escaped as an unhandledrejection with a stack trace pointing only into node_modules/broadcast-channel/... and no queryHash context.
the fix wraps all three call sites in a single safePost helper that:
- attaches a
.catchso the rejection never bubbles as anunhandledrejection, even when a user-suppliedonBroadcastErroritself throws (the hook call is wrapped in try/catch and the handler error is logged in dev). - routes the failure with
{ type, queryHash, queryKey }metadata to a new optionalonBroadcastErrorhook onBroadcastQueryClientOptions, for consumers who want to forward to sentry/datadog. - falls back to a development-only
console.warnwhen no hook is provided, so cross-tab sync failures are never entirely silent.
the wire format of the broadcast messages is unchanged, so tabs running old and new code stay compatible during a roll-out. related: #8356 was closed with a user-side toRaw() workaround that does not generalize to ReadableStream / File / function payloads; this change supersedes that guidance at the library level.
checklist#
- i have followed the steps in the contributing guide.
- i have tested this locally - `test:lib`, `test:types`, `test:eslint`, `test:build`, and `test:knip` all pass for the affected package.
release impact#
- this change affects published code, and i have generated a changeset.
- this change is docs/ci/dev-only (no release).
Summary by CodeRabbit#
Bug Fixes
- Broadcast message failures (e.g., non-cloneable query values) no longer surface as unhandled errors; affected queries skip cross-tab sync while others continue.
New Features
- Optional onBroadcastError callback to handle per-query broadcast failures; if absent, a development-time console warning is emitted.
Documentation
- Added guidance on handling broadcast errors and an example wiring errors to a tracker.
Tests
- Added tests covering broadcast failure handling and dev-time warnings.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsdocumentation
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: query-broadcast-client-experimental
Actionable comments posted: 1
🧹 Nitpick comments (1)
docs/framework/react/plugins/broadcastQueryClient.md (1)
52-65: Optional: reference the exportedBroadcastErrorEventto avoid drift.The inlined event shape duplicates the exported
BroadcastErrorEventinterface frompackages/query-broadcast-client-experimental/src/index.ts(lines 10–14). If the event type grows a field, this docs snippet will silently fall out of sync. Since the surrounding snippet already usesQueryKeywithout importing it, it's fine to keep it type-level and point at the exported name.📚 Proposed tweak
- /** Called when a query event cannot be broadcast to other tabs — - * most commonly because the query's `state.data`, `state.error`, or - * `queryKey` contains a value the structured-clone algorithm cannot - * serialize (e.g. `ReadableStream`, `File`, functions, framework - * proxies). Useful for routing failures to an error tracker. */ - onBroadcastError?: ( - error: unknown, - event: { - type: 'added' | 'removed' | 'updated' - queryHash: string - queryKey: QueryKey - }, - ) => void + /** Called when a query event cannot be broadcast to other tabs — + * most commonly because the query's `state.data`, `state.error`, or + * `queryKey` contains a value the structured-clone algorithm cannot + * serialize (e.g. `ReadableStream`, `File`, functions, framework + * proxies). Useful for routing failures to an error tracker. + * See `BroadcastErrorEvent`. */ + onBroadcastError?: (error: unknown, event: BroadcastErrorEvent) => void🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@docs/framework/react/plugins/broadcastQueryClient.md` around lines 52 - 65, The docs inline event type for onBroadcastError duplicates the exported BroadcastErrorEvent interface and may drift; update the onBroadcastError signature to reference the exported BroadcastErrorEvent (instead of the inline object type) so it stays in sync with packages/query-broadcast-client-experimental's BroadcastErrorEvent, keeping QueryKey as-is and preserving the parameter order (error: unknown, event: BroadcastErrorEvent) in the onBroadcastError declaration.🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed. Inline comments: In `@packages/query-broadcast-client-experimental/src/index.ts`: - Around line 69-90: The .catch in safePost may reintroduce unhandled rejections if onBroadcastError throws; modify the catch block in safePost to invoke onBroadcastError inside a try/catch and swallow or locally log any error thrown by the user handler so the promise chain never rejects due to a user callback (keep the existing process.env.NODE_ENV console.warn behavior for non-production); reference safePost, onBroadcastError, and the constructed event object when locating the change. Also add a test variant to the existing "does not surface broadcast failures as unhandled rejections" test that passes an onBroadcastError which throws to assert no unhandled rejection occurs. --- Nitpick comments: In `@docs/framework/react/plugins/broadcastQueryClient.md`: - Around line 52-65: The docs inline event type for onBroadcastError duplicates the exported BroadcastErrorEvent interface and may drift; update the onBroadcastError signature to reference the exported BroadcastErrorEvent (instead of the inline object type) so it stays in sync with packages/query-broadcast-client-experimental's BroadcastErrorEvent, keeping QueryKey as-is and preserving the parameter order (error: unknown, event: BroadcastErrorEvent) in the onBroadcastError declaration.🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
ed021583-1d1a-4711-b89f-212c355bdeb9📥 Commits
Reviewing files that changed from the base of the PR and between 2f9527ed76306a0a73af452724da8906ad1c10aa and 288375600571d3af25eb7a5558f47ddb4687615e.
📒 Files selected for processing (4)
.changeset/fix-broadcast-client-postmessage.mddocs/framework/react/plugins/broadcastQueryClient.mdpackages/query-broadcast-client-experimental/src/__tests__/index.test.tspackages/query-broadcast-client-experimental/src/index.ts
packages/query-broadcast-client-experimental/src/index.ts lines 69 to 103Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -30,0 +70,21 @@70+ channel.postMessage(message).catch((error: unknown) => {71+ const event: BroadcastErrorEvent = {72+ type: message.type,73+ queryHash: message.queryHash,74+ queryKey: message.queryKey,75+ }76+77+ if (onBroadcastError) {78+ onBroadcastError(error, event)79+ return80+ }81+82+ if (process.env.NODE_ENV !== 'production') {83+ console.warn(84+ `[broadcastQueryClient] Failed to broadcast "${event.type}" event for query ${event.queryHash}. ` +85+ 'The query value could not be structured-cloned; cross-tab sync for this query was skipped.',86+ error,87+ )88+ }89+ })90+ }⚠️ Potential issue | 🟠 Major
A throwing
onBroadcastErrorreintroduces the unhandled rejection.onBroadcastErroris called inside the.catchhandler. If user code throws (e.g. a misconfigured Sentry wrapper, orevent.queryKeybeing touched on a framework proxy), the handler's return value becomes a rejected promise with no further.catch, which is exactly theunhandledrejectionthis PR is trying to eliminate. Worth guarding the user-land call so the guarantee holds even when the hook misbehaves.🛡️ Proposed fix
- if (onBroadcastError) { - onBroadcastError(error, event) - return - } - - if (process.env.NODE_ENV !== 'production') { + if (onBroadcastError) { + try { + onBroadcastError(error, event) + } catch (hookError) { + if (process.env.NODE_ENV !== 'production') { + console.warn( + `[broadcastQueryClient] onBroadcastError threw while handling "${event.type}" for query ${event.queryHash}.`, + hookError, + ) + } + } + return + } + + if (process.env.NODE_ENV !== 'production') { console.warn(Consider adding a test variant of the "does not surface broadcast failures as unhandled rejections" case where
onBroadcastErroritself throws.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/query-broadcast-client-experimental/src/index.ts` around lines 69 - 90, The .catch in safePost may reintroduce unhandled rejections if onBroadcastError throws; modify the catch block in safePost to invoke onBroadcastError inside a try/catch and swallow or locally log any error thrown by the user handler so the promise chain never rejects due to a user callback (keep the existing process.env.NODE_ENV console.warn behavior for non-production); reference safePost, onBroadcastError, and the constructed event object when locating the change. Also add a test variant to the existing "does not surface broadcast failures as unhandled rejections" test that passes an onBroadcastError which throws to assert no unhandled rejection occurs.✅ Addressed in commit cf838d6
🧹 Nitpick comments (2)
packages/query-broadcast-client-experimental/src/index.ts (1)
40-43: Optional: deriveBroadcastMessagefromBroadcastErrorEventto avoid drift.The three
type/queryHash/queryKeyfields are now duplicated betweenBroadcastMessage(internal wire shape) and the exportedBroadcastErrorEvent. They must stay in sync becausesafePostconstructs the latter from the former at lines 71–75. A small shared base would make future shape changes safer, but this is purely a nit on the current diff.♻️ Possible shape
-type BroadcastMessage = - | { type: 'added'; queryHash: string; queryKey: QueryKey } - | { type: 'removed'; queryHash: string; queryKey: QueryKey } - | { type: 'updated'; queryHash: string; queryKey: QueryKey; state: unknown } +type BroadcastMessage = + | ({ type: 'added' } & Omit<BroadcastErrorEvent, 'type'>) + | ({ type: 'removed' } & Omit<BroadcastErrorEvent, 'type'>) + | ({ type: 'updated'; state: unknown } & Omit<BroadcastErrorEvent, 'type'>)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/query-broadcast-client-experimental/src/index.ts` around lines 40 - 43, BroadcastMessage duplicates fields with the exported BroadcastErrorEvent; unify them to avoid drift by extracting a shared base type (e.g., BroadcastBase or reuse BroadcastErrorEvent's common fields) and have BroadcastMessage extend that base (or derive BroadcastMessage from BroadcastErrorEvent) so the common properties type/queryHash/queryKey are defined once; update the union variants (types 'added'|'removed'|'updated') to extend the shared base and ensure safePost (which constructs BroadcastErrorEvent from BroadcastMessage) still type-checks against the new shared type.packages/query-broadcast-client-experimental/src/__tests__/index.test.ts (1)
148-178: Tighten the assertion so the test distinguishes the two warn paths.Today this test only asserts
warn.toHaveBeenCalled(). Both theonBroadcastError threwbranch (lines 86–89 inindex.ts) and the no-hook fallback branch (lines 96–100) log throughconsole.warn, so a regression that made the fallback fire while the hook was also wired would still pass this test. Assert on the specific "onBroadcastError threw" message so the test actually proves the guarded branch ran.Also,
await new Promise(r => setTimeout(r, 20))here and at lines 140–141 is a timing gamble —vi.waitFor(() => expect(warn).toHaveBeenCalled())(for this test) and waiting onpostMessagecall count (for the previous one) would be deterministic; then a single trailing microtask flush is enough before theonUnhandledassertion.🧪 Proposed tweak
- await new Promise((resolve) => setTimeout(resolve, 20)) - - expect(onUnhandled).not.toHaveBeenCalled() - expect(warn).toHaveBeenCalled() + await vi.waitFor(() => { + expect(warn).toHaveBeenCalled() + }) + // Flush one more turn so any would-be unhandledRejection has fired. + await new Promise((resolve) => setImmediate(resolve)) + + expect(onUnhandled).not.toHaveBeenCalled() + expect(warn.mock.calls[0]?.[0]).toEqual( + expect.stringContaining('onBroadcastError threw'), + )🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/query-broadcast-client-experimental/src/__tests__/index.test.ts` around lines 148 - 178, Tighten the test to assert the specific "onBroadcastError threw" warning and make the wait deterministic: replace the loose await new Promise(setTimeout) with vi.waitFor(() => expect(warn).toHaveBeenCalledWith(expect.stringContaining('onBroadcastError threw'))) to ensure the guarded branch logged the specific message, then await Promise.resolve() (a single microtask flush) before asserting on onUnhandled; keep the existing console.warn spy and process.on/off setup and reference this change in the test named 'does not surface failures even when onBroadcastError itself throws' and the warn spy variable `warn`.🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed. Nitpick comments: In `@packages/query-broadcast-client-experimental/src/__tests__/index.test.ts`: - Around line 148-178: Tighten the test to assert the specific "onBroadcastError threw" warning and make the wait deterministic: replace the loose await new Promise(setTimeout) with vi.waitFor(() => expect(warn).toHaveBeenCalledWith(expect.stringContaining('onBroadcastError threw'))) to ensure the guarded branch logged the specific message, then await Promise.resolve() (a single microtask flush) before asserting on onUnhandled; keep the existing console.warn spy and process.on/off setup and reference this change in the test named 'does not surface failures even when onBroadcastError itself throws' and the warn spy variable `warn`. In `@packages/query-broadcast-client-experimental/src/index.ts`: - Around line 40-43: BroadcastMessage duplicates fields with the exported BroadcastErrorEvent; unify them to avoid drift by extracting a shared base type (e.g., BroadcastBase or reuse BroadcastErrorEvent's common fields) and have BroadcastMessage extend that base (or derive BroadcastMessage from BroadcastErrorEvent) so the common properties type/queryHash/queryKey are defined once; update the union variants (types 'added'|'removed'|'updated') to extend the shared base and ensure safePost (which constructs BroadcastErrorEvent from BroadcastMessage) still type-checks against the new shared type.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
eefd5dfc-a53a-4d1a-a8d2-d99555e36598📥 Commits
Reviewing files that changed from the base of the PR and between 288375600571d3af25eb7a5558f47ddb4687615e and cf838d698e73d94ca61483ec9a0492ad03230954.
📒 Files selected for processing (3)
docs/framework/react/plugins/broadcastQueryClient.mdpackages/query-broadcast-client-experimental/src/__tests__/index.test.tspackages/query-broadcast-client-experimental/src/index.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- docs/framework/react/plugins/broadcastQueryClient.md
TkDodo closed this
Sign in to join this conversation.
📝 Walkthrough
Walkthrough#
Adds guarded broadcasting: BroadcastChannel.postMessage calls are wrapped in a safePost helper that catches structured‑clone/postMessage rejections and forwards them to an optional onBroadcastError hook or emits a development-time console.warn, preventing unhandled rejections and skipping sync for the failing query.
Changes#
.changeset/fix-broadcast-client-postmessage.mdonBroadcastErroror devconsole.warn.docs/framework/react/plugins/broadcastQueryClient.mdonBroadcastErrorandBroadcastErrorEventdocs; new “Handling broadcast errors” section and example wiring (Sentry).packages/query-broadcast-client-experimental/src/index.tssafePostwrapper, catches postMessage rejections, emitsBroadcastErrorEvent, addsonBroadcastErroroption and exportsBroadcastErrorEventtype; replaces directpostMessagecalls withsafePost.packages/query-broadcast-client-experimental/src/__tests__/index.test.tsonBroadcastErrorinvocation and devconsole.warn, and ensures no unhandledRejection even if the hook throws.Sequence Diagram(s)#
Estimated code review effort#
🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem#
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
#10543by implementing the hookable approach: wrapping postMessage calls in safePost, adding onBroadcastError callback support, and emitting development-only console.warn messages with query context.✨ Finishing Touches
🧪 Generate unit tests (beta)
Warning
Review ran into problems#
🔥 Problems
Git: Failed to clone repository. Please run the
@coderabbitai full reviewcommand to re-trigger a full review. If the issue persists, setpath_filtersto include or exclude specific files.Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share