Skip to content

Commit afb59bc

Browse files
committed
fix(server): backend children no longer count as desktop resources
1 parent f87dd14 commit afb59bc

2 files changed

Lines changed: 66 additions & 1 deletion

File tree

‎apps/server/src/resourceTelemetry/Model.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,68 @@ describe("resource telemetry process model", () => {
174174
expect(result.groups.electron.processCount).toBe(2);
175175
});
176176

177+
it("keeps backend descendants out of desktop totals when Electron launches the server", () => {
178+
const samples = [
179+
processSample({ pid: 300, ppid: 1, startTimeMs: 1_000, cpuPercent: 2 }),
180+
processSample({ pid: SERVER_PID, ppid: 300, startTimeMs: 2_000, cpuPercent: 5 }),
181+
processSample({ pid: 200, ppid: SERVER_PID, startTimeMs: 3_000, cpuPercent: 400 }),
182+
processSample({ pid: 201, ppid: 200, startTimeMs: 4_000, cpuPercent: 100 }),
183+
processSample({ pid: 301, ppid: 300, startTimeMs: 3_000, cpuPercent: 3 }),
184+
processSample({ pid: 900, ppid: SERVER_PID, startTimeMs: 3_000, cpuPercent: 1 }),
185+
];
186+
const desktop = desktopSnapshot(BASE_TIME_MS, [
187+
electronMetric({ pid: 300, creationTimeMs: 1_000, type: "Browser" }),
188+
]);
189+
const first = merge({
190+
sidecarPid: 900,
191+
native: nativeSnapshot(BASE_TIME_MS, samples),
192+
desktop,
193+
});
194+
const result = merge({
195+
sidecarPid: 900,
196+
previous: first,
197+
native: nativeSnapshot(
198+
BASE_TIME_MS + 1_000,
199+
samples.map((sample) => ({
200+
...sample,
201+
cpuTimeMs: sample.cpuPercent * 10,
202+
ioReadBytes: sample.cpuPercent * 100,
203+
ioWriteBytes: sample.cpuPercent * 200,
204+
})),
205+
2,
206+
),
207+
desktop,
208+
});
209+
210+
expect(result.processes.find((process) => process.identity.pid === 200)?.category).toBe(
211+
"server-child",
212+
);
213+
expect(result.processes.find((process) => process.identity.pid === 201)?.category).toBe(
214+
"server-child",
215+
);
216+
expect(result.groups.backend).toMatchObject({
217+
processCount: 3,
218+
currentCpuPercent: 505,
219+
cpuTimeMs: 5_050,
220+
currentRssBytes: 3_072,
221+
ioReadBytes: 50_500,
222+
ioWriteBytes: 101_000,
223+
processStarts: 3,
224+
});
225+
expect(result.groups.electron).toMatchObject({
226+
processCount: 2,
227+
currentCpuPercent: 5,
228+
cpuTimeMs: 50,
229+
currentRssBytes: 2_048,
230+
ioReadBytes: 500,
231+
ioWriteBytes: 1_000,
232+
processStarts: 2,
233+
});
234+
expect(result.groups.monitor.processCount).toBe(1);
235+
expect(result.groups.allT3.currentCpuPercent).toBe(511);
236+
expect(result.groups.allT3.currentRssBytes).toBe(6_144);
237+
});
238+
177239
it("ignores stale Electron metrics after PID reuse", () => {
178240
const result = merge({
179241
native: nativeSnapshot(BASE_TIME_MS, [

‎apps/server/src/resourceTelemetry/Model.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,14 @@ function isElectronDescendant(
192192
pid: number,
193193
processesByPid: ReadonlyMap<number, ResourceMonitorProcessSample>,
194194
electronPids: ReadonlySet<number>,
195+
serverPid: number,
195196
): boolean {
196197
const visited = new Set<number>();
197198
let currentPid = pid;
198199
while (!visited.has(currentPid)) {
199200
visited.add(currentPid);
201+
// The backend's children belong to the backend even when Electron launched it.
202+
if (currentPid === serverPid) return false;
200203
if (electronPids.has(currentPid)) return true;
201204
const current = processesByPid.get(currentPid);
202205
if (!current || current.ppid <= 0 || current.ppid === currentPid) return false;
@@ -508,7 +511,7 @@ export function mergeProcesses(input: MergeProcessesInput): MergeProcessesResult
508511
? "electron-main"
509512
: electronMetric
510513
? electronCategory(electronMetric)
511-
: isElectronDescendant(process.pid, processesByPid, electronPids)
514+
: isElectronDescendant(process.pid, processesByPid, electronPids, input.serverPid)
512515
? inferredElectronCategory(process)
513516
: "server-child";
514517
const firstSeenAt = previous?.process.firstSeenAt ?? DateTime.makeUnsafe(sampledAtMs);

0 commit comments

Comments
 (0)