1111 * abort path's `clearTimeout` and only the abort case fails.
1212 */
1313import assert from 'node:assert/strict' ;
14+ import http from 'node:http' ;
15+ import { DAEMON_HTTP_INSTANCE_MISMATCH_HEADER } from '@agent-device/contracts/daemon-http' ;
1416import { test , vi } from 'vitest' ;
1517import { AppError , isRequestCanceledError } from '@agent-device/kernel/errors' ;
1618import { getRequestSignal } from '@agent-device/host-kit/request' ;
@@ -20,6 +22,7 @@ import { sendRequest } from '../daemon-client-transport.ts';
2022import { resolveDaemonPaths } from '../../daemon-resolution.ts' ;
2123import {
2224 closeLoopbackServer ,
25+ listenOnLoopback ,
2326 skipWhenLoopbackUnavailable ,
2427 trackLoopbackSockets ,
2528 type SkippableTestContext ,
@@ -111,3 +114,54 @@ test('an abort with a timeout armed clears the timer instead of running the time
111114test ( 'the same budget without a signal reaches the timeout seam' , async ( t ) => {
112115 await runTimerDiscipline ( t , 'timed-out' ) ;
113116} ) ;
117+
118+ test ( 'an abort during a restart health probe never retires the daemon or redispatches' , async ( t ) => {
119+ if ( await skipWhenLoopbackUnavailable ( t ) ) return ;
120+ handleRequestTimeoutCalls . length = 0 ;
121+ const controller = new AbortController ( ) ;
122+ let rpcCount = 0 ;
123+ let healthCount = 0 ;
124+ let reachedHealth ! : ( ) => void ;
125+ const healthStarted = new Promise < void > ( ( resolve ) => {
126+ reachedHealth = resolve ;
127+ } ) ;
128+ const server = http . createServer ( ( req , res ) => {
129+ if ( req . url === '/health' ) {
130+ healthCount += 1 ;
131+ reachedHealth ( ) ;
132+ return ;
133+ }
134+ rpcCount += 1 ;
135+ res . statusCode = 409 ;
136+ res . setHeader ( DAEMON_HTTP_INSTANCE_MISMATCH_HEADER , 'true' ) ;
137+ res . end ( ) ;
138+ } ) ;
139+ const destroySockets = trackLoopbackSockets ( server ) ;
140+ try {
141+ const port = await listenOnLoopback ( server ) ;
142+ const request = sendRequest (
143+ {
144+ baseUrl : `http://127.0.0.1:${ port } ` ,
145+ token : 'secret' ,
146+ pid : 1 ,
147+ remoteInstanceId : 'previous-instance' ,
148+ } ,
149+ { token : 'secret' , command : 'devices' , session : 'default' , positionals : [ ] , flags : { } } ,
150+ 'auto' ,
151+ STATE_PATHS ,
152+ 1_000 ,
153+ { signal : controller . signal } ,
154+ ) ;
155+ const rejection = assert . rejects ( request , ( error : unknown ) => isRequestCanceledError ( error ) ) ;
156+ await healthStarted ;
157+ controller . abort ( ) ;
158+ await rejection ;
159+ assert . equal ( rpcCount , 1 ) ;
160+ assert . equal ( healthCount , 1 ) ;
161+ assert . deepEqual ( handleRequestTimeoutCalls , [ ] ) ;
162+ } finally {
163+ controller . abort ( ) ;
164+ destroySockets ( ) ;
165+ await closeLoopbackServer ( server ) ;
166+ }
167+ } ) ;
0 commit comments