diff --git a/docs/operations/client-cache-and-storage-recovery.md b/docs/operations/client-cache-and-storage-recovery.md index cd432943..a600287a 100644 --- a/docs/operations/client-cache-and-storage-recovery.md +++ b/docs/operations/client-cache-and-storage-recovery.md @@ -54,7 +54,9 @@ session/account Query lifecycle, strict query policy와 Web Storage v2 lifecycle 2. `cache.operation.failed`와 `storage.operation.failed`의 allowlisted operation/outcome/reason만으로 범위를 좁힌다. 3. runtime status가 `ACTIVE_BROADCAST`, `ACTIVE_STORAGE_FALLBACK`, - `DEGRADED_LOCAL_ONLY`, `CLOSED` 중 무엇인지 확인한다. + `DEGRADED_STORAGE_FALLBACK`, `DEGRADED_LOCAL_ONLY`, `CLOSED` 중 무엇인지 + 확인한다. `DEGRADED_STORAGE_FALLBACK`은 BroadcastChannel open에 실패한 tab이다. + 다른 tab이 broadcast로만 보낸 event를 받지 못한다. 4. 현재 tab의 mutation 성공 후 local namespace가 stale 처리되는지 확인한다. 5. 다른 tab의 오래된 화면이 단순 stale 표시인지, 실제 server authorization 우회인지 분리한다. authorization 우회는 cache incident가 아니라 P1 security @@ -70,7 +72,7 @@ session/account Query lifecycle, strict query policy와 Web Storage v2 lifecycle - `BROADCAST_PUBLISH_FAILED` - `STORAGE_LISTENER_FAILED` - `STORAGE_PUBLISH_FAILED` -- runtime status `DEGRADED_LOCAL_ONLY` +- runtime status `DEGRADED_STORAGE_FALLBACK` 또는 `DEGRADED_LOCAL_ONLY` ### 확인 diff --git a/src/adapters/cross-context-invalidation/browser-cross-context-invalidation.ts b/src/adapters/cross-context-invalidation/browser-cross-context-invalidation.ts index a6743c3a..6872046a 100644 --- a/src/adapters/cross-context-invalidation/browser-cross-context-invalidation.ts +++ b/src/adapters/cross-context-invalidation/browser-cross-context-invalidation.ts @@ -13,6 +13,12 @@ import { export type CrossContextInvalidationStatus = | "ACTIVE_BROADCAST" | "ACTIVE_STORAGE_FALLBACK" + /** + * The browser has BroadcastChannel but this tab could not open it. Other + * tabs publish on BroadcastChannel only, so this tab misses their events + * while its own storage pulses still reach them. + */ + | "DEGRADED_STORAGE_FALLBACK" | "DEGRADED_LOCAL_ONLY" | "CLOSED"; @@ -176,6 +182,10 @@ export function createBrowserCrossContextInvalidation( let closed = false; let sequence = 0; let broadcast: BroadcastChannelFacade | undefined; + let broadcastOpenFailed = false; + // A failed postMessage moves publishing to storage but keeps the channel + // open, because other tabs keep publishing on it. + let broadcastPublishFailed = false; let storageListenerInstalled = false; let status: CrossContextInvalidationStatus = "DEGRADED_LOCAL_ONLY"; @@ -249,6 +259,7 @@ export function createBrowserCrossContextInvalidation( // Opening still falls back when provider cleanup is rejected. } } + broadcastOpenFailed = true; observe({ operation: "OPEN", outcome: "DEGRADED", @@ -261,14 +272,16 @@ export function createBrowserCrossContextInvalidation( function refreshStatus(): void { if (closed) { status = "CLOSED"; - } else if (broadcast) { + } else if (broadcast && !broadcastPublishFailed) { status = "ACTIVE_BROADCAST"; } else if ( dependencies.storage && dependencies.storageEvents && storageListenerInstalled ) { - status = "ACTIVE_STORAGE_FALLBACK"; + status = broadcastOpenFailed + ? "DEGRADED_STORAGE_FALLBACK" + : "ACTIVE_STORAGE_FALLBACK"; } else { status = "DEGRADED_LOCAL_ONLY"; } @@ -322,7 +335,7 @@ export function createBrowserCrossContextInvalidation( sequence = candidate.sequence; pruneTracking(emittedAt); - if (broadcast) { + if (broadcast && !broadcastPublishFailed) { try { broadcast.postMessage(candidate); observe({ @@ -342,7 +355,7 @@ export function createBrowserCrossContextInvalidation( transport: "BROADCAST", reason: "BROADCAST_PUBLISH_FAILED", }); - closeBroadcast(); + broadcastPublishFailed = true; refreshStatus(); } } @@ -413,7 +426,7 @@ export function createBrowserCrossContextInvalidation( reason: "STORAGE_CLEANUP_FAILED", }); } - status = "ACTIVE_STORAGE_FALLBACK"; + refreshStatus(); observe({ operation: "PUBLISH", outcome: "ACCEPTED", diff --git a/tests/unit/cross-tab-invalidation.test.ts b/tests/unit/cross-tab-invalidation.test.ts index dc0c944b..88df5162 100644 --- a/tests/unit/cross-tab-invalidation.test.ts +++ b/tests/unit/cross-tab-invalidation.test.ts @@ -523,7 +523,9 @@ describe("browser cross-context invalidation transport", () => { const received = vi.fn(); second.subscribe(received); - expect(second.getStatus()).toBe("ACTIVE_STORAGE_FALLBACK"); + // R-2.3. Other tabs publish on BroadcastChannel only, so a tab whose + // channel failed to open misses their events and must not look healthy. + expect(second.getStatus()).toBe("DEGRADED_STORAGE_FALLBACK"); expect(first.publish({ topic: TOPIC, topicVersion: 1 })).toEqual({ ok: true, transport: "STORAGE", @@ -534,11 +536,54 @@ describe("browser cross-context invalidation transport", () => { expect(firstStorage.storage.removeCount).toBe(1); expect(storageBus.values.has(STORAGE_PULSE_KEY)).toBe(false); expect(secondNetwork.channels).toHaveLength(0); + expect(second.publish({ topic: TOPIC, topicVersion: 1 })).toEqual({ + ok: true, + transport: "STORAGE", + }); + expect(second.getStatus()).toBe("DEGRADED_STORAGE_FALLBACK"); first.close(); second.close(); }); + it("keeps receiving broadcasts after its own broadcast publish fails", () => { + const network = new FakeBroadcastNetwork(); + const storageBus = new FakeStorageBus(); + const senderStorage = storageBus.createEndpoint(); + const receiverStorage = storageBus.createEndpoint(); + const sender = createBrowserCrossContextInvalidation( + dependencies("tab-a", { + createBroadcastChannel: network.createChannel, + storage: senderStorage.storage, + storageEvents: senderStorage.target, + }), + ); + const receiver = createBrowserCrossContextInvalidation( + dependencies("tab-b", { + createBroadcastChannel: network.createChannel, + storage: receiverStorage.storage, + storageEvents: receiverStorage.target, + }), + ); + network.channels[1]!.failPost = true; + expect( + receiver.publish({ topic: TOPIC, topicVersion: 1 }), + ).toEqual({ ok: true, transport: "STORAGE" }); + const delivered: CrossContextInvalidationDelivery[] = []; + receiver.subscribe((delivery) => delivered.push(delivery)); + + expect( + sender.publish({ topic: TOPIC, topicVersion: 1 }), + ).toEqual({ ok: true, transport: "BROADCAST" }); + + expect(delivered.map(({ transport }) => transport)).toEqual([ + "BROADCAST", + ]); + expect(receiver.getStatus()).toBe("ACTIVE_STORAGE_FALLBACK"); + sender.close(); + receiver.close(); + }); + it("rejects storage pulses from another or unknown storage area", () => { const storageBus = new FakeStorageBus(); const publisherStorage = storageBus.createEndpoint(); @@ -599,7 +644,7 @@ describe("browser cross-context invalidation transport", () => { }), ); - expect(runtime.getStatus()).toBe("ACTIVE_STORAGE_FALLBACK"); + expect(runtime.getStatus()).toBe("DEGRADED_STORAGE_FALLBACK"); expect(runtime.publish({ topic: TOPIC, topicVersion: 1 })).toEqual({ ok: false, reason: "TRANSPORT_UNAVAILABLE",