Skip to content

Commit 3088e00

Browse files
committed
feat(bridge): clean up mesh state on Claude Code exit
Register SIGTERM/SIGINT/SIGHUP handlers to set the agent offline and shut down the store before the process exits.
1 parent d9303ff commit 3088e00

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

src/bridges/claude-code/channel.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,31 @@ export async function run(): Promise<void> {
270270
defaultName: `claude-code-${nanoid(4)}`,
271271
});
272272
agentId = reg.agentId;
273+
274+
// -----------------------------------------------------------------------
275+
// Shutdown — clean up mesh state when Claude Code exits
276+
// -----------------------------------------------------------------------
277+
278+
async function shutdown(): Promise<void> {
279+
try {
280+
if (agentId !== undefined) {
281+
await store.setAgentOffline(agentId);
282+
}
283+
await store.shutdown();
284+
} catch {
285+
// best-effort — the process is exiting anyway
286+
}
287+
}
288+
289+
for (const signal of ["SIGTERM", "SIGINT", "SIGHUP"] as const) {
290+
process.on(signal, () => {
291+
void shutdown().finally(() => process.exit(0));
292+
});
293+
}
294+
295+
process.on("exit", () => {
296+
// Synchronous fallback if async shutdown didn't complete in time.
297+
// store.shutdown() is async but the coordinator socket unref() in
298+
// MeshStore.init() means the process won't hang on the way out.
299+
});
273300
}

0 commit comments

Comments
 (0)