Bug 106% raceCondition

This commit is contained in:
chk
2026-06-05 05:46:59 +02:00
parent 787a710fb5
commit 6dfe42dd18
2 changed files with 82 additions and 0 deletions

View File

@@ -114,6 +114,30 @@ function createSnapshotRouter(go2rtcUrl) {
return res.status(503).json({ error: 'kein verwertbarer Hi-Res-Frame (Warmup-Timeout)' });
}
// Schritt 3: Warten bis cam_hires Producer gestoppt ist bevor Client cam0 reconnectet.
// Ohne dieses Warten: cam_hires-FFmpeg hält /dev/videoN noch offen, wenn startStream(cam)
// go2rtc's cam-Producer startet → Race, zwei FFmpeg auf demselben Device → 108% CPU.
{
const t2 = Date.now();
while (Date.now() - t2 < 5000) {
try {
const rp = await fetch(`${go2rtcUrl}/api/streams`, { signal: AbortSignal.timeout(1000) });
if (rp.ok) {
const ss = await rp.json();
const sh = ss[hiresId];
const nCh = sh ? (sh.consumers ?? []).length : 0;
const pHRunning = sh ? (sh.producers ?? []).some(p => (p.state ?? '') === 'running') : false;
if (nCh === 0 && !pHRunning) {
console.log(`[hires][${id}] cam_hires gestoppt nach ${Date.now() - t2}ms Gerät frei`);
break;
}
}
} catch (_e) { /* ignore */ }
await sleep(300);
}
await sleep(400); // Puffer: FFmpeg-Prozess-Exit bis Kernel Device-FD freigibt
}
console.log(`[hires][${id}] OK ${jpeg.length} bytes, Breite=${lastWidth}, Dauer=${Date.now() - t0}ms`);
res.set({
'Content-Type': 'image/jpeg',