| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- import { execFile, execFileSync } from "node:child_process";
- const E2E_RUN_ID_ARG_NAME = "--zcode-e2e-run-id";
- const E2E_HOME_ARG_NAME = "--zcode-e2e-home";
- const E2E_PROCESS_SCAN_MAX_BUFFER_BYTES = 20 * 1024 * 1024;
- export interface E2EProcessInfo {
- command: string;
- pid: number;
- ppid: number;
- }
- export interface E2EProcessCleanupIdentity {
- chromiumProfileRootDir?: string;
- currentPid?: number;
- homeDir: string;
- /**
- * 完整 run 收尾需要回收遗留的 ChromeDriver;但单次 reload 必须让 driver 存活以复用
- * 同一 WDIO service。调用方可关闭该父链补齐,避免 exit barrier 误等 driver 自己退出。
- */
- includeChromeDriverAncestors?: boolean;
- rootPids?: number[];
- runId?: string | null;
- }
- export interface E2EProcessScanOptions {
- platform?: NodeJS.Platform;
- runCommand?: (command: string, args: string[]) => string;
- }
- export interface E2EAsyncProcessScanOptions {
- platform?: NodeJS.Platform;
- runCommand?: (command: string, args: string[]) => Promise<string>;
- }
- export function buildE2ERunIdArg(runId: string) {
- return `${E2E_RUN_ID_ARG_NAME}=${runId}`;
- }
- export function createWindowsE2EHomeMarkerArg(homeDir: string) {
- return `${E2E_HOME_ARG_NAME}=${homeDir}`;
- }
- export function buildWindowsTaskkillArgs(pids: number[]) {
- const uniquePids = [...new Set(pids)].filter((pid) => Number.isInteger(pid) && pid > 0);
- return [...uniquePids.flatMap((pid) => ["/PID", String(pid)]), "/T", "/F"];
- }
- export function collectWindowsE2EProcessTreePids(
- output: string,
- identity: E2EProcessCleanupIdentity,
- ) {
- return collectE2EProcessTreePids(parseWindowsProcessRows(output), identity);
- }
- export function listE2EProcessTreePids(
- identity: E2EProcessCleanupIdentity,
- options: E2EProcessScanOptions = {},
- ) {
- const platform = options.platform ?? process.platform;
- const runCommand = options.runCommand ?? runProcessScanCommand;
- if (platform === "win32") {
- return collectWindowsE2EProcessTreePids(runWindowsProcessScan(runCommand), identity);
- }
- return collectE2EProcessTreePids(
- parsePosixProcessRows(runCommand("ps", ["-axo", "pid=,ppid=,command="])),
- identity,
- );
- }
- export async function listSystemProcesses(
- options: E2EAsyncProcessScanOptions = {},
- ): Promise<E2EProcessInfo[]> {
- const platform = options.platform ?? process.platform;
- const runCommand = options.runCommand ?? runProcessScanCommandAsync;
- return platform === "win32"
- ? parseWindowsProcessRows(await runWindowsProcessScanAsync(runCommand))
- : parsePosixProcessRows(await runCommand("ps", ["-axo", "pid=,ppid=,command="]));
- }
- export function collectProcessTreeByRootPids(
- processes: E2EProcessInfo[],
- rootPids: number[],
- ): E2EProcessInfo[] {
- const processPids = new Set(processes.map((item) => item.pid));
- const targetPids = new Set(
- rootPids.filter((pid) => Number.isInteger(pid) && pid > 0 && processPids.has(pid)),
- );
- let changed = true;
- while (changed) {
- changed = false;
- for (const item of processes) {
- if (!targetPids.has(item.pid) && targetPids.has(item.ppid)) {
- targetPids.add(item.pid);
- changed = true;
- }
- }
- }
- return processes
- .filter((item) => targetPids.has(item.pid))
- .sort((left, right) => left.pid - right.pid);
- }
- export function collectE2EProcessTreePids(
- processes: E2EProcessInfo[],
- identity: E2EProcessCleanupIdentity,
- ) {
- const excludedPids = collectCurrentProcessAncestorPids(
- processes,
- identity.currentPid ?? process.pid,
- );
- const processPids = new Set(processes.map((item) => item.pid));
- // Bug 原因:macOS Electron main 的实际命令行不保证保留 appArgs identity marker;
- // reload 已从 main bridge 取得权威 PID,必须允许它作为显式根,再递归收集全部后代。
- const targetPids = new Set([
- ...(identity.rootPids ?? []).filter(
- (pid) => Number.isInteger(pid) && pid > 0 && processPids.has(pid) && !excludedPids.has(pid),
- ),
- ...processes
- .filter(
- (item) =>
- !excludedPids.has(item.pid) && isCurrentE2EProcessCleanupRoot(item.command, identity),
- )
- .map((item) => item.pid),
- ]);
- const processByPid = new Map(processes.map((item) => [item.pid, item]));
- const seedPids = Array.from(targetPids);
- for (const seedPid of seedPids) {
- let current = processByPid.get(seedPid);
- while (current && current.ppid > 1) {
- const parent = processByPid.get(current.ppid);
- if (
- !parent ||
- excludedPids.has(parent.pid) ||
- !isE2EProcessAncestorCandidate(
- parent.command,
- identity.includeChromeDriverAncestors !== false,
- )
- ) {
- break;
- }
- // Bug 根因:macOS 会把 Electron main 的命令行收敛成 `ZCode E2E`,
- // ChromeDriver 也不携带 appArgs;只有 renderer/utility 还保留当前 run 的
- // profile/HOME。精确命中后代后必须反向补齐父链,否则只能杀 renderer,
- // markerless main/ChromeDriver 会跨 worker 存活并持续重建窗口。
- targetPids.add(parent.pid);
- current = parent;
- }
- }
- let changed = true;
- while (changed) {
- changed = false;
- for (const item of processes) {
- if (!targetPids.has(item.pid) && targetPids.has(item.ppid) && !excludedPids.has(item.pid)) {
- targetPids.add(item.pid);
- changed = true;
- }
- }
- }
- return [...targetPids].sort((left, right) => right - left);
- }
- function isE2EProcessAncestorCandidate(command: string, includeChromeDriverAncestors: boolean) {
- const normalizedCommand = command.trim().replaceAll("\\", "/").toLowerCase();
- return (
- (includeChromeDriverAncestors &&
- /(?:^|\/)chromedriver(?:\.exe)?(?:\s|$)/u.test(normalizedCommand)) ||
- /(?:^|\/)electron(?:\.exe)?(?:\s|$)/u.test(normalizedCommand) ||
- /(?:^|\/)zcode e2e(?:\.exe)?(?:\s|$)/u.test(normalizedCommand)
- );
- }
- export function isCurrentE2EProcessCleanupRoot(
- command: string,
- identity: E2EProcessCleanupIdentity,
- ) {
- const normalizedCommand = command.toLowerCase();
- if (normalizedCommand.includes("@wdio/cli") || normalizedCommand.includes("wdio.conf.ts")) {
- return false;
- }
- const runId = identity.runId?.trim();
- if (runId && isCommandMarkerPresent(command, buildE2ERunIdArg(runId))) {
- return true;
- }
- // Bug 原因:main 改名后丢失启动参数,Chromium 子进程只保留 run cache 下的
- // profile,不包含 HOME;用当前 run 独占的 profile 路径找回父链,避免漏清 main。
- const chromiumProfileRootDir = identity.chromiumProfileRootDir?.trim();
- if (chromiumProfileRootDir && isCommandPathPresent(command, chromiumProfileRootDir)) {
- return true;
- }
- const homeDir = identity.homeDir.trim();
- if (!homeDir) {
- return false;
- }
- if (buildE2EHomeArgMarkers(homeDir).some((marker) => isCommandMarkerPresent(command, marker))) {
- return true;
- }
- return buildE2EHomePathMarkers(homeDir).some((marker) => isCommandPathPresent(command, marker));
- }
- function parseWindowsProcessRows(output: string): E2EProcessInfo[] {
- if (!output.trim()) {
- return [];
- }
- let parsed: unknown;
- try {
- parsed = JSON.parse(output) as unknown;
- } catch {
- return [];
- }
- const rows = Array.isArray(parsed) ? parsed : [parsed];
- return rows.flatMap((row) => {
- if (!row || typeof row !== "object") {
- return [];
- }
- const record = row as Record<string, unknown>;
- const pid = Number(record.ProcessId);
- const parentPid = Number(record.ParentProcessId);
- if (!Number.isInteger(pid) || !Number.isInteger(parentPid)) {
- return [];
- }
- return [
- {
- command: typeof record.CommandLine === "string" ? record.CommandLine : "",
- pid,
- ppid: parentPid,
- },
- ];
- });
- }
- function parsePosixProcessRows(output: string): E2EProcessInfo[] {
- return output
- .split("\n")
- .map((line) => line.match(/^\s*(\d+)\s+(\d+)\s+(.+)$/u))
- .filter((match): match is RegExpMatchArray => Boolean(match))
- .map((match) => ({
- command: match[3] ?? "",
- pid: Number(match[1]),
- ppid: Number(match[2]),
- }))
- .filter((item) => Number.isInteger(item.pid) && Number.isInteger(item.ppid));
- }
- function runWindowsProcessScan(runCommand: (command: string, args: string[]) => string) {
- const script = [
- "Get-CimInstance Win32_Process |",
- "Select-Object ProcessId,ParentProcessId,CommandLine |",
- "ConvertTo-Json -Compress",
- ].join(" ");
- let latestError: unknown = null;
- for (const powershell of ["powershell.exe", "pwsh"]) {
- try {
- return runCommand(powershell, ["-NoProfile", "-Command", script]);
- } catch (error) {
- latestError = error;
- }
- }
- throw latestError;
- }
- async function runWindowsProcessScanAsync(
- runCommand: (command: string, args: string[]) => Promise<string>,
- ) {
- const script = [
- "Get-CimInstance Win32_Process |",
- "Select-Object ProcessId,ParentProcessId,CommandLine |",
- "ConvertTo-Json -Compress",
- ].join(" ");
- let latestError: unknown = null;
- for (const powershell of ["powershell.exe", "pwsh"]) {
- try {
- return await runCommand(powershell, ["-NoProfile", "-Command", script]);
- } catch (error) {
- latestError = error;
- }
- }
- throw latestError;
- }
- function runProcessScanCommand(command: string, args: string[]) {
- return execFileSync(command, args, {
- encoding: "utf-8",
- maxBuffer: E2E_PROCESS_SCAN_MAX_BUFFER_BYTES,
- windowsHide: true,
- });
- }
- function runProcessScanCommandAsync(command: string, args: string[]): Promise<string> {
- return new Promise((resolveCommand, rejectCommand) => {
- execFile(
- command,
- args,
- {
- encoding: "utf-8",
- maxBuffer: E2E_PROCESS_SCAN_MAX_BUFFER_BYTES,
- windowsHide: true,
- },
- (error, stdout) => {
- if (error) {
- rejectCommand(error);
- return;
- }
- resolveCommand(stdout);
- },
- );
- });
- }
- function collectCurrentProcessAncestorPids(processes: E2EProcessInfo[], currentPid: number) {
- const byPid = new Map(processes.map((item) => [item.pid, item]));
- const pids = new Set<number>([currentPid]);
- let current = byPid.get(currentPid);
- while (current && current.ppid > 0 && !pids.has(current.ppid)) {
- pids.add(current.ppid);
- current = byPid.get(current.ppid);
- }
- return pids;
- }
- function buildE2EHomeArgMarkers(homeDir: string) {
- return buildE2EHomePathMarkers(homeDir).map(createWindowsE2EHomeMarkerArg);
- }
- function buildE2EHomePathMarkers(homeDir: string) {
- return uniqueStrings([homeDir, homeDir.replaceAll("\\", "/"), homeDir.replaceAll("/", "\\")]);
- }
- function isCommandMarkerPresent(command: string, marker: string) {
- if (!marker) {
- return false;
- }
- let index = command.indexOf(marker);
- while (index >= 0) {
- if (
- hasCommandBoundary(command[index - 1]) &&
- hasCommandBoundary(command[index + marker.length])
- ) {
- return true;
- }
- index = command.indexOf(marker, index + marker.length);
- }
- return false;
- }
- function isCommandPathPresent(command: string, marker: string) {
- const normalizedCommand = normalizePathForComparison(command);
- const normalizedMarker = normalizePathForComparison(marker);
- let index = normalizedCommand.indexOf(normalizedMarker);
- while (index >= 0) {
- const before = normalizedCommand[index - 1];
- const after = normalizedCommand[index + normalizedMarker.length];
- // 修复原因:共享 app 路径不能作为 identity;HOME 必须按完整路径边界匹配,
- // 同时允许命令行继续引用 HOME 下的配置文件,避免 current 命中 current-copy。
- if (isHomeBoundaryBefore(before) && isHomeBoundaryAfter(after)) {
- return true;
- }
- index = normalizedCommand.indexOf(normalizedMarker, index + normalizedMarker.length);
- }
- return false;
- }
- function normalizePathForComparison(value: string) {
- return value.trim().replaceAll("\\", "/").replace(/\/+$/u, "").toLowerCase();
- }
- function isHomeBoundaryBefore(char: string | undefined) {
- return char === undefined || /[\s"'=]/u.test(char);
- }
- function isHomeBoundaryAfter(char: string | undefined) {
- return char === undefined || /[\s/"']/u.test(char);
- }
- function hasCommandBoundary(char: string | undefined) {
- return char === undefined || !/[A-Za-z0-9._~:/\\-]/u.test(char);
- }
- function uniqueStrings(values: string[]) {
- return [...new Set(values.filter(Boolean))];
- }
- /**
- * E2E runner 里被 await 的等待(等待残留进程退出、重试删除 HOME)。
- *
- * Bug 根因:原实现(wdio.conf.ts 的 delay)给计时器 unref。onComplete 强杀残留 Electron 时,进程里
- * 只剩这个计时器,事件循环为空,Node 在等待中途直接以 0 退出:summary 没写、退出看门狗没装、
- * WDIO 也没按失败结果退出,本机 Docker runner 因此把失败的 spec 报成 PASS。
- * 修法:被等待的计时器保持 ref。等待本身有界(调用方的超时循环),兜底退出仍由 onComplete 看门狗负责。
- */
- export function waitE2EDelay(ms: number): Promise<void> {
- return new Promise<void>((resolveDelay) => setTimeout(resolveDelay, Math.max(ms, 0)));
- }
|