Fix Windows process tree traversal, remove dead code, deduplicate normalizeRunningPorts

- win32.ts: Replace direct-child-only query with full BFS traversal
  (collectWindowsProcessFamilyPids) to match POSIX behavior for
  detecting grandchild processes and their listening ports
- index.ts: Remove unused subprocessCheckerToInspector export and
  clean up unused type imports
- Deduplicate normalizeRunningPorts by exporting from terminalStateStore
  and importing in Sidebar.tsx and ThreadTerminalDrawer.tsx
This commit is contained in:
Cursor Agent committed 2026-04-04 20:40:03 +00:00
1 parent 05a7018f03
commit 755bf3aa0d
5 files changed
+58 -45

No files matched your search

@@ -1,11 +1,6 @@
import { collectPosixProcessFamilyPids, checkPosixListeningPorts } from "./posix";
import {
type TerminalSubprocessActivity,
type TerminalSubprocessChecker,
type TerminalSubprocessInspector,
type TerminalWebPortInspector,
} from "./types";
import { checkWindowsListeningPorts, collectWindowsChildPids } from "./win32";
import { type TerminalSubprocessActivity } from "./types";
import { checkWindowsListeningPorts, collectWindowsProcessFamilyPids } from "./win32";
export { arePortListsEqual, normalizeRunningPorts } from "./utils";
@@ -24,11 +19,15 @@ export async function defaultSubprocessInspector(
}
if (process.platform === "win32") {
const childPids = await collectWindowsChildPids(terminalPid);
const processPidsForPortScan = [terminalPid, ...childPids];
const runningPorts = await checkWindowsListeningPorts(processPidsForPortScan);
const processFamilyPids = await collectWindowsProcessFamilyPids(terminalPid);
if (processFamilyPids.length === 0) {
return { hasRunningSubprocess: false, runningPorts: [] };
}
const subprocessPids = processFamilyPids.filter((pid) => pid !== terminalPid);
const runningPorts = await checkWindowsListeningPorts(processFamilyPids);
return {
hasRunningSubprocess: childPids.length > 0 || runningPorts.length > 0,
hasRunningSubprocess: subprocessPids.length > 0 || runningPorts.length > 0,
runningPorts,
};
}
@@ -45,12 +44,3 @@ export async function defaultSubprocessInspector(
runningPorts,
};
}
export function subprocessCheckerToInspector(
subprocessChecker: TerminalSubprocessChecker,
): TerminalSubprocessInspector {
return async (terminalPid: number) => ({
hasRunningSubprocess: await subprocessChecker(terminalPid),
runningPorts: [],
});
}
@@ -1,27 +1,56 @@
import { runProcess } from "../processRunner";
import { parsePidList, parsePortList } from "./utils";
import { parsePortList } from "./utils";
export async function collectWindowsChildPids(terminalPid: number): Promise<number[]> {
export async function collectWindowsProcessFamilyPids(terminalPid: number): Promise<number[]> {
const command = [
`$children = Get-CimInstance Win32_Process -Filter "ParentProcessId = ${terminalPid}" -ErrorAction SilentlyContinue`,
"if (-not $children) { exit 0 }",
"$children | Select-Object -ExpandProperty ProcessId",
"$procs = Get-CimInstance Win32_Process -Property ProcessId,ParentProcessId -ErrorAction SilentlyContinue",
"if (-not $procs) { exit 0 }",
'$procs | ForEach-Object { "$($_.ProcessId) $($_.ParentProcessId)" }',
].join("; ");
try {
const result = await runProcess(
"powershell.exe",
["-NoProfile", "-NonInteractive", "-Command", command],
{
timeoutMs: 1_500,
timeoutMs: 2_000,
allowNonZeroExit: true,
maxBufferBytes: 32_768,
maxBufferBytes: 262_144,
outputMode: "truncate",
},
);
if (result.code !== 0) {
return [];
}
return parsePidList(result.stdout);
const childrenByParentPid = new Map<number, number[]>();
for (const line of result.stdout.split(/\r?\n/g)) {
const [pidRaw, ppidRaw] = line.trim().split(/\s+/g);
const pid = Number(pidRaw);
const ppid = Number(ppidRaw);
if (!Number.isInteger(pid) || !Number.isInteger(ppid)) continue;
const children = childrenByParentPid.get(ppid);
if (children) {
children.push(pid);
} else {
childrenByParentPid.set(ppid, [pid]);
}
}
const processFamily = new Set<number>([terminalPid]);
const pendingParents = [terminalPid];
while (pendingParents.length > 0) {
const parentPid = pendingParents.shift();
if (!parentPid) continue;
const childPids = childrenByParentPid.get(parentPid);
if (!childPids || childPids.length === 0) continue;
for (const childPid of childPids) {
if (processFamily.has(childPid)) continue;
processFamily.add(childPid);
pendingParents.push(childPid);
}
}
return [...processFamily];
} catch {
return [];
}
+5 -8
View File
@@ -61,7 +61,11 @@ import { APP_STAGE_LABEL, APP_VERSION } from "../branding";
import { isTerminalFocused } from "../lib/terminalFocus";
import { isLinuxPlatform, isMacPlatform, newCommandId, newProjectId } from "../lib/utils";
import { useStore } from "../store";
import { selectThreadTerminalState, useTerminalStateStore } from "../terminalStateStore";
import {
normalizeRunningPorts,
selectThreadTerminalState,
useTerminalStateStore,
} from "../terminalStateStore";
import { useUiStateStore } from "../uiStateStore";
import {
resolveShortcutCommand,
@@ -204,13 +208,6 @@ function ThreadStatusLabel({
);
}
function normalizeRunningPorts(rawPorts: number[] | undefined): number[] {
if (!rawPorts) return [];
return [...new Set(rawPorts)]
.filter((port) => Number.isInteger(port) && port > 0 && port <= 65_535)
.toSorted((left, right) => left - right);
}
function terminalStatusFromTerminalState(
runningTerminalIds: string[],
runningTerminalPorts: Record<string, number[]>,
@@ -33,7 +33,11 @@ import {
type ThreadTerminalGroup,
} from "../types";
import { readNativeApi } from "~/nativeApi";
import { selectTerminalEventEntries, useTerminalStateStore } from "../terminalStateStore";
import {
normalizeRunningPorts,
selectTerminalEventEntries,
useTerminalStateStore,
} from "../terminalStateStore";
const MIN_DRAWER_HEIGHT = 180;
const MAX_DRAWER_HEIGHT_RATIO = 0.75;
@@ -81,13 +85,6 @@ interface TerminalRuntimeStatus {
extraWebPortCount: number;
}
function normalizeRunningPorts(rawPorts: number[] | undefined): number[] {
if (!rawPorts) return [];
return [...new Set(rawPorts)]
.filter((port) => Number.isInteger(port) && port > 0 && port <= 65_535)
.toSorted((left, right) => left - right);
}
function terminalRuntimeStatus(
terminalId: string,
runningTerminalIds: Set<string>,
+1 -1
View File
@@ -62,7 +62,7 @@ function normalizeRunningTerminalIds(
.filter((id) => id.length > 0 && validTerminalIdSet.has(id));
}
function normalizeRunningPorts(ports: readonly number[] | undefined): number[] {
export function normalizeRunningPorts(ports: readonly number[] | undefined): number[] {
if (!ports || ports.length === 0) return [];
return [...new Set(ports)]
.filter((port) => Number.isInteger(port) && port > 0 && port <= 65_535)