From e611aeb74dc29b9e80ae656068a73a9b06ef3c4a Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Fri, 20 Nov 2026 01:15:46 -0500 Subject: [PATCH 1/2] Apply suggested fix to web/js/components/preact/HLSVideoCell.tsx from Copilot Autofix Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> --- web/js/components/preact/HLSVideoCell.tsx | 54 +++++++++++++++++++++-- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/web/js/components/preact/HLSVideoCell.tsx b/web/js/components/preact/HLSVideoCell.tsx index b1a83007c..3d25ff646 100644 --- a/web/js/components/preact/HLSVideoCell.tsx +++ b/web/js/components/preact/HLSVideoCell.tsx @@ -738,6 +738,14 @@ export function HLSVideoCell({ // Auto-retry while the error overlay is visible — see WebRTCVideoCell for rationale. const autoRetryCountdown = useAutoRetry(privacyActive ? null : error, handleRetry); + const privacyRequestGenerationRef = useRef(0); + const streamIdentityRef = useRef(stream.name); + + useEffect(() => { + streamIdentityRef.current = stream.name; + privacyRequestGenerationRef.current += 1; + }, [stream.name]); + useEffect(() => { if (error && onTransportFailure) onTransportFailure(error); }, [error, onTransportFailure]); @@ -747,22 +755,41 @@ export function HLSVideoCell({ * The stream stays in the Live View grid under a privacy overlay. */ const handlePauseForPrivacy = async () => { + const requestStreamIdentity = stream.name; + const requestGeneration = ++privacyRequestGenerationRef.current; setIsTogglingEnabled(true); try { - const res = await fetch(`/api/streams/${encodeURIComponent(stream.name)}`, { + const res = await fetch(`/api/streams/${encodeURIComponent(requestStreamIdentity)}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ set_privacy_mode: true }), }); if (!res.ok) throw new Error(`HTTP ${res.status}`); + if ( + privacyRequestGenerationRef.current !== requestGeneration || + streamIdentityRef.current !== requestStreamIdentity + ) { + return; + } setPrivacyActive(true); setShowPrivacyConfirm(false); queryClient.invalidateQueries({ queryKey: ['streams'] }); } catch (err) { + if ( + privacyRequestGenerationRef.current !== requestGeneration || + streamIdentityRef.current !== requestStreamIdentity + ) { + return; + } showStatusMessage(`${t('live.pauseForPrivacy')}: ${(err as Error).message}`, 'error', 5000); setShowPrivacyConfirm(false); } finally { - setIsTogglingEnabled(false); + if ( + privacyRequestGenerationRef.current === requestGeneration && + streamIdentityRef.current === requestStreamIdentity + ) { + setIsTogglingEnabled(false); + } } }; @@ -770,20 +797,39 @@ export function HLSVideoCell({ * Resume stream from privacy mode — sets privacy_mode=false and re-starts the stream. */ const handleResumeFromPrivacy = async () => { + const requestStreamIdentity = stream.name; + const requestGeneration = ++privacyRequestGenerationRef.current; setIsTogglingEnabled(true); try { - const res = await fetch(`/api/streams/${encodeURIComponent(stream.name)}`, { + const res = await fetch(`/api/streams/${encodeURIComponent(requestStreamIdentity)}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ set_privacy_mode: false }), }); if (!res.ok) throw new Error(`HTTP ${res.status}`); + if ( + privacyRequestGenerationRef.current !== requestGeneration || + streamIdentityRef.current !== requestStreamIdentity + ) { + return; + } setPrivacyActive(false); queryClient.invalidateQueries({ queryKey: ['streams'] }); } catch (err) { + if ( + privacyRequestGenerationRef.current !== requestGeneration || + streamIdentityRef.current !== requestStreamIdentity + ) { + return; + } showStatusMessage(`${t('live.resumeStream')}: ${(err as Error).message}`, 'error', 5000); } finally { - setIsTogglingEnabled(false); + if ( + privacyRequestGenerationRef.current === requestGeneration && + streamIdentityRef.current === requestStreamIdentity + ) { + setIsTogglingEnabled(false); + } } }; From 8ed02f75034754cb1a8ae9113f2fff3db77ed599 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Fri, 20 Nov 2026 01:21:33 -0500 Subject: [PATCH 2/2] Ignore stale HLS privacy responses across streams --- web/js/components/preact/HLSVideoCell.tsx | 7 +++++-- web/tests/e2e/hlsRecovery.browser.spec.js | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/web/js/components/preact/HLSVideoCell.tsx b/web/js/components/preact/HLSVideoCell.tsx index 3d25ff646..0cbaea3c0 100644 --- a/web/js/components/preact/HLSVideoCell.tsx +++ b/web/js/components/preact/HLSVideoCell.tsx @@ -4,7 +4,7 @@ import { refreshStream } from '../../api/mediaLifecycle.ts'; * A self-contained component for displaying an HLS video stream */ -import { useState, useEffect, useRef } from 'preact/hooks'; +import { useState, useEffect, useLayoutEffect, useRef } from 'preact/hooks'; import { DetectionOverlay, drawDetectionsOnCanvas } from './DetectionOverlay.tsx'; import { SnapshotButton } from './SnapshotManager.tsx'; import { ManualRecordingButton } from './ManualRecordingButton.tsx'; @@ -741,9 +741,12 @@ export function HLSVideoCell({ const privacyRequestGenerationRef = useRef(0); const streamIdentityRef = useRef(stream.name); - useEffect(() => { + useLayoutEffect(() => { streamIdentityRef.current = stream.name; privacyRequestGenerationRef.current += 1; + setIsTogglingEnabled(false); + setShowPrivacyConfirm(false); + return () => { privacyRequestGenerationRef.current += 1; }; }, [stream.name]); useEffect(() => { diff --git a/web/tests/e2e/hlsRecovery.browser.spec.js b/web/tests/e2e/hlsRecovery.browser.spec.js index e3b29d866..055125dcf 100644 --- a/web/tests/e2e/hlsRecovery.browser.spec.js +++ b/web/tests/e2e/hlsRecovery.browser.spec.js @@ -36,6 +36,9 @@ beforeAll(async () => { HTMLMediaElement.prototype.load=()=>{}; window.fetch=async(input,options={})=>{ window.requests.push({url:String(input),signal:options.signal}); + if(options.method==='PUT'&&window.config.deferPrivacy) return new Promise(resolve=>{ + window.resolvePrivacyRequest=()=>{window.privacyResolved=true;resolve(new Response('',{status:200}));}; + }); if(window.config.deferManifest) return new Promise((resolve,reject)=>{ options.signal.addEventListener('abort',()=>{window.fetchAborted=true;reject(new DOMException('Aborted','AbortError'));},{once:true}); }); @@ -164,6 +167,25 @@ it('focuses the resume control when privacy mode starts active',async()=>{ } finally {await browser.close();} }); +it('ignores an old privacy response after the tile changes streams',async()=>{ + const {browser,page}=await openPlayer({privacyMode:true,deferPrivacy:true}); + try { + await page.locator('button[aria-label="live.resumeStream"]').click(); + await page.waitForFunction(()=>!!window.resolvePrivacyRequest); + await page.evaluate(()=>{ + window.config.streamName='second camera'; + window.config.streamId='second'; + window.mount(); + window.resolvePrivacyRequest(); + }); + await page.waitForFunction(()=>window.privacyResolved&& + document.querySelector('.video-cell')?.dataset.streamName==='second camera'); + expect(await page.locator('button[aria-label="live.resumeStream"]').isVisible()).toBe(true); + expect(await page.locator('button[aria-label="live.resumeStream"]').isEnabled()).toBe(true); + expect(await page.evaluate(()=>window.errors)).toEqual([]); + } finally {await browser.close();} +}); + it('resets fallback transport when the tile receives a different stream',async()=>{ const {browser,page}=await openPlayer({enabled:false}); try {