fix: clean up session locks on exit (#2483) (thanks @janeexai)

This commit is contained in:
Shadow
2026-01-26 19:48:46 -06:00
parent 14f8acdecb
commit d8e5dd91ba
4 changed files with 131 additions and 33 deletions

View File

@@ -124,6 +124,11 @@ export async function acquireSessionWriteLock(params: {
*/
function releaseAllLocksSync(): void {
for (const [sessionFile, held] of HELD_LOCKS) {
try {
fsSync.closeSync(held.handle.fd);
} catch {
// Ignore close errors during cleanup - best effort
}
try {
fsSync.rmSync(held.lockPath, { force: true });
} catch {
@@ -147,13 +152,17 @@ function registerCleanupHandlers(): void {
// Handle SIGINT (Ctrl+C) and SIGTERM
const handleSignal = (signal: NodeJS.Signals) => {
releaseAllLocksSync();
// Remove our handler and re-raise signal for proper exit code
process.removeAllListeners(signal);
// Remove only our handlers and re-raise signal for proper exit code.
process.off("SIGINT", onSigInt);
process.off("SIGTERM", onSigTerm);
process.kill(process.pid, signal);
};
process.on("SIGINT", () => handleSignal("SIGINT"));
process.on("SIGTERM", () => handleSignal("SIGTERM"));
const onSigInt = () => handleSignal("SIGINT");
const onSigTerm = () => handleSignal("SIGTERM");
process.on("SIGINT", onSigInt);
process.on("SIGTERM", onSigTerm);
}
// Register cleanup handlers when module loads