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: 0 additions & 9 deletions pnpm-lock.yaml

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

1 change: 0 additions & 1 deletion tests/e2e/utils/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ 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: 28 additions & 20 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 treeKill from 'tree-kill';
import { setTimeout as sleep } from 'node:timers/promises';
import { delimiter, join, resolve } from 'node:path';
import { stripVTControlCharacters, styleText } from 'node:util';

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

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

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

const killed = childProc.kill(signal);
if (killed) {
_processes.splice(i, 1);
continue;
}
}

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();
});
}),
);
// If still have processes, wait a bit before the next retry (e.g., 100ms)
if (_processes.length > 0 && attempts < maxRetries) {
await sleep(100);
}
}

await Promise.all(processesToKill);
}

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