@@ -140,33 +140,147 @@ test.runIf(process.platform !== 'win32')(
140140) ;
141141
142142test . runIf ( process . platform !== 'win32' ) (
143- 'a cancellation arriving after the child was reaped signals no process group ' ,
143+ 'a deadline that fires after the child exited settles without waiting for the pipe holder ' ,
144144 async ( ) => {
145- // The direct child exits at once and leaves `sleep 0.4` draining the streams.
146- // Its pid — and therefore its process-group id — is reusable from the moment it
147- // is reaped, so the cancellation must not address that group any more.
148- let childReaped : ( ) => void = ( ) => { } ;
149- const reaped = new Promise < void > ( ( resolve ) => {
150- childReaped = resolve ;
151- } ) ;
145+ // The direct child is gone, so no kill can reach the descendant that inherited its
146+ // pipes, and `close` only arrives when that descendant finishes. Settlement has to
147+ // come from the deadline noticing an already-exited child.
148+ const startedAt = Date . now ( ) ;
149+ await assert . rejects (
150+ ( ) => runCmd ( '/bin/sh' , [ '-c' , 'sleep 2 & exit 0' ] , { timeoutMs : 100 } ) ,
151+ ( error : unknown ) => {
152+ assert . equal ( isCommandTimeoutError ( error ) , true ) ;
153+ return true ;
154+ } ,
155+ ) ;
156+ assert . ok ( Date . now ( ) - startedAt < 1_000 , 'settled only once the pipe holder finished' ) ;
157+ } ,
158+ 10_000 ,
159+ ) ;
160+
161+ // The kill paths below address a process group whose leader this worker already reaped,
162+ // and the hermetic signal setup ends a worker's authority over a pid at that moment.
163+ // So the group writes are intercepted here, which is the seam that setup points at for
164+ // a real kill path, and the probe answer is what each test is choosing between.
165+
166+ type GroupWrite = { readonly pid : number ; readonly signal : string | number } ;
167+
168+ function interceptGroupWrites ( probeAnswer : 'reachable' | 'gone' ) : {
169+ restore : ( ) => void ;
170+ signals : GroupWrite [ ] ;
171+ } {
172+ const original = process . kill . bind ( process ) ;
173+ const signals : GroupWrite [ ] = [ ] ;
174+ process . kill = ( ( pid : number , signal : string | number = 'SIGTERM' ) => {
175+ if ( pid >= 0 ) return original ( pid , signal as NodeJS . Signals ) ;
176+ if ( signal === 0 ) {
177+ if ( probeAnswer === 'gone' ) {
178+ throw Object . assign ( new Error ( 'no such process group' ) , { code : 'ESRCH' } ) ;
179+ }
180+ return true ;
181+ }
182+ signals . push ( { pid, signal } ) ;
183+ return true ;
184+ } ) as typeof process . kill ;
185+ return { signals, restore : ( ) => ( process . kill = original ) } ;
186+ }
187+
188+ test . runIf ( process . platform !== 'win32' ) (
189+ 'a detached deadline still kills the group its reaped child left behind' ,
190+ async ( ) => {
191+ const groupWrites = interceptGroupWrites ( 'reachable' ) ;
192+ let childPid = 0 ;
193+ try {
194+ const startedAt = Date . now ( ) ;
195+ await assert . rejects (
196+ ( ) =>
197+ runCmdStreaming ( '/bin/sh' , [ '-c' , 'sleep 2 & exit 0' ] , {
198+ detached : true ,
199+ timeoutMs : 100 ,
200+ onSpawn : ( child ) => {
201+ childPid = child . pid ?? 0 ;
202+ } ,
203+ } ) ,
204+ ( error : unknown ) => {
205+ assert . equal ( isCommandTimeoutError ( error ) , true ) ;
206+ return true ;
207+ } ,
208+ ) ;
209+ assert . ok ( Date . now ( ) - startedAt < 1_000 , 'settled only once the pipe holder finished' ) ;
210+ assert . deepEqual ( groupWrites . signals , [ { pid : - childPid , signal : 'SIGKILL' } ] ) ;
211+ } finally {
212+ groupWrites . restore ( ) ;
213+ }
214+ } ,
215+ 10_000 ,
216+ ) ;
217+
218+ test . runIf ( process . platform !== 'win32' ) (
219+ 'a detached deadline whose group is already gone signals nothing and still settles' ,
220+ async ( ) => {
221+ // A group with no members left has an id the kernel can hand to anyone, so a stale
222+ // deadline must not aim a signal at it.
223+ const groupWrites = interceptGroupWrites ( 'gone' ) ;
224+ try {
225+ const startedAt = Date . now ( ) ;
226+ await assert . rejects (
227+ ( ) => runCmd ( '/bin/sh' , [ '-c' , 'sleep 2 & exit 0' ] , { detached : true , timeoutMs : 100 } ) ,
228+ ( error : unknown ) => {
229+ assert . equal ( isCommandTimeoutError ( error ) , true ) ;
230+ return true ;
231+ } ,
232+ ) ;
233+ assert . ok ( Date . now ( ) - startedAt < 1_000 , 'settled only once the pipe holder finished' ) ;
234+ assert . deepEqual ( groupWrites . signals , [ ] ) ;
235+ } finally {
236+ groupWrites . restore ( ) ;
237+ }
238+ } ,
239+ 10_000 ,
240+ ) ;
241+
242+ test . runIf ( process . platform !== 'win32' ) (
243+ 'a request that was already canceled kills the command it arrives on' ,
244+ async ( ) => {
245+ // The kill is issued before the caller finishes wiring, so a settlement that read
246+ // the watcher mid-construction would fail here rather than at the next await.
152247 const controller = new AbortController ( ) ;
153- const running = runCmdStreaming ( '/bin/sh' , [ '-c' , 'sleep 0.4 &' ] , {
154- signal : controller . signal ,
155- detached : true ,
156- onSpawn : ( child ) => {
157- child . once ( 'exit' , childReaped ) ;
248+ controller . abort ( ) ;
249+ await assert . rejects (
250+ ( ) => runCmd ( '/bin/sh' , [ '-c' , 'sleep 5' ] , { signal : controller . signal } ) ,
251+ ( error : unknown ) => {
252+ assert . equal (
253+ ( error as { details ?: Record < string , unknown > } ) . details ?. reason ,
254+ 'request_canceled' ,
255+ ) ;
256+ return true ;
158257 } ,
159- } ) ;
160- const rejection = settledRejection ( running ) ;
258+ ) ;
259+ } ,
260+ 5_000 ,
261+ ) ;
161262
162- await reaped ;
263+ test . runIf ( process . platform !== 'win32' ) (
264+ 'a background request that was already canceled ends its wait' ,
265+ async ( ) => {
266+ const controller = new AbortController ( ) ;
163267 controller . abort ( ) ;
164- const outcome = await rejection ;
268+ const background = runCmdBackground ( '/bin/sh' , [ '-c' , 'sleep 5' ] , {
269+ signal : controller . signal ,
270+ } ) ;
165271
166- assert . ok ( outcome , 'a canceled command must not resolve' ) ;
167- const details = ( outcome . error as { details ?: Record < string , unknown > } ) . details ;
168- assert . equal ( details ?. reason , 'request_canceled' ) ;
272+ await assert . rejects (
273+ ( ) => background . wait ,
274+ ( error : unknown ) => {
275+ assert . equal (
276+ ( error as { details ?: Record < string , unknown > } ) . details ?. reason ,
277+ 'request_canceled' ,
278+ ) ;
279+ return true ;
280+ } ,
281+ ) ;
169282 } ,
283+ 5_000 ,
170284) ;
171285
172286test . runIf ( process . platform !== 'win32' ) (
0 commit comments