diff --git a/extensions/firefox-bidi.ts b/extensions/firefox-bidi.ts index 587df56..3f9898f 100644 --- a/extensions/firefox-bidi.ts +++ b/extensions/firefox-bidi.ts @@ -366,15 +366,24 @@ async function ensureBridge( ctx?: any, ): Promise { if (await bridgeAlive()) return; - if (!(await isPortOpen(9222))) { + + if (onConflict === "close" || !(await isPortOpen(9222))) { await startFirefox(profile, onConflict, ctx); } else { log("Firefox già attivo su 9222: avvio solo il bridge"); } + 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( - `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.`, ); }