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.
This commit is contained in:
AAA committed 2026-11-24 12:06:11 -05:00
1 parent 5cce34641f
commit 1b459fbddf
2 files changed
+42 -1

No files matched your search

+4
View File
@@ -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();
}
+38 -1
View File
@@ -15414,4 +15414,41 @@ test("page owner and borrowed player each release their page at most once", () =
releaseSecond();
releaseSecond();
expect(dispose).toHaveBeenCalledOnce();
});
});
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<string, WatchPlayer> }).watchPlayers.set(id, incumbent);
let releaseReady!: () => void;
const readiness = new Promise<void>(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(); }
});