Skip to content

Commit 2dc5dd8

Browse files
committed
1 parent 41dd999 commit 2dc5dd8

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

‎src/gateway/server-cron.drain.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,28 @@ describe("gateway cron stop-and-drain automation ownership", () => {
148148
}
149149
});
150150

151+
it("does not reopen the scheduler when a later stop wins a pending restart", async () => {
152+
const exitWatcherDrain = createDeferred();
153+
cancelAllMock.mockReturnValue(exitWatcherDrain.promise);
154+
stopAllMock.mockResolvedValue(undefined);
155+
const original = await startGatewayCron("exit-watcher-restart-cancelled");
156+
157+
try {
158+
original.state.cron.stop();
159+
const restart = original.state.cron.start();
160+
await vi.waitFor(() => expect(cancelAllMock).toHaveBeenCalledOnce());
161+
162+
original.state.cron.stop();
163+
exitWatcherDrain.resolve(undefined);
164+
await restart;
165+
166+
expect(sessionHasAutomation("agent:main:main", original.cfg)).toBe(false);
167+
} finally {
168+
exitWatcherDrain.resolve(undefined);
169+
await cleanGatewayCron(original);
170+
}
171+
});
172+
151173
it("unregisters a stopped scheduler when stream draining fails and permits retry", async () => {
152174
stopAllMock.mockRejectedValueOnce(new Error("stream drain failed"));
153175
stopAllMock.mockResolvedValue(undefined);

‎src/gateway/server-cron.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,10 +1439,16 @@ export function buildGatewayCronService(params: {
14391439
};
14401440
const startCron = cron.start.bind(cron);
14411441
cron.start = async () => {
1442-
const generation = streamWatcherGeneration;
1442+
const exitGeneration = exitWatcherGeneration;
1443+
const streamGeneration = streamWatcherGeneration;
1444+
const lifecycleChanged = () =>
1445+
exitGeneration !== exitWatcherGeneration || streamGeneration !== streamWatcherGeneration;
14431446
await exitWatchersStopPromise;
1447+
if (lifecycleChanged()) {
1448+
return;
1449+
}
14441450
await startCron();
1445-
if (generation !== streamWatcherGeneration) {
1451+
if (lifecycleChanged()) {
14461452
return;
14471453
}
14481454
exitWatchersStopped = false;
@@ -1451,15 +1457,15 @@ export function buildGatewayCronService(params: {
14511457
exitWatchersStopPromise = undefined;
14521458
streamWatchersStopPromise = undefined;
14531459
streamWatchersRef.current?.resume();
1454-
if (generation !== streamWatcherGeneration) {
1460+
if (lifecycleChanged()) {
14551461
return;
14561462
}
14571463
await reconcileStreamWatchers();
1458-
if (generation !== streamWatcherGeneration) {
1464+
if (lifecycleChanged()) {
14591465
return;
14601466
}
14611467
await reconcileHeartbeatJobs();
1462-
if (generation !== streamWatcherGeneration) {
1468+
if (lifecycleChanged()) {
14631469
return;
14641470
}
14651471
// Register only once started, under the build-time epoch, so a stale lazy

0 commit comments

Comments
 (0)