From 1b459fbddff397ddad94b66bb56cc8a71ceb4ab0 Mon Sep 17 00:00:00 2001 From: AAA <35992542+TNTcraftHIM@users.noreply.github.com> Date: Tue, 24 Nov 2026 12:06:11 -0500 Subject: [PATCH] fix(watch): clear a retired source from its pending replacement If the old player ends while the source chooser is preparing another video, the request must no longer name that retired activity as its predecessor. Clear the matching selection reference at the player's existing retirement owner. The regression reproduces watch_identity_conflict before the fix and starts the replacement afterward. Full Web verification passes on the integrated tree. --- src/client/channel/session.ts | 4 ++++ tests/channel-session.test.ts | 39 ++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/client/channel/session.ts b/src/client/channel/session.ts index 2b67084ac..95c1bcd67 100644 --- a/src/client/channel/session.ts +++ b/src/client/channel/session.ts @@ -2711,6 +2711,10 @@ export class ChannelSession { const pageId = player.media.page?.id; debugEvent("channel", "watch-owner-retired", { activityId: id, reason, ...(pageId && { pageId }) }); } + if (player?.owner) { + const selection = this.sourceSelections.get(player.roomId); + if (selection?.replaceWatchId === id) delete selection.replaceWatchId; + } this.watchPlayers.delete(id); player?.close(); } diff --git a/tests/channel-session.test.ts b/tests/channel-session.test.ts index ded13bb0a..e53b40112 100644 --- a/tests/channel-session.test.ts +++ b/tests/channel-session.test.ts @@ -15414,4 +15414,41 @@ test("page owner and borrowed player each release their page at most once", () = releaseSecond(); releaseSecond(); expect(dispose).toHaveBeenCalledOnce(); -}); \ No newline at end of file +}); + +test('a watch that ends during source selection does not block the new share', async () => { + const { session, socket } = await setup(1000, false); + const incumbentVideo = new WatchTestVideo(); + const candidateVideo = new WatchTestVideo(); + vi.stubGlobal('document', { createElement: vi.fn().mockReturnValueOnce(incumbentVideo).mockReturnValue(candidateVideo) }); + const id = 'ended-watch'; + const incumbent = new WatchPlayer('first', true, new HTMLVideoMedia('https://media.test/old.mp4', 'file'), () => {}); + incumbent.mode = 'direct'; + (session as unknown as { watchPlayers: Map }).watchPlayers.set(id, incumbent); + let releaseReady!: () => void; + const readiness = new Promise(resolve => { releaseReady = resolve; }); + const ready = vi.spyOn(HTMLVideoMedia.prototype, 'ready').mockImplementation(() => readiness); + try { + const opening = session.startWatch('first', 'https://media.test/new.mp4').catch(error => error); + await vi.waitFor(() => expect(ready).toHaveBeenCalled()); + const stopping = session.stopWatch(id, 'media-closed'); + const stop = socket.sent.findLast(command => command.type === 'stop_watch')!; + socket.receive({ type: 'room_reply', roomId: 'first', requestId: stop.requestId, result: { type: 'done' } }); + await stopping; + + releaseReady(); + await vi.waitFor(() => expect(socket.sent.some(command => command.type === 'start_watch')).toBe(true)); + const start = socket.sent.findLast(command => command.type === 'start_watch')!; + if (start.replaceId) { + socket.receive({ type: 'room_reply', roomId: 'first', requestId: start.requestId, result: { type: 'error', code: 'watch_identity_conflict' } }); + } else { + socket.receive({ type: 'room_reply', roomId: 'first', requestId: start.requestId, result: { type: 'watch_ready', serverTime: 1000 } }); + await vi.waitFor(() => expect(socket.sent.some(command => command.type === 'update_watch' && command.id === start.id)).toBe(true)); + const update = socket.sent.findLast(command => command.type === 'update_watch' && command.id === start.id)!; + socket.receive({ type: 'room_reply', roomId: 'first', requestId: update.requestId, result: { type: 'done' } }); + } + expect(await opening).toBeUndefined(); + expect(start.replaceId).toBeUndefined(); + expect(session.getSnapshot().watchPlayers.get(start.id as string)?.owner).toBe(true); + } finally { releaseReady(); session.close(); } +});