Skip to content

Commit 857432a

Browse files
committed
fix(broadcast-client): recover from errors thrown while applying an incoming cross-tab message
If applying an incoming cross-tab message (query.setState/queryCache.build, which notifies observers) throws, the internal `tx()` helper's `transaction` flag was never reset. This silently disabled all future outgoing broadcasts from this tab for the rest of the session. Guard with try/finally so a single failed message can't permanently break cross-tab sync.
1 parent 2fbe04e commit 857432a

3 files changed

Lines changed: 53 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/query-broadcast-client-experimental': patch
3+
---
4+
5+
fix(broadcast-client): recover from errors thrown while applying an incoming cross-tab message instead of permanently disabling this tab's own broadcasts

‎packages/query-broadcast-client-experimental/src/__tests__/index.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { QueryCache } from '@tanstack/query-core'
77

88
const mockPostMessage = vi.fn().mockResolvedValue(undefined)
99
const mockClose = vi.fn()
10+
let lastCreatedChannel: { onmessage: ((action: any) => void) | null }
1011

1112
vi.mock('broadcast-channel', async (importOriginal) => {
1213
const actual = await importOriginal()
@@ -16,6 +17,9 @@ vi.mock('broadcast-channel', async (importOriginal) => {
1617
onmessage = null
1718
postMessage = mockPostMessage
1819
close = mockClose
20+
constructor() {
21+
lastCreatedChannel = this
22+
}
1923
},
2024
}
2125
})
@@ -48,6 +52,40 @@ describe('broadcastQueryClient', () => {
4852
expect(queryCache.hasListeners()).toBe(false)
4953
})
5054

55+
describe('incoming message handling', () => {
56+
it('should keep broadcasting local changes after applying an incoming message throws', () => {
57+
broadcastQueryClient({
58+
queryClient,
59+
broadcastChannel: 'test_channel',
60+
})
61+
62+
// Simulate some other part of the app (e.g. another cache subscriber)
63+
// throwing synchronously while an incoming cross-tab message is being
64+
// applied locally.
65+
const explodingUnsubscribe = queryCache.subscribe(() => {
66+
throw new Error('boom from an unrelated cache listener')
67+
})
68+
69+
expect(() => {
70+
lastCreatedChannel.onmessage?.({
71+
type: 'added',
72+
queryHash: '["remote"]',
73+
queryKey: ['remote'],
74+
state: { data: 1 },
75+
})
76+
}).toThrow('boom')
77+
78+
explodingUnsubscribe()
79+
mockPostMessage.mockClear()
80+
81+
// A later local change must still be broadcast to other tabs, instead
82+
// of being silently swallowed because the transaction flag got stuck.
83+
queryClient.setQueryData(['local'], { value: 1 })
84+
85+
expect(mockPostMessage).toHaveBeenCalled()
86+
})
87+
})
88+
5189
describe('postMessage error handling', () => {
5290
let originalEnv: string | undefined
5391

‎packages/query-broadcast-client-experimental/src/index.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,16 @@ export function broadcastQueryClient({
5555
let transaction = false
5656
const tx = (cb: () => void) => {
5757
transaction = true
58-
cb()
59-
transaction = false
58+
try {
59+
cb()
60+
} finally {
61+
// Guard against `cb` throwing (e.g. `query.setState`/`queryCache.build`
62+
// triggering a listener that throws while applying an incoming
63+
// cross-tab message). Without this, `transaction` would stay `true`
64+
// forever, silently disabling this tab's own broadcasts to other tabs
65+
// for the rest of the session.
66+
transaction = false
67+
}
6068
}
6169

6270
const channel = new BroadcastChannel(broadcastChannel, {

0 commit comments

Comments
 (0)