fix(bidi): force restart on close conflict or orphan session recovery

This commit is contained in:
Matteo Benedetto
2026-08-22 12:55:32 +02:00
parent 7dc7601102
commit 29b304537d
+12 -3
View File
@@ -366,15 +366,24 @@ async function ensureBridge(
ctx?: any, ctx?: any,
): Promise<void> { ): Promise<void> {
if (await bridgeAlive()) return; if (await bridgeAlive()) return;
if (!(await isPortOpen(9222))) {
if (onConflict === "close" || !(await isPortOpen(9222))) {
await startFirefox(profile, onConflict, ctx); await startFirefox(profile, onConflict, ctx);
} else { } else {
log("Firefox già attivo su 9222: avvio solo il bridge"); log("Firefox già attivo su 9222: avvio solo il bridge");
} }
await startBridgeProcess(); await startBridgeProcess();
if (await waitBridgeReady()) return; if (await waitBridgeReady(8_000)) return;
// Se fallisce e non è stato fatto un restart pulito, proviamo a sbloccare la sessione orfana
log("Bridge non pronto, tentativo recupero sessione orfana riavviando Firefox...");
await startFirefox(profile, "close", ctx);
await startBridgeProcess();
if (await waitBridgeReady(15_000)) return;
throw new Error( throw new Error(
`Bridge BiDi non stabile per ${BRIDGE_STABLE_MS}ms. Nessun riavvio automatico di Firefox eseguito. ` + `Bridge BiDi non stabile per ${BRIDGE_STABLE_MS}ms. ` +
`Verifica ${BRIDGE_LOG} e lo stato della porta 9222.`, `Verifica ${BRIDGE_LOG} e lo stato della porta 9222.`,
); );
} }