Skip to content

Commit 7566714

Browse files
committed
fix(daemon-client): report the caller deadline when the restart health probe runs to it
1 parent 1433e11 commit 7566714

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎src/daemon-client/__tests__/daemon-client-transport.test.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from 'node:assert/strict';
22
import http from 'node:http';
3-
import { test } from 'vitest';
3+
import { test, vi } from 'vitest';
44
import { AppError } from '@agent-device/kernel/errors';
55
import {
66
DAEMON_HTTP_INSTANCE_HEADER,
@@ -222,6 +222,35 @@ test('a delayed restart health probe stops at the RPC deadline without retrying'
222222
}
223223
});
224224

225+
test('a restart health probe that times out just before the RPC deadline still reports the deadline', async (t) => {
226+
if (await skipWhenLoopbackUnavailable(t)) return;
227+
let probing = false;
228+
const realNow = performance.now.bind(performance);
229+
const server = http.createServer((req, res) => {
230+
if (req.url === '/health') {
231+
probing = true;
232+
return;
233+
}
234+
res.statusCode = 409;
235+
res.setHeader(DAEMON_HTTP_INSTANCE_MISMATCH_HEADER, 'true');
236+
res.end();
237+
});
238+
const clock = vi
239+
.spyOn(performance, 'now')
240+
.mockImplementation(() => realNow() - (probing ? 2 : 0));
241+
try {
242+
const port = await listenOnLoopback(server);
243+
await assert.rejects(
244+
sendWithStaleInstance(port, 150),
245+
(error: unknown) =>
246+
error instanceof AppError && error.details?.reason === 'daemon_transport_timeout',
247+
);
248+
} finally {
249+
clock.mockRestore();
250+
await closeLoopbackServer(server);
251+
}
252+
});
253+
225254
test('proxy forwards cached upstream identity and rejects a restarted upstream before dispatch', async (t) => {
226255
if (await skipWhenLoopbackUnavailable(t)) return;
227256
let upstreamInstance = 'upstream-one';

‎src/daemon-client/daemon-client-transport.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,15 @@ async function retryAfterRemoteInstanceMismatch(
254254
timeoutMs,
255255
deadline,
256256
);
257+
const probeStartedAt = performance.now();
257258
const health = await readRemoteDaemonHealth(info, probeTimeoutMs);
259+
if (!health.reachable && probeRanToCallerDeadline(probeStartedAt, probeTimeoutMs)) {
260+
throw handleRequestTimeout({
261+
info,
262+
statePaths,
263+
...timeoutRequestContext(req, true, timeoutMs ?? 0),
264+
});
265+
}
258266
const remainingMs = remainingRemoteRequestTimeoutMs(info, req, statePaths, timeoutMs, deadline);
259267
if (!health.reachable) {
260268
throw new AppError('COMMAND_FAILED', 'Remote daemon is unavailable', {
@@ -272,6 +280,19 @@ async function retryAfterRemoteInstanceMismatch(
272280
}
273281
}
274282

283+
// Node timers may fire a few ms before performance.now() reaches the same instant.
284+
const PROBE_TIMER_SLOP_MS = 5;
285+
286+
function probeRanToCallerDeadline(
287+
probeStartedAt: number,
288+
probeTimeoutMs: number | undefined,
289+
): boolean {
290+
if (probeTimeoutMs === undefined || probeTimeoutMs > REMOTE_DAEMON_HEALTHCHECK_TIMEOUT_MS) {
291+
return false;
292+
}
293+
return performance.now() - probeStartedAt >= probeTimeoutMs - PROBE_TIMER_SLOP_MS;
294+
}
295+
275296
function remainingRemoteRequestTimeoutMs(
276297
info: DaemonInfo,
277298
req: DaemonRequest,

0 commit comments

Comments
 (0)