@@ -27,6 +27,8 @@ export interface RegistrationRunnerLaunch {
2727 readonly stderr : string ;
2828 readonly timedOut : boolean ;
2929 readonly spawnError ?: string ;
30+ /** Terminal interrupt that was forwarded to the runner tree, if any. */
31+ readonly interruptedSignal ?: string ;
3032}
3133
3234/** Launch shape shared with the supervised spawn so tests can observe it. */
@@ -78,6 +80,7 @@ export const materializeRegistrationRunnerCache = async (
7880 } ) ;
7981 return launch . timedOut ||
8082 launch . spawnError !== undefined ||
83+ launch . interruptedSignal !== undefined ||
8184 launch . exitCode !== 0
8285 ? { status : "failed" , reason : prewarmFailureReason ( launch ) }
8386 : { status : "warmed" } ;
@@ -115,35 +118,64 @@ export const launchRegistrationRunnerCommand: RegistrationRunnerLauncher = (
115118 env : options . env ,
116119 cwd : options . cwd ,
117120 stdio : [ "ignore" , "pipe" , "pipe" ] ,
118- // POSIX runs the runner in its own group so one signal stops the tree.
121+ // POSIX runs the runner in its own group so one signal stops the tree;
122+ // an interrupt must therefore be forwarded below, because the detached
123+ // group no longer receives the terminal's signal.
119124 ...( options . platform === "win32" ? { } : { detached : true } ) ,
120125 } ) ;
121126 const stdout = boundedStream ( child . stdout ) ;
122127 const stderr = boundedStream ( child . stderr ) ;
123128 let timedOut = false ;
124129 let spawnError : string | undefined ;
130+ let interruptedSignal : string | undefined ;
125131 const timer = setTimeout ( ( ) => {
126132 timedOut = true ;
127133 void stopRunnerTree ( child , options . platform ) . finally ( ( ) => {
128134 child . kill ( "SIGKILL" ) ;
129135 } ) ;
130136 } , options . timeoutMs ) ;
137+ const detachSignalForwarding = ( ) => {
138+ for ( const [ signal , handler ] of signalHandlers )
139+ process . removeListener ( signal , handler ) ;
140+ } ;
141+ const signalHandlers = new Map < NodeJS . Signals , ( ) => void > (
142+ FORWARD_SIGNALS . map ( ( signal ) => [
143+ signal ,
144+ ( ) => {
145+ if ( interruptedSignal !== undefined ) return ;
146+ interruptedSignal = signal ;
147+ clearTimeout ( timer ) ;
148+ void stopRunnerTree ( child , options . platform ) . finally ( ( ) => {
149+ child . kill ( "SIGKILL" ) ;
150+ detachSignalForwarding ( ) ;
151+ // Restore default termination for the interrupt that arrived.
152+ process . kill ( process . pid , signal ) ;
153+ } ) ;
154+ } ,
155+ ] ) ,
156+ ) ;
157+ for ( const [ signal , handler ] of signalHandlers ) process . on ( signal , handler ) ;
131158 child . on ( "error" , ( error : NodeJS . ErrnoException ) => {
132159 spawnError = error . message ;
133160 } ) ;
134161 child . on ( "close" , ( code , signal ) => {
135162 clearTimeout ( timer ) ;
163+ detachSignalForwarding ( ) ;
136164 resolve ( {
137165 exitCode : code ?? signal ,
138166 signal : signal ?? null ,
139167 stdout : stdout ( ) ,
140168 stderr : stderr ( ) ,
141169 timedOut,
142170 ...( spawnError === undefined ? { } : { spawnError } ) ,
171+ ...( interruptedSignal === undefined ? { } : { interruptedSignal } ) ,
143172 } ) ;
144173 } ) ;
145174 } ) ;
146175
176+ /** Terminal interrupts that must also stop a detached package-runner tree. */
177+ const FORWARD_SIGNALS : readonly NodeJS . Signals [ ] = [ "SIGINT" , "SIGTERM" ] ;
178+
147179const boundedStream = (
148180 stream : NodeJS . ReadableStream | null ,
149181) : ( ( ) => string ) => {
@@ -159,11 +191,13 @@ const prewarmFailureReason = (launch: RegistrationRunnerLaunch): string => {
159191 const observed =
160192 launch . spawnError !== undefined
161193 ? launch . spawnError
162- : launch . timedOut
163- ? `the package runner timed out after the bounded prewarm window`
164- : `exit ${ launch . exitCode === null ? "unknown" : String ( launch . exitCode ) } ${
165- launch . stderr . trim ( ) === "" ? "" : `: ${ launch . stderr . trim ( ) } `
166- } `;
194+ : launch . interruptedSignal !== undefined
195+ ? `the package runner was stopped by ${ launch . interruptedSignal } `
196+ : launch . timedOut
197+ ? `the package runner timed out after the bounded prewarm window`
198+ : `exit ${ launch . exitCode === null ? "unknown" : String ( launch . exitCode ) } ${
199+ launch . stderr . trim ( ) === "" ? "" : `: ${ launch . stderr . trim ( ) } `
200+ } `;
167201 const normalized = observed
168202 . replace ( / \u001b (?: \[ [ 0 - 9 ; ? ] * [ - / ] * [ @ - ~ ] | \( [ 0 - 9 A - Z a - z ] | [ @ - Z \\ ^ _ ] ) / gu, "" )
169203 . replace ( / [ \u0000 - \u001f \u007f ] + / gu, " " )
0 commit comments