33import { EventEmitter } from "node:events" ;
44import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
55
6- const { spawnMock, waitForChildProcessMock } = vi . hoisted ( ( ) => ( {
6+ const { killProcessTreeMock, spawnMock, waitForChildProcessMock } = vi . hoisted ( ( ) => ( {
7+ killProcessTreeMock : vi . fn ( ) ,
78 spawnMock : vi . fn ( ) ,
89 waitForChildProcessMock : vi . fn ( ) ,
910} ) ) ;
@@ -16,14 +17,20 @@ vi.mock("../utils/child-process.js", () => ({
1617 waitForChildProcess : waitForChildProcessMock ,
1718} ) ) ;
1819
20+ vi . mock ( "../../process/kill-tree.js" , ( ) => ( {
21+ killProcessTree : killProcessTreeMock ,
22+ } ) ) ;
23+
1924type StubChild = EventEmitter & {
2025 kill : ReturnType < typeof vi . fn > ;
26+ pid ?: number ;
2127 stderr : EventEmitter ;
2228 stdout : EventEmitter ;
2329} ;
2430
2531function createStubChild ( ) : StubChild {
2632 const child = new EventEmitter ( ) as StubChild ;
33+ child . pid = 1234 ;
2734 child . stdout = new EventEmitter ( ) ;
2835 child . stderr = new EventEmitter ( ) ;
2936 child . kill = vi . fn ( ) ;
@@ -42,6 +49,7 @@ function createDeferred<T>() {
4249
4350describe ( "execCommand" , ( ) => {
4451 beforeEach ( ( ) => {
52+ killProcessTreeMock . mockReset ( ) ;
4553 spawnMock . mockReset ( ) ;
4654 waitForChildProcessMock . mockReset ( ) ;
4755 vi . useRealTimers ( ) ;
@@ -76,6 +84,26 @@ describe("execCommand", () => {
7684 expect ( result . stderrTruncatedChars ) . toBeGreaterThan ( 0 ) ;
7785 } ) ;
7886
87+ it ( "spawns commands with process-tree cleanup options" , async ( ) => {
88+ const child = createStubChild ( ) ;
89+ const wait = createDeferred < number | null > ( ) ;
90+ spawnMock . mockReturnValue ( child ) ;
91+ waitForChildProcessMock . mockReturnValue ( wait . promise ) ;
92+ const { execCommand } = await import ( "./exec.js" ) ;
93+
94+ const resultPromise = execCommand ( "cmd" , [ "arg" ] , "/tmp" ) ;
95+ wait . resolve ( 0 ) ;
96+ await resultPromise ;
97+
98+ expect ( spawnMock ) . toHaveBeenCalledWith ( "cmd" , [ "arg" ] , {
99+ cwd : "/tmp" ,
100+ detached : process . platform !== "win32" ,
101+ shell : false ,
102+ stdio : [ "ignore" , "pipe" , "pipe" ] ,
103+ windowsHide : true ,
104+ } ) ;
105+ } ) ;
106+
79107 it ( "honors caller-supplied small output caps" , async ( ) => {
80108 const child = createStubChild ( ) ;
81109 const wait = createDeferred < number | null > ( ) ;
@@ -105,7 +133,11 @@ describe("execCommand", () => {
105133 wait . resolve ( 0 ) ;
106134
107135 const result = await resultPromise ;
108- expect ( child . kill ) . toHaveBeenCalledWith ( "SIGTERM" ) ;
136+ expect ( killProcessTreeMock ) . toHaveBeenCalledWith ( 1234 , {
137+ detached : process . platform !== "win32" ,
138+ graceMs : 5000 ,
139+ } ) ;
140+ expect ( child . kill ) . not . toHaveBeenCalled ( ) ;
109141 expect ( result . code ) . toBe ( 1 ) ;
110142 expect ( result . killed ) . toBe ( true ) ;
111143 expect ( result . outputLimitExceeded ) . toBe ( "stdout" ) ;
@@ -114,9 +146,9 @@ describe("execCommand", () => {
114146 expect ( result . stderr ) . toContain ( "exec stdout exceeded output limit" ) ;
115147 } ) ;
116148
117- it ( "escalates timed-out commands to SIGKILL after the grace period " , async ( ) => {
118- // SIGTERM gives child processes a chance to clean up; SIGKILL is the
119- // bounded fallback so an ignored signal cannot hang the session .
149+ it ( "terminates timed-out commands through the process-tree killer " , async ( ) => {
150+ // Extension exec uses the same tree-kill boundary as the built-in shell so
151+ // timed-out wrappers do not leave descendant processes running .
120152 vi . useFakeTimers ( ) ;
121153 const child = createStubChild ( ) ;
122154 const wait = createDeferred < number | null > ( ) ;
@@ -126,13 +158,11 @@ describe("execCommand", () => {
126158
127159 const resultPromise = execCommand ( "cmd" , [ ] , "/tmp" , { timeout : 10 } ) ;
128160 await vi . advanceTimersByTimeAsync ( 10 ) ;
129- expect ( child . kill ) . toHaveBeenCalledWith ( "SIGTERM" ) ;
130- expect ( child . kill ) . not . toHaveBeenCalledWith ( "SIGKILL" ) ;
131-
132- await vi . advanceTimersByTimeAsync ( 4_999 ) ;
133- expect ( child . kill ) . not . toHaveBeenCalledWith ( "SIGKILL" ) ;
134- await vi . advanceTimersByTimeAsync ( 1 ) ;
135- expect ( child . kill ) . toHaveBeenCalledWith ( "SIGKILL" ) ;
161+ expect ( killProcessTreeMock ) . toHaveBeenCalledWith ( 1234 , {
162+ detached : process . platform !== "win32" ,
163+ graceMs : 5000 ,
164+ } ) ;
165+ expect ( child . kill ) . not . toHaveBeenCalled ( ) ;
136166
137167 wait . resolve ( null ) ;
138168 const result = await resultPromise ;
0 commit comments