fix(gateway): enforce singleton lock

This commit is contained in:
Peter Steinberger
2026-01-21 22:46:19 +00:00
parent 529372f762
commit 05a254746e
5 changed files with 216 additions and 0 deletions

View File

@@ -45,6 +45,7 @@ async function main() {
{ startGatewayServer },
{ setGatewayWsLogStyle },
{ setVerbose },
{ acquireGatewayLock, GatewayLockError },
{ consumeGatewaySigusr1RestartAuthorization, isGatewaySigusr1RestartExternallyAllowed },
{ defaultRuntime },
{ enableConsoleCapture, setConsoleTimestampPrefix },
@@ -53,6 +54,7 @@ async function main() {
import("../gateway/server.js"),
import("../gateway/ws-logging.js"),
import("../globals.js"),
import("../infra/gateway-lock.js"),
import("../infra/restart.js"),
import("../runtime.js"),
import("../logging.js"),
@@ -103,6 +105,7 @@ async function main() {
if (token) process.env.CLAWDBOT_GATEWAY_TOKEN = token;
let server: Awaited<ReturnType<typeof startGatewayServer>> | null = null;
let lock: Awaited<ReturnType<typeof acquireGatewayLock>> | null = null;
let shuttingDown = false;
let forceExitTimer: ReturnType<typeof setTimeout> | null = null;
let restartResolver: (() => void) | null = null;
@@ -177,6 +180,15 @@ async function main() {
process.on("SIGUSR1", onSigusr1);
try {
try {
lock = await acquireGatewayLock();
} catch (err) {
if (err instanceof GatewayLockError) {
defaultRuntime.error(`Gateway start blocked: ${err.message}`);
process.exit(1);
}
throw err;
}
// eslint-disable-next-line no-constant-condition
while (true) {
try {
@@ -191,6 +203,7 @@ async function main() {
});
}
} finally {
await lock?.release();
cleanupSignals();
}
}