Skip to content
This repository was archived by the owner on Aug 6, 2026. It is now read-only.

Commit c57c0b9

Browse files
fix: preserve process fallback per root
Generated-By: PostHog Code Task-Id: 739c9e2e-e891-4f4d-a293-843446a3fd73
1 parent f06ebf5 commit c57c0b9

2 files changed

Lines changed: 28 additions & 16 deletions

File tree

packages/workspace-server/src/services/process-tracking/process-utils.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,21 @@ describe("killUnixProcessTrees", () => {
104104
expect(signal).toHaveBeenCalledWith([-10, 10], "SIGTERM");
105105
expect(schedule).not.toHaveBeenCalled();
106106
});
107+
108+
it("falls back for missing roots in a mixed batch", () => {
109+
const signal = vi.fn();
110+
111+
killUnixProcessTrees(
112+
[10, 20],
113+
[{ pid: 10, ppid: 1, pgid: 10, startedAt }],
114+
1,
115+
{
116+
currentProcesses: () => [],
117+
signal,
118+
schedule: vi.fn(),
119+
},
120+
);
121+
122+
expect(signal).toHaveBeenCalledWith([-20, 20, -10, 10], "SIGTERM");
123+
});
107124
});

packages/workspace-server/src/services/process-tracking/process-utils.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,32 +114,27 @@ export function killUnixProcessTrees(
114114
deps: UnixProcessKillerDeps,
115115
): void {
116116
const children = indexProcesses(initialProcesses);
117-
const trees = rootPids
118-
.map((pid) => findProcessTreeFromIndex(pid, initialProcesses, children))
119-
.filter((tree) => tree.length > 0);
117+
const missingRootPids: number[] = [];
118+
const trees = rootPids.flatMap((pid) => {
119+
const tree = findProcessTreeFromIndex(pid, initialProcesses, children);
120+
if (tree.length === 0) missingRootPids.push(pid);
121+
return tree;
122+
});
120123
const originalTree = Array.from(
121124
new Map(trees.flat().map((entry) => [entry.pid, entry])).values(),
122125
);
123-
if (originalTree.length === 0) {
124-
deps.signal(
125-
rootPids.flatMap((pid) => [-pid, pid]),
126-
"SIGTERM",
127-
);
128-
return;
129-
}
130-
if (ownPgid === undefined) {
126+
if (originalTree.length === 0 || ownPgid === undefined) {
131127
deps.signal(
132128
rootPids.flatMap((pid) => [-pid, pid]),
133129
"SIGTERM",
134130
);
135131
return;
136132
}
137133

138-
const targets = findMatchingProcessTargets(
139-
originalTree,
140-
initialProcesses,
141-
ownPgid,
142-
);
134+
const targets = [
135+
...missingRootPids.flatMap((pid) => [-pid, pid]),
136+
...findMatchingProcessTargets(originalTree, initialProcesses, ownPgid),
137+
];
143138
deps.signal(targets, "SIGTERM");
144139
deps.schedule(() => {
145140
deps.signal(

0 commit comments

Comments
 (0)