Skip to content

Commit 6e57a6a

Browse files
miga-heygenclaude
andcommitted
fix: address review feedback — widen retry scope, deduplicate patterns
- Move createCaptureSession inside the retry try/catch so browser launch failures (Failed to launch the browser process, ECONNREFUSED) are also retried — not just initializeSession errors. - Deduplicate transient error patterns: remove "Protocol error.*Target closed" (subsumed by "Target closed") and "Navigation failed because browser has disconnected" (subsumed by "browser has disconnected"). - Add browser launch failure patterns: "Failed to launch the browser process" and "ECONNREFUSED". - Add test for createCaptureSession transient throw (browser launch retry). - Update test mock comment to document sync requirement with engine pattern list. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 8deb82f commit 6e57a6a

4 files changed

Lines changed: 68 additions & 34 deletions

File tree

‎packages/engine/src/services/frameCapture-transientErrors.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ describe("isTransientBrowserError", () => {
77
"Target closed",
88
"Session closed. Most likely the page has been closed.",
99
"Protocol error (Runtime.callFunctionOn): Target closed",
10-
"Protocol error (Page.navigate): Target closed",
1110
"Navigation failed because browser has disconnected",
1211
"browser has disconnected",
1312
"Page crashed!",
1413
"Execution context was destroyed",
1514
"Cannot find context with specified id",
15+
"Failed to launch the browser process! TROUBLESHOOTING: https://pptr.dev/troubleshooting",
16+
"connect ECONNREFUSED 127.0.0.1:9222",
1617
])("returns true for transient error: %s", (message) => {
1718
expect(isTransientBrowserError(new Error(message))).toBe(true);
1819
});

‎packages/engine/src/services/frameCapture.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,18 +1936,18 @@ export function getCapturePerfSummary(session: CaptureSession): CapturePerfSumma
19361936
// ── Transient browser error classification ─────────────────────────────────
19371937
// Puppeteer/Chrome can fail with transient errors that succeed on retry with a
19381938
// fresh browser session. These are infrastructure-level failures (frame
1939-
// detachment, connection drop, OOM kill) — NOT composition bugs.
1939+
// detachment, connection drop, OOM kill, launch failure) — NOT composition bugs.
19401940

19411941
const TRANSIENT_BROWSER_ERROR_PATTERNS = [
19421942
/Navigating frame was detached/i,
19431943
/Target closed/i,
19441944
/Session closed/i,
1945-
/Protocol error.*Target closed/i,
1946-
/Navigation failed because browser has disconnected/i,
19471945
/browser has disconnected/i,
19481946
/Page crashed/i,
19491947
/Execution context was destroyed/i,
19501948
/Cannot find context with specified id/i,
1949+
/Failed to launch the browser process/i,
1950+
/ECONNREFUSED/i,
19511951
];
19521952

19531953
export function isTransientBrowserError(error: unknown): boolean {

‎packages/producer/src/services/render/stages/probeStage.test.ts‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@ const mockPage = {
2020
let initializeSessionCallCount = 0;
2121
let initializeSessionFailUntilAttempt = 0;
2222
let initializeSessionError: Error | null = null;
23+
let createSessionCallCount = 0;
24+
let createSessionFailUntilAttempt = 0;
25+
let createSessionError: Error | null = null;
2326
let closeCaptureSessionCallCount = 0;
2427

2528
function resetRetryMocks() {
2629
initializeSessionCallCount = 0;
2730
initializeSessionFailUntilAttempt = 0;
2831
initializeSessionError = null;
32+
createSessionCallCount = 0;
33+
createSessionFailUntilAttempt = 0;
34+
createSessionError = null;
2935
closeCaptureSessionCallCount = 0;
3036
}
3137

@@ -37,7 +43,11 @@ mock.module("@hyperframes/engine", () => ({
3743
_nullArg: unknown,
3844
cfg: unknown,
3945
) => {
46+
createSessionCallCount++;
4047
capturedCfgs.push(cfg);
48+
if (createSessionError && createSessionCallCount <= createSessionFailUntilAttempt) {
49+
throw createSessionError;
50+
}
4151
return {
4252
isInitialized: false,
4353
browserConsoleBuffer: [],
@@ -55,10 +65,12 @@ mock.module("@hyperframes/engine", () => ({
5565
closeCaptureSession: async () => {
5666
closeCaptureSessionCallCount++;
5767
},
68+
// Mirror of the real engine classifier. Canonical tests + pattern list
69+
// live in frameCapture-transientErrors.test.ts — update both if patterns change.
5870
isTransientBrowserError: (error: unknown) => {
59-
const message = error instanceof Error ? error.message : String(error);
60-
return /Navigating frame was detached|Target closed|Session closed|Protocol error.*Target closed|browser has disconnected|Page crashed|Execution context was destroyed|Cannot find context with specified id/i.test(
61-
message,
71+
const msg = error instanceof Error ? error.message : String(error);
72+
return /Navigating frame was detached|Target closed|Session closed|browser has disconnected|Page crashed|Execution context was destroyed|Cannot find context with specified id|Failed to launch the browser process|ECONNREFUSED/i.test(
73+
msg,
6274
);
6375
},
6476
}));
@@ -307,4 +319,21 @@ describe("runProbeStage — transient browser error retry (#1687)", () => {
307319
expect(initializeSessionCallCount).toBe(2);
308320
expect(closeCaptureSessionCallCount).toBe(2);
309321
});
322+
323+
it("retries on a transient browser LAUNCH failure (createCaptureSession throws)", async () => {
324+
resetRetryMocks();
325+
capturedCfgs.length = 0;
326+
createSessionError = new Error("Failed to launch the browser process!");
327+
createSessionFailUntilAttempt = 1;
328+
329+
const { runProbeStage } = await import("./probeStage.js");
330+
const input = makeProbeInput({ cfgForceScreenshot: false, stageForceScreenshot: false });
331+
332+
const result = await runProbeStage(input);
333+
334+
expect(createSessionCallCount).toBe(2);
335+
expect(closeCaptureSessionCallCount).toBe(0);
336+
expect(result.duration).toBe(5);
337+
expect(result.probeSession).not.toBeNull();
338+
});
310339
});

‎packages/producer/src/services/render/stages/probeStage.ts‎

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -180,43 +180,47 @@ export async function runProbeStage(input: ProbeStageInput): Promise<ProbeStageR
180180

181181
const PROBE_MAX_ATTEMPTS = 2;
182182
for (let attempt = 1; attempt <= PROBE_MAX_ATTEMPTS; attempt++) {
183-
log.info("Creating capture session...", { attempt, maxAttempts: PROBE_MAX_ATTEMPTS });
184-
probeSession = await createCaptureSession(
185-
fileServer.url,
186-
join(workDir, "probe"),
187-
captureOpts,
188-
null,
189-
probeCfg,
190-
);
191-
log.info("Waiting for composition to initialize...", { attempt });
192-
const initStart = Date.now();
193-
const heartbeat = setInterval(() => {
194-
const elapsed = ((Date.now() - initStart) / 1000).toFixed(1);
195-
log.info(`Still waiting for browser initialization... (${elapsed}s elapsed)`);
196-
}, 30_000);
183+
const attemptStart = Date.now();
197184
try {
198-
await initializeSession(probeSession);
199-
clearInterval(heartbeat);
185+
log.info("Creating capture session...", { attempt, maxAttempts: PROBE_MAX_ATTEMPTS });
186+
probeSession = await createCaptureSession(
187+
fileServer.url,
188+
join(workDir, "probe"),
189+
captureOpts,
190+
null,
191+
probeCfg,
192+
);
193+
log.info("Waiting for composition to initialize...", { attempt });
194+
const heartbeat = setInterval(() => {
195+
const elapsed = ((Date.now() - attemptStart) / 1000).toFixed(1);
196+
log.info(`Still waiting for browser initialization... (${elapsed}s elapsed)`);
197+
}, 30_000);
198+
try {
199+
await initializeSession(probeSession);
200+
} finally {
201+
clearInterval(heartbeat);
202+
}
200203
} catch (err) {
201-
clearInterval(heartbeat);
202204
const isTransient = isTransientBrowserError(err);
203205
const errMsg = err instanceof Error ? err.message : String(err);
204-
log.warn("Browser session initialization failed", {
206+
log.warn("Browser probe attempt failed", {
205207
attempt,
206208
maxAttempts: PROBE_MAX_ATTEMPTS,
207209
isTransient,
208210
error: errMsg,
209-
elapsedMs: Date.now() - initStart,
211+
elapsedMs: Date.now() - attemptStart,
210212
});
211213

212-
try {
213-
await closeCaptureSession(probeSession);
214-
} catch (closeErr) {
215-
log.warn("Failed to close crashed probe session", {
216-
error: closeErr instanceof Error ? closeErr.message : String(closeErr),
217-
});
214+
if (probeSession) {
215+
try {
216+
await closeCaptureSession(probeSession);
217+
} catch (closeErr) {
218+
log.warn("Failed to close crashed probe session", {
219+
error: closeErr instanceof Error ? closeErr.message : String(closeErr),
220+
});
221+
}
222+
probeSession = null;
218223
}
219-
probeSession = null;
220224

221225
if (isTransient && attempt < PROBE_MAX_ATTEMPTS) {
222226
log.info("Retrying with a fresh browser session...", {
@@ -230,7 +234,7 @@ export async function runProbeStage(input: ProbeStageInput): Promise<ProbeStageR
230234
}
231235
log.info("Composition ready", {
232236
attempt,
233-
initMs: Date.now() - initStart,
237+
initMs: Date.now() - attemptStart,
234238
});
235239
break;
236240
}

0 commit comments

Comments
 (0)