Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/e2e/utils/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ ts_project(
"//:node_modules/verdaccio-auth-memory",
"//tests:node_modules/@types/tar-stream",
"//tests:node_modules/tar-stream",
"//tests:node_modules/tree-kill",
],
)
48 changes: 20 additions & 28 deletions tests/e2e/utils/process.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { spawn, SpawnOptions } from 'node:child_process';
import * as child_process from 'node:child_process';
import { getGlobalVariable, getGlobalVariablesEnv } from './env';
import { setTimeout as sleep } from 'node:timers/promises';
import treeKill from 'tree-kill';
import { delimiter, join, resolve } from 'node:path';
import { stripVTControlCharacters, styleText } from 'node:util';

Expand Down Expand Up @@ -255,37 +255,29 @@ export async function waitForAnyProcessOutputToMatch(
return matchingProcess;
}

/**
* Kills all tracked processes with a retry mechanism.
*/
export async function killAllProcesses(signal: NodeJS.Signals = 'SIGTERM'): Promise<void> {
let attempts = 0;
const maxRetries = 3;

while (_processes.length > 0 && attempts < maxRetries) {
attempts++;

// Iterate backwards so we can remove elements while looping if needed.
for (let i = _processes.length - 1; i >= 0; i--) {
const childProc = _processes[i];
export async function killAllProcesses(signal = 'SIGTERM'): Promise<void> {
const processesToKill: Promise<void>[] = [];

if (!childProc || childProc.killed) {
_processes.splice(i, 1);
continue;
}

const killed = childProc.kill(signal);
if (killed) {
_processes.splice(i, 1);
continue;
}
while (_processes.length) {
const childProc = _processes.pop();
if (!childProc || childProc.pid === undefined) {
continue;
}

// If still have processes, wait a bit before the next retry (e.g., 100ms)
if (_processes.length > 0 && attempts < maxRetries) {
await sleep(100);
}
processesToKill.push(
new Promise<void>((resolve) => {
treeKill(childProc.pid!, signal, () => {
// Ignore all errors.
// This is due to a race condition with the `waitForMatch` logic.
// where promises are resolved on matches and not when the process terminates.
// Also in some cases in windows we get `The operation attempted is not supported`.
resolve();
});
}),
);
}

await Promise.all(processesToKill);
}

export function exec(cmd: string, ...args: string[]) {
Expand Down
3 changes: 2 additions & 1 deletion tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"devDependencies": {
"@types/tar-stream": "3.1.4",
"@angular-devkit/schematics": "workspace:*",
"tar-stream": "3.1.7"
"tar-stream": "3.1.7",
"tree-kill": "1.2.2"
}
}