Skip to content

Commit 54e6d09

Browse files
steipetehugin-bot
andcommitted
fix(cron): exclude queued lane waits from agent setup timeouts
Co-authored-by: hugin-bot <[email protected]>
1 parent 8479063 commit 54e6d09

3 files changed

Lines changed: 65 additions & 13 deletions

File tree

‎src/cron/service/agent-watchdog.test.ts‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe("cron agent setup watchdog", () => {
2525
expect(watchdog.observedLaneWait()).toBe(false);
2626
});
2727

28-
it("keeps lane-wait evidence after setup timeout fires", async () => {
28+
it("starts setup timeout only after lane admission", async () => {
2929
vi.useFakeTimers();
3030
const triggerTimeout = vi.fn();
3131
const watchdog = createCronAgentWatchdog({
@@ -39,9 +39,42 @@ describe("cron agent setup watchdog", () => {
3939

4040
await vi.advanceTimersByTimeAsync(CRON_AGENT_SETUP_WATCHDOG_MS + 1);
4141

42+
expect(triggerTimeout).not.toHaveBeenCalled();
43+
expect(watchdog.observedLaneWait()).toBe(true);
44+
4245
watchdog.noteLaneAdmitted();
4346

47+
expect(triggerTimeout).not.toHaveBeenCalled();
48+
expect(watchdog.observedLaneWait()).toBe(false);
49+
50+
await vi.advanceTimersByTimeAsync(CRON_AGENT_SETUP_WATCHDOG_MS + 1);
51+
4452
expect(triggerTimeout).toHaveBeenCalledTimes(1);
45-
expect(watchdog.observedLaneWait()).toBe(true);
53+
expect(watchdog.observedLaneWait()).toBe(false);
54+
});
55+
56+
it("restarts the complete setup budget after repeated lane contention", async () => {
57+
vi.useFakeTimers();
58+
const triggerTimeout = vi.fn();
59+
const watchdog = createCronAgentWatchdog({
60+
deferUntilRunner: true,
61+
jobTimeoutMs: CRON_AGENT_SETUP_WATCHDOG_MS * 2,
62+
triggerTimeout,
63+
});
64+
65+
watchdog.start();
66+
for (let attempt = 0; attempt < 12; attempt += 1) {
67+
watchdog.noteLaneWait();
68+
await vi.advanceTimersByTimeAsync(CRON_AGENT_SETUP_WATCHDOG_MS * 2);
69+
expect(triggerTimeout).not.toHaveBeenCalled();
70+
watchdog.noteLaneAdmitted();
71+
await vi.advanceTimersByTimeAsync(CRON_AGENT_SETUP_WATCHDOG_MS - 1);
72+
expect(triggerTimeout).not.toHaveBeenCalled();
73+
}
74+
75+
await vi.advanceTimersByTimeAsync(1);
76+
77+
expect(triggerTimeout).toHaveBeenCalledTimes(1);
78+
expect(watchdog.observedLaneWait()).toBe(false);
4679
});
4780
});

‎src/cron/service/agent-watchdog.ts‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export function createCronAgentWatchdog(params: {
6969
let preExecutionTimeoutId: NodeJS.Timeout | undefined;
7070
let activeExecution: CronAgentExecutionStarted | undefined;
7171
let observedLaneWait = false;
72+
let waitingForLane = false;
7273

7374
const setTimedOut = (reason: string) => {
7475
if (state === "timed_out" || state === "disposed") {
@@ -92,6 +93,17 @@ export function createCronAgentWatchdog(params: {
9293
clearTimeout(setupTimeoutId);
9394
setupTimeoutId = undefined;
9495
};
96+
// Queue contention is not runner setup; admission must begin a fresh setup budget.
97+
const startSetupTimeout = () => {
98+
if (setupTimeoutId || state !== "waiting_for_runner" || waitingForLane) {
99+
return;
100+
}
101+
setupTimeoutId = setTimeout(() => {
102+
if (state === "waiting_for_runner" && !waitingForLane) {
103+
setTimedOut(setupTimeoutErrorMessage(activeExecution));
104+
}
105+
}, CRON_AGENT_SETUP_WATCHDOG_MS);
106+
};
95107
const clearPreExecutionTimeout = () => {
96108
if (!preExecutionTimeoutId) {
97109
return;
@@ -138,23 +150,23 @@ export function createCronAgentWatchdog(params: {
138150
return {
139151
start: () => {
140152
if (params.deferUntilRunner) {
141-
setupTimeoutId = setTimeout(() => {
142-
if (state === "waiting_for_runner") {
143-
setTimedOut(setupTimeoutErrorMessage(activeExecution));
144-
}
145-
}, CRON_AGENT_SETUP_WATCHDOG_MS);
153+
startSetupTimeout();
146154
return;
147155
}
148156
startTimeout();
149157
},
150158
noteLaneWait: () => {
151159
if (state === "waiting_for_runner") {
152160
observedLaneWait = true;
161+
waitingForLane = true;
162+
clearSetupTimeout();
153163
}
154164
},
155165
noteLaneAdmitted: () => {
156166
if (state === "waiting_for_runner") {
157167
observedLaneWait = false;
168+
waitingForLane = false;
169+
startSetupTimeout();
158170
}
159171
},
160172
noteRunnerStarted: (info?: CronAgentExecutionStarted) => {

‎src/cron/service/timer.regression.test.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3039,7 +3039,7 @@ describe("cron service timer regressions", () => {
30393039
}
30403040
});
30413041

3042-
it("does not notify setup timeout for cron-nested lane contention", async () => {
3042+
it("does not spend setup timeout while waiting for cron-nested admission", async () => {
30433043
vi.useFakeTimers();
30443044
try {
30453045
const store = timerRegressionFixtures.makeStorePath();
@@ -3075,25 +3075,32 @@ describe("cron service timer regressions", () => {
30753075
cleanupTimedOutAgentRun: vi.fn(async () => {}),
30763076
onIsolatedAgentSetupTimeout,
30773077
runIsolatedAgentJob: vi.fn(async ({ onLaneWait }) => {
3078-
onLaneWait?.();
3078+
onLaneWait?.({ waiting: true });
30793079
return await enqueueCommandInLane(CommandLane.CronNested, async () => {
3080+
onLaneWait?.({ waiting: false });
30803081
return { status: "ok" as const, summary: "lane released" };
30813082
});
30823083
}),
30833084
});
30843085

30853086
const timerPromise = onTimer(state);
3087+
let timerSettled = false;
3088+
void timerPromise.then(() => {
3089+
timerSettled = true;
3090+
});
30863091
await vi.advanceTimersByTimeAsync(60_100);
30873092
now += 60_100;
3088-
await timerPromise;
30893093

3094+
expect(timerSettled).toBe(false);
30903095
expect(onIsolatedAgentSetupTimeout).not.toHaveBeenCalled();
3091-
const job = requireJob(state, cronJob.id);
3092-
expect(job.state.lastStatus).toBe("error");
3093-
expect(job.state.lastError).toContain("setup timed out before runner start");
30943096

30953097
releaseLane.resolve();
30963098
await laneBlocker;
3099+
await timerPromise;
3100+
3101+
const job = requireJob(state, cronJob.id);
3102+
expect(job.state.lastStatus).toBe("ok");
3103+
expect(job.state.lastError).toBeUndefined();
30973104
} finally {
30983105
vi.useRealTimers();
30993106
}

0 commit comments

Comments
 (0)