11import { spawn , type ChildProcess } from "node:child_process" ;
22import { realpathSync } from "node:fs" ;
33
4- const SWEEP = `$b=[int]$env:HF_CHROME_PID; $x=$env:HF_CHROME_EXE; Get-CimInstance Win32_Process | Where-Object { ($_.ProcessId -eq $b -or $_.ParentProcessId -eq $b) -and $_.ExecutablePath -eq $x } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }` ;
4+ const SWEEP = `$b=[int]$env:HF_CHROME_PID; $x=$env:HF_CHROME_EXE; $t=[DateTimeOffset]::FromUnixTimeMilliseconds([int64]$env:HF_CHROME_SINCE).UtcDateTime; Get-CimInstance Win32_Process | Where-Object { ($_.ProcessId -eq $b -or $_.ParentProcessId -eq $b) -and $_.ExecutablePath -eq $x -and $_.CreationDate.ToUniversalTime() -ge $t } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }` ;
55
66const WATCHDOG = `let clean = false;
77const done = () => {
@@ -25,12 +25,20 @@ function executableOf(chrome: ChildProcess): string | undefined {
2525}
2626
2727/** Windows does not end a process's children with it, so Chrome's children outlive a killed CLI without this. */
28- export function watchChromeTree ( chrome : ChildProcess ) : ChromeWatchdog | undefined {
28+ export function watchChromeTree (
29+ chrome : ChildProcess ,
30+ launchedAt : number ,
31+ ) : ChromeWatchdog | undefined {
2932 const executable = process . platform === "win32" ? executableOf ( chrome ) : undefined ;
3033 if ( ! executable || chrome . pid === undefined ) return undefined ;
3134 const watchdog = spawn ( process . execPath , [ "-e" , WATCHDOG ] , {
3235 detached : true ,
33- env : { ...process . env , HF_CHROME_PID : String ( chrome . pid ) , HF_CHROME_EXE : executable } ,
36+ env : {
37+ ...process . env ,
38+ HF_CHROME_PID : String ( chrome . pid ) ,
39+ HF_CHROME_EXE : executable ,
40+ HF_CHROME_SINCE : String ( launchedAt ) ,
41+ } ,
3442 stdio : [ "pipe" , "ignore" , "ignore" ] ,
3543 windowsHide : true ,
3644 } ) ;
0 commit comments