Skip to content

The open-source app everyone uses to manage agents at work

fix(server): avoid hot restart shutdown deadlock (#9670)

Dotta263316609e09
diff --git a/server/src/index.ts b/server/src/index.ts
index 7499477f15..2755cc9177 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -64,6 +64,7 @@ import { getBoardClaimWarningUrl, initializeBoardClaimChallenge } from "./board-
import { maybePersistWorktreeRuntimePorts } from "./worktree-config.js";
import { initTelemetry, getTelemetryClient } from "./telemetry.js";
import { conflict } from "./errors.js";
+import { coordinateHeartbeatSchedulerShutdown } from "./shutdown.js";
import type {
InstanceDatabaseBackupRunResult,
InstanceDatabaseBackupTrigger,
@@ -1203,7 +1204,24 @@ export async function startServer(): Promise<StartedServer> {
clearInterval(heartbeatSchedulerInterval);
heartbeatSchedulerInterval = null;
}
- await waitForHeartbeatSchedulerIdle();
+
+ const heartbeatShutdown = await coordinateHeartbeatSchedulerShutdown({
+ signal,
+ prepareHotRestartShutdown,
+ waitForHeartbeatSchedulerIdle,
+ });
+ const skipHeartbeatDrain = heartbeatShutdown.hotRestart?.skipDrain === true;
+ if (skipHeartbeatDrain) {
+ logger.info(
+ { signal, hotRestart: heartbeatShutdown.hotRestart },
+ "hot-restart shutdown prepared; skipping heartbeat scheduler idle wait and graceful run drain",
+ );
+ } else if (heartbeatShutdown.preparationError) {
+ logger.error(
+ { err: heartbeatShutdown.preparationError, signal },
+ "hot-restart shutdown preparation failed; falling back to graceful heartbeat run drain",
+ );
+ }
const telemetryClient = getTelemetryClient();
if (telemetryClient) {
@@ -1211,19 +1229,6 @@ export async function startServer(): Promise<StartedServer> {
await telemetryClient.flush();
}
- let skipHeartbeatDrain = false;
- if (prepareHotRestartShutdown) {
- try {
- const hotRestart = await prepareHotRestartShutdown(signal);
- skipHeartbeatDrain = hotRestart.skipDrain;
- if (skipHeartbeatDrain) {
- logger.info({ signal, hotRestart }, "hot-restart shutdown prepared; skipping graceful heartbeat run drain");
- }
- } catch (err) {
- logger.error({ err, signal }, "hot-restart shutdown preparation failed; falling back to graceful heartbeat run drain");
- }
- }
-
if (!skipHeartbeatDrain && drainHeartbeatRunsForShutdown) {
try {
const drain = await drainHeartbeatRunsForShutdown(signal);
diff --git a/server/src/shutdown.test.ts b/server/src/shutdown.test.ts
new file mode 100644
index 0000000000..524cc2418c
--- /dev/null
+++ b/server/src/shutdown.test.ts
@@ -0,0 +1,94 @@
+import { describe, expect, it, vi } from "vitest";
+import { coordinateHeartbeatSchedulerShutdown } from "./shutdown.js";
+
+describe("coordinateHeartbeatSchedulerShutdown", () => {
+ it("captures a hot-restart snapshot without waiting for active scheduler work", async () => {
+ let snapshotCaptured = false;
+ const waitForHeartbeatSchedulerIdle = vi.fn(() => new Promise<void>(() => undefined));
+
+ const result = await coordinateHeartbeatSchedulerShutdown({
+ signal: "SIGTERM",
+ prepareHotRestartShutdown: vi.fn(async () => {
+ snapshotCaptured = true;
+ return { mode: "prepared" as const, skipDrain: true };
+ }),
+ waitForHeartbeatSchedulerIdle,
+ });
+
+ expect(snapshotCaptured).toBe(true);
+ expect(waitForHeartbeatSchedulerIdle).not.toHaveBeenCalled();
+ expect(result).toEqual({
+ hotRestart: { mode: "prepared", skipDrain: true },
+ preparationError: null,
+ waitedForSchedulerIdle: false,
+ });
+ });
+
+ it("preserves the scheduler idle wait for normal graceful shutdown", async () => {
+ let releaseScheduler!: () => void;
+ const schedulerIdle = new Promise<void>((resolve) => {
+ releaseScheduler = resolve;
+ });
+ const waitForHeartbeatSchedulerIdle = vi.fn(() => schedulerIdle);
+ let settled = false;
+
+ const shutdown = coordinateHeartbeatSchedulerShutdown({
+ signal: "SIGTERM",
+ prepareHotRestartShutdown: vi.fn(async () => ({
+ mode: "not_requested" as const,
+ skipDrain: false,
+ })),
+ waitForHeartbeatSchedulerIdle,
+ }).finally(() => {
+ settled = true;
+ });
+
+ await vi.waitFor(() => expect(waitForHeartbeatSchedulerIdle).toHaveBeenCalledOnce());
+ expect(settled).toBe(false);
+
+ releaseScheduler();
+
+ await expect(shutdown).resolves.toEqual({
+ hotRestart: { mode: "not_requested", skipDrain: false },
+ preparationError: null,
+ waitedForSchedulerIdle: true,
+ });
+ });
+
+ it("waits for scheduler idle when hot-restart preparation is unavailable", async () => {
+ const waitForHeartbeatSchedulerIdle = vi.fn(async () => undefined);
+
+ const result = await coordinateHeartbeatSchedulerShutdown({
+ signal: "SIGTERM",
+ prepareHotRestartShutdown: null,
+ waitForHeartbeatSchedulerIdle,
+ });
+
+ expect(waitForHeartbeatSchedulerIdle).toHaveBeenCalledOnce();
+ expect(result).toEqual({
+ hotRestart: null,
+ preparationError: null,
+ waitedForSchedulerIdle: true,
+ });
+ });
+
+ it("falls back to the scheduler idle wait when hot-restart preparation fails", async () => {
+ const preparationError = new Error("snapshot failed");
+ const waitForHeartbeatSchedulerIdle = vi.fn(async () => undefined);
+
+ const result = await coordinateHeartbeatSchedulerShutdown({
+ signal: "SIGTERM",
+ prepareHotRestartShutdown: vi.fn(async () => {
+ throw preparationError;
+ }),
+ waitForHeartbeatSchedulerIdle,
+ });
+
+ expect(waitForHeartbeatSchedulerIdle).toHaveBeenCalledOnce();
+ expect(result).toEqual({
+ hotRestart: null,
+ preparationError,
+ waitedForSchedulerIdle: true,
+ });
+ });
+});
diff --git a/server/src/shutdown.ts b/server/src/shutdown.ts
new file mode 100644
index 0000000000..a0e657eae7
--- /dev/null
+++ b/server/src/shutdown.ts
@@ -0,0 +1,41 @@
+type HotRestartShutdownPreparation = {
+ skipDrain: boolean;
+};
+
+export async function coordinateHeartbeatSchedulerShutdown<
+ TPreparation extends HotRestartShutdownPreparation,
+>(input: {
+ signal: "SIGINT" | "SIGTERM";
+ prepareHotRestartShutdown: ((signal: "SIGINT" | "SIGTERM") => Promise<TPreparation>) | null;
+ waitForHeartbeatSchedulerIdle: () => Promise<void>;
+}): Promise<{
+ hotRestart: TPreparation | null;
+ preparationError: unknown;
+ waitedForSchedulerIdle: boolean;
+}> {
+ let hotRestart: TPreparation | null = null;
+ let preparationError: unknown = null;
+
+ if (input.prepareHotRestartShutdown) {
+ try {
+ hotRestart = await input.prepareHotRestartShutdown(input.signal);
+ } catch (err) {
+ preparationError = err;
+ }
+ }
+
+ if (hotRestart?.skipDrain) {
+ return {
+ hotRestart,
+ preparationError,
+ waitedForSchedulerIdle: false,
+ };
+ }
+
+ await input.waitForHeartbeatSchedulerIdle();
+ return {
+ hotRestart,
+ preparationError,
+ waitedForSchedulerIdle: true,
+ };
+}