Skip to content

Commit 73b9e37

Browse files
committed
Revert "refactor: remove tree-kill dependency and refactor killAllProcesses to use native childProc.kill with retries."
This reverts commit 597e6c3.
1 parent 414320d commit 73b9e37

File tree

4 files changed

+32
-29
lines changed

4 files changed

+32
-29
lines changed

pnpm-lock.yaml

Lines changed: 9 additions & 0 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ 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",
2324
],
2425
)

tests/e2e/utils/process.ts

Lines changed: 20 additions & 28 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 { setTimeout as sleep } from 'node:timers/promises';
4+
import treeKill from 'tree-kill';
55
import { delimiter, join, resolve } from 'node:path';
66
import { stripVTControlCharacters, styleText } from 'node:util';
77

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

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];
258+
export async function killAllProcesses(signal = 'SIGTERM'): Promise<void> {
259+
const processesToKill: Promise<void>[] = [];
271260

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-
}
261+
while (_processes.length) {
262+
const childProc = _processes.pop();
263+
if (!childProc || childProc.pid === undefined) {
264+
continue;
282265
}
283266

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-
}
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+
);
288278
}
279+
280+
await Promise.all(processesToKill);
289281
}
290282

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

tests/package.json

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

0 commit comments

Comments
 (0)