Skip to content

Commit 597e6c3

Browse files
authored
refactor: remove tree-kill dependency and refactor killAllProcesses to use native childProc.kill with retries.
Remove last `tree-kill` package usage.
1 parent 363fd37 commit 597e6c3

File tree

4 files changed

+29
-32
lines changed

4 files changed

+29
-32
lines changed

pnpm-lock.yaml

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/e2e/utils/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,5 @@ ts_project(
2020
"//:node_modules/verdaccio-auth-memory",
2121
"//tests:node_modules/@types/tar-stream",
2222
"//tests:node_modules/tar-stream",
23-
"//tests:node_modules/tree-kill",
2423
],
2524
)

tests/e2e/utils/process.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { spawn, SpawnOptions } from 'node:child_process';
22
import * as child_process from 'node:child_process';
33
import { getGlobalVariable, getGlobalVariablesEnv } from './env';
4-
import treeKill from 'tree-kill';
4+
import { setTimeout as sleep } from 'node:timers/promises';
55
import { delimiter, join, resolve } from 'node:path';
66
import { stripVTControlCharacters, styleText } from 'node:util';
77

@@ -255,29 +255,37 @@ export async function waitForAnyProcessOutputToMatch(
255255
return matchingProcess;
256256
}
257257

258-
export async function killAllProcesses(signal = 'SIGTERM'): Promise<void> {
259-
const processesToKill: Promise<void>[] = [];
258+
/**
259+
* Kills all tracked processes with a retry mechanism.
260+
*/
261+
export async function killAllProcesses(signal: NodeJS.Signals = 'SIGTERM'): Promise<void> {
262+
let attempts = 0;
263+
const maxRetries = 3;
264+
265+
while (_processes.length > 0 && attempts < maxRetries) {
266+
attempts++;
267+
268+
// Iterate backwards so we can remove elements while looping if needed.
269+
for (let i = _processes.length - 1; i >= 0; i--) {
270+
const childProc = _processes[i];
260271

261-
while (_processes.length) {
262-
const childProc = _processes.pop();
263-
if (!childProc || childProc.pid === undefined) {
264-
continue;
272+
if (!childProc || childProc.killed) {
273+
_processes.splice(i, 1);
274+
continue;
275+
}
276+
277+
const killed = childProc.kill(signal);
278+
if (killed) {
279+
_processes.splice(i, 1);
280+
continue;
281+
}
265282
}
266283

267-
processesToKill.push(
268-
new Promise<void>((resolve) => {
269-
treeKill(childProc.pid!, signal, () => {
270-
// Ignore all errors.
271-
// This is due to a race condition with the `waitForMatch` logic.
272-
// where promises are resolved on matches and not when the process terminates.
273-
// Also in some cases in windows we get `The operation attempted is not supported`.
274-
resolve();
275-
});
276-
}),
277-
);
284+
// If still have processes, wait a bit before the next retry (e.g., 100ms)
285+
if (_processes.length > 0 && attempts < maxRetries) {
286+
await sleep(100);
287+
}
278288
}
279-
280-
await Promise.all(processesToKill);
281289
}
282290

283291
export function exec(cmd: string, ...args: string[]) {

tests/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"devDependencies": {
33
"@types/tar-stream": "3.1.4",
44
"@angular-devkit/schematics": "workspace:*",
5-
"tar-stream": "3.1.7",
6-
"tree-kill": "1.2.2"
5+
"tar-stream": "3.1.7"
76
}
87
}

0 commit comments

Comments
 (0)