@@ -36,25 +36,44 @@ const TERMINAL_BUFFER_LIMIT = 1000
3636/** TERM handed to spawned PTYs; also used to reject fallback process labels. */
3737const PTY_TERM_NAME = 'xterm-256color'
3838
39- /**
40- * Kill a `startChildProcess()` run together with everything it spawned.
41- *
42- * On Windows, tinyexec runs anything that isn't a `.exe`/`.com` - including
43- * the `node_modules/.bin/*.cmd` shims package managers generate - through
44- * `cmd.exe /d /s /c`, so the pid held here is the wrapper's. Killing a process
45- * on Windows doesn't reach its descendants, so `cp.kill()` alone would leave
46- * the real program running (and holding its ports). `taskkill /T /F` ends the
47- * whole tree; `cp.kill()` stays the fallback and the POSIX path.
48- */
39+ function processTree ( pid : number , psOutput : string ) : number [ ] {
40+ const children = new Map < number , number [ ] > ( )
41+ for ( const [ , childPid , parentPid ] of psOutput . matchAll ( / ^ \s * ( \d + ) \s + ( \d + ) $ / gm) ) {
42+ const parent = Number ( parentPid )
43+ if ( ! children . has ( parent ) )
44+ children . set ( parent , [ ] )
45+ children . get ( parent ) ! . push ( Number ( childPid ) )
46+ }
47+ const tree = new Set ( [ pid ] )
48+ for ( const parent of tree ) {
49+ for ( const child of children . get ( parent ) ?? [ ] )
50+ tree . add ( child )
51+ }
52+ return [ ...tree ]
53+ }
54+
55+ // On Windows, tinyexec runs non-.exe/.com commands such as node_modules/.bin/*.cmd
56+ // through cmd.exe /d /s /c. Killing that shell PID leaves the real program running.
57+ // taskkill stops the whole tree. On POSIX, use ps to stop children before their parents.
4958async function killProcessTree ( cp : TinyExecResult ) : Promise < void > {
5059 const child = cp . process
5160 const pid = child ?. pid
52- if ( process . platform !== 'win32' || ! child || pid === undefined || child . exitCode !== null || child . signalCode !== null ) {
61+ if ( ! child || pid === undefined || child . exitCode !== null || child . signalCode !== null ) {
5362 cp . kill ( )
5463 return
5564 }
5665 const { exec } = await import ( 'tinyexec' )
5766 try {
67+ if ( process . platform !== 'win32' ) {
68+ const { stdout } = await exec ( 'ps' , [ '-A' , '-o' , 'pid=,ppid=' ] , { throwOnError : true } )
69+ for ( const target of processTree ( pid , stdout ) . reverse ( ) ) {
70+ try {
71+ process . kill ( target , 'SIGTERM' )
72+ }
73+ catch { }
74+ }
75+ return
76+ }
5877 const { exitCode } = await exec ( 'taskkill' , [
5978 '/pid' ,
6079 String ( pid ) ,
0 commit comments