@@ -79,17 +79,33 @@ function signalHostCommandProcess(pid: number | undefined, signal: NodeJS.Signal
7979 if ( ! pid ) {
8080 return ;
8181 }
82- try {
83- if ( process . platform === "win32" ) {
82+ if ( process . platform === "win32" ) {
83+ try {
8484 process . kill ( pid , signal ) ;
85- } else {
86- process . kill ( - pid , signal ) ;
85+ } catch ( error ) {
86+ const code = ( error as NodeJS . ErrnoException ) . code ;
87+ if ( code !== "ESRCH" ) {
88+ warn ( `failed to send ${ signal } to host command process ${ pid } : ${ code ?? String ( error ) } ` ) ;
89+ }
8790 }
91+ return ;
92+ }
93+ try {
94+ process . kill ( - pid , signal ) ;
8895 } catch ( error ) {
8996 const code = ( error as NodeJS . ErrnoException ) . code ;
90- if ( code !== "ESRCH" ) {
97+ if ( code === "ESRCH" ) {
98+ return ;
99+ }
100+ try {
101+ process . kill ( pid , signal ) ;
102+ } catch ( fallbackError ) {
103+ const fallbackCode = ( fallbackError as NodeJS . ErrnoException ) . code ;
104+ if ( fallbackCode === "ESRCH" ) {
105+ return ;
106+ }
91107 warn (
92- `failed to send ${ signal } to timed host command process ${ pid } : ${ code ?? String ( error ) } ` ,
108+ `failed to send ${ signal } to host command process ${ pid } : group ${ code ?? String ( error ) } , leader ${ fallbackCode ?? String ( fallbackError ) } ` ,
93109 ) ;
94110 }
95111 }
@@ -115,8 +131,8 @@ writeSync(
115131);
116132
117133let timedOut = false;
118- let killTimer;
119134let killDeadlineAt = 0;
135+ let timedOutCleanupStarted = false;
120136let outputExceeded = false;
121137let forwardedSignal;
122138let forwardedSignalKillTimer;
@@ -137,9 +153,17 @@ function signalGroup(signal) {
137153 }
138154 try {
139155 process.kill(-child.pid, signal);
156+ return;
140157 } catch (error) {
141- if (error && error.code !== "ESRCH") {
142- process.stderr.write("failed to send " + signal + " to timed host command process " + child.pid + ": " + (error.code || String(error)) + "\n");
158+ if (error && error.code === "ESRCH") {
159+ return;
160+ }
161+ try {
162+ process.kill(child.pid, signal);
163+ } catch (fallbackError) {
164+ if (!fallbackError || fallbackError.code !== "ESRCH") {
165+ process.stderr.write("failed to send " + signal + " to host command process " + child.pid + ": group " + ((error && error.code) || String(error)) + ", leader " + ((fallbackError && fallbackError.code) || String(fallbackError)) + "\n");
166+ }
143167 }
144168 }
145169}
@@ -152,19 +176,31 @@ function groupAlive() {
152176 process.kill(-child.pid, 0);
153177 return true;
154178 } catch (error) {
155- return Boolean(error && error.code === "EPERM");
179+ if (!error || error.code !== "EPERM") {
180+ return false;
181+ }
182+ if (child.exitCode !== null || child.signalCode !== null) {
183+ return false;
184+ }
185+ try {
186+ process.kill(child.pid, 0);
187+ return true;
188+ } catch {
189+ return false;
190+ }
156191 }
157192}
158193
159194function finishTimedOut() {
160- if (killTimer) {
161- clearTimeout(killTimer);
162- }
163195 writeSync(3, ${ JSON . stringify ( HOST_COMMAND_TIMEOUT_PREFIX ) } + "{}\n");
164196 process.exit(124);
165197}
166198
167199function finishTimedOutAfterCleanup() {
200+ if (timedOutCleanupStarted) {
201+ return;
202+ }
203+ timedOutCleanupStarted = true;
168204 if (!groupAlive()) {
169205 finishTimedOut();
170206 return;
@@ -273,8 +309,7 @@ const timeout = setTimeout(() => {
273309 timedOut = true;
274310 signalGroup("SIGTERM");
275311 killDeadlineAt = Date.now() + payload.timeoutKillGraceMs;
276- killTimer = setTimeout(() => signalGroup("SIGKILL"), payload.timeoutKillGraceMs);
277- killTimer.unref();
312+ finishTimedOutAfterCleanup();
278313}, payload.timeoutMs);
279314timeout.unref();
280315
@@ -287,9 +322,6 @@ child.stdin.on("error", (error) => {
287322});
288323child.on("error", (error) => {
289324 clearTimeout(timeout);
290- if (killTimer) {
291- clearTimeout(killTimer);
292- }
293325 writeSync(
294326 3,
295327 ${ JSON . stringify ( HOST_COMMAND_SPAWN_ERROR_PREFIX ) } + JSON.stringify({
@@ -310,9 +342,6 @@ child.on("close", (code, signal) => {
310342 finishTimedOutAfterCleanup();
311343 return;
312344 }
313- if (killTimer) {
314- clearTimeout(killTimer);
315- }
316345 if (outputExceeded) {
317346 process.exit(1);
318347 }
@@ -583,7 +612,18 @@ export async function runStreaming(
583612 process . kill ( - childPid , 0 ) ;
584613 return true ;
585614 } catch ( error ) {
586- return ( error as NodeJS . ErrnoException ) . code === "EPERM" ;
615+ if ( ( error as NodeJS . ErrnoException ) . code !== "EPERM" ) {
616+ return false ;
617+ }
618+ if ( child . exitCode !== null || child . signalCode !== null ) {
619+ return false ;
620+ }
621+ try {
622+ process . kill ( childPid , 0 ) ;
623+ return true ;
624+ } catch {
625+ return false ;
626+ }
587627 }
588628 } ;
589629 const waitForStreamingProcessGroupExit = async ( timeoutBudgetMs : number ) : Promise < boolean > => {
@@ -703,6 +743,7 @@ export async function runStreaming(
703743 let timedOut = false ;
704744 let killTimer : NodeJS . Timeout | undefined ;
705745 let killDeadlineAt = 0 ;
746+ let forceKillSent = false ;
706747 const waitForStreamingTimeoutCleanup = async ( ) : Promise < void > => {
707748 if ( ! detached ) {
708749 signalStreamingChild ( "SIGKILL" ) ;
@@ -712,7 +753,12 @@ export async function runStreaming(
712753 if ( remainingGraceMs > 0 ) {
713754 await waitForStreamingProcessGroupExit ( remainingGraceMs ) ;
714755 }
715- if ( streamingProcessGroupAlive ( ) ) {
756+ if ( streamingProcessGroupAlive ( ) && ! forceKillSent ) {
757+ if ( killTimer ) {
758+ clearTimeout ( killTimer ) ;
759+ killTimer = undefined ;
760+ }
761+ forceKillSent = true ;
716762 signalStreamingChild ( "SIGKILL" ) ;
717763 await waitForStreamingProcessGroupExit ( HOST_COMMAND_POST_FORCE_KILL_WAIT_MS ) ;
718764 }
@@ -724,10 +770,10 @@ export async function runStreaming(
724770 timedOut = true ;
725771 signalHostCommandProcess ( childPid , "SIGTERM" ) ;
726772 killDeadlineAt = Date . now ( ) + HOST_COMMAND_STREAMING_TIMEOUT_KILL_GRACE_MS ;
727- killTimer = setTimeout (
728- ( ) => signalHostCommandProcess ( childPid , "SIGKILL" ) ,
729- HOST_COMMAND_STREAMING_TIMEOUT_KILL_GRACE_MS ,
730- ) ;
773+ killTimer = setTimeout ( ( ) => {
774+ forceKillSent = true ;
775+ signalHostCommandProcess ( childPid , "SIGKILL" ) ;
776+ } , HOST_COMMAND_STREAMING_TIMEOUT_KILL_GRACE_MS ) ;
731777 killTimer . unref ( ) ;
732778 } , timeoutMs ) ;
733779
0 commit comments