Skip to content

Commit dfd3f96

Browse files
Fix desktop test title capture after process exit
Previously, window title capture would continue running even after process termination was initiated. This occurred because the capture loop only checked the `isDone` flag, which is set after termination completes, not when it begins. This bug caused: - Unnecessary processing during termination grace period - Potential race conditions when accessing process resources - Attempts to capture titles from already-terminated processes The fix introduces a cancelCapture function that checks both `isDone` and `explicitlyKilled` flags, stopping title capture immediately when either termination process begins, not just after it completes.
1 parent 757834f commit dfd3f96

File tree

1 file changed

+28
-7
lines changed
  • tests/checks/desktop-runtime-errors/check-desktop-runtime-errors/app

1 file changed

+28
-7
lines changed

tests/checks/desktop-runtime-errors/check-desktop-runtime-errors/app/runner.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function runApplication(
3636
if (process.pid === undefined) {
3737
throw new Error('Unknown PID');
3838
}
39-
beginCapturingTitles(process.pid, processDetails);
39+
startWindowTitleCapturing(process.pid, processDetails);
4040
handleProcessEvents(
4141
processDetails,
4242
enableScreenshot,
@@ -78,11 +78,34 @@ function logDetails(
7878
);
7979
}
8080

81-
function beginCapturingTitles(
81+
function startWindowTitleCapturing(
8282
processId: number,
8383
processDetails: ApplicationProcessDetails,
8484
): void {
85-
const capture = async () => {
85+
let timeoutId: NodeJS.Timeout | null = null;
86+
87+
const shouldCapture = () => !processDetails.isDone && !processDetails.explicitlyKilled;
88+
89+
const scheduleNextCapture = () => {
90+
if (!shouldCapture()) {
91+
return;
92+
}
93+
timeoutId = setTimeout(captureAndScheduleNext, WINDOW_TITLE_CAPTURE_INTERVAL_IN_MS);
94+
};
95+
96+
const cancelNextCapture = () => {
97+
if (timeoutId) {
98+
clearTimeout(timeoutId);
99+
timeoutId = null;
100+
}
101+
};
102+
103+
const captureAndScheduleNext = async () => {
104+
if (!shouldCapture()) {
105+
cancelNextCapture();
106+
return;
107+
}
108+
86109
const titles = await captureWindowTitles(processId);
87110

88111
(titles || []).forEach((title) => {
@@ -95,12 +118,10 @@ function beginCapturingTitles(
95118
}
96119
});
97120

98-
if (!processDetails.isDone) {
99-
setTimeout(capture, WINDOW_TITLE_CAPTURE_INTERVAL_IN_MS);
100-
}
121+
scheduleNextCapture();
101122
};
102123

103-
capture();
124+
captureAndScheduleNext();
104125
}
105126

106127
function handleProcessEvents(

0 commit comments

Comments
 (0)