fix(mitm): recover from stale lock file on server start

Detect dead PID in lock file and reclaim it instead of failing, and drop unused fs dependency.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
decolua
2026-07-07 11:44:20 +07:00
parent 1885ad7f64
commit da0149de97
2 changed files with 9 additions and 4 deletions

View File

@@ -24,7 +24,6 @@
"bcryptjs": "^3.0.3",
"confbox": "^0.2.4",
"express": "^5.2.1",
"fs": "^0.0.1-security",
"http-proxy-middleware": "^3.0.5",
"jose": "^6.1.3",
"marked": "^18.0.1",

View File

@@ -496,9 +496,15 @@ async function startServer(apiKey, sudoPassword, forceKillPort443 = false) {
fs.writeFileSync(LOCK_FILE, String(process.pid), { flag: "wx" });
} catch (e) {
if (e.code === "EEXIST") {
throw new Error("MITM server is already starting (lock contention)");
}
throw e;
let stale = false;
try {
const pid = parseInt(fs.readFileSync(LOCK_FILE, "utf-8").trim(), 10);
stale = !pid || !isProcessAlive(pid);
} catch { stale = true; } // unreadable lock → treat as stale
if (!stale) throw new Error("MITM server is already starting (lock contention)");
try { fs.unlinkSync(LOCK_FILE); } catch { /* ignore */ }
fs.writeFileSync(LOCK_FILE, String(process.pid), { flag: "wx" });
} else throw e;
}
try {