Skip to content

fix(engine): retry transient screenshot refusals in place - #9219

Queued
miguel-heygen wants to merge 2 commits into
mainfrom
dcloudpro/screenshot-retry
Queued

miguel-heygen wants to merge 2 commits into
mainfrom
dcloudpro/screenshot-retry

Conversation

@miguel-heygen

Copy link
Copy Markdown
Collaborator

Summary

Chrome's Page.captureScreenshot CDP call can transiently reject a single frame with Protocol error (Page.captureScreenshot): Unable to capture screenshot during long or busy renders. Previously, pageScreenshotCapture, captureAlphaPng, captureScreenshotWithAlpha, and the page-side compositing micro-screenshot did zero in-place retries, so one refusal immediately aborted the worker pool and discarded captured frames; segmented renders also omitted this refusal from isTargetLossError, so they never recycled the browser and retried the segment.

Changes

  • Single-owner classifier (packages/engine/src/services/captureFailure.ts): extract isTransientScreenshotRefusal anchored to Protocol error (Page.captureScreenshot): Unable to capture screenshot and reuse the same pattern inside TRANSIENT_BROWSER_ERROR_PATTERNS.
  • Bounded in-place retry (packages/engine/src/services/screenshotService.ts, packages/engine/src/services/frameCapture.ts): route pageScreenshotCapture, captureAlphaPng, captureScreenshotWithAlpha, and the page-side compositing micro-screenshot through sendCaptureScreenshot, retrying up to 2 times with short exponential backoff (150ms, 300ms) only on isTransientScreenshotRefusal. Deterministic screenshot errors (such as zero-width screenshots), target loss, and timeouts propagate without extra retries; captureScreenshotWithAlpha still restores the background color override once in finally.
  • Segmented fallback (packages/producer/src/services/render/segmentRecycle.ts): include isTransientScreenshotRefusal in isTargetLossError so segmented renders recycle the browser and retry the segment once if in-place per-frame retries are exhausted.
  • Observability (packages/engine/src/types.ts, packages/producer/src/services/render/observability.ts, packages/producer/src/services/renderOrchestrator.ts, packages/cli/src/telemetry/renderObservability.ts, packages/cli/src/telemetry/events.ts): wire onScreenshotRetry into RenderCaptureObservability.screenshotRetries and emit screenshot_retries on render_complete and render_error.

Verification

fails without the fix: packages/engine/src/services/screenshotService.test.ts (3 new retry tests threw Unable to capture screenshot on attempt 1), packages/engine/src/services/frameCapture-screenshotRetry.test.ts (aborted mid-render on frame 1 refusal), packages/producer/src/services/render/segmentRecycle.test.ts (isTargetLossError returned false), packages/producer/src/services/render/stages/captureSegmentedStage.test.ts (failed segment without recycling), packages/cli/src/telemetry/renderObservability.test.ts (captureScreenshotRetries was undefined), and packages/cli/src/telemetry/events.test.ts (screenshot_retries omitted from render_complete/render_error).

  • All added and modified unit tests verified red-first before the fix, then passing 3x in a row with shuffled order:
    • packages/engine/src/services/screenshotService.test.ts (43 tests: single refusal recovery, exhaustion after 2 retries, no retry on deterministic/target-loss/timeout errors, alpha and background-override restore)
    • packages/engine/src/services/frameCapture-screenshotRetry.test.ts (multi-frame captureFrameToBuffer run where a fake page refuses Page.captureScreenshot once mid-render and finishes with every frame intact and onScreenshotRetry counted)
    • packages/engine/src/services/captureFailure.test.ts (27 tests)
    • packages/producer/src/services/render/segmentRecycle.test.ts (3 tests) and packages/producer/src/services/render/stages/captureSegmentedStage.test.ts (41 tests)
    • packages/cli/src/telemetry/renderObservability.test.ts (12 tests) and packages/cli/src/telemetry/events.test.ts (87 tests)
  • Sibling suites passed: renderOrchestrator.test.ts (313), captureStage.test.ts (24), captureStreamingStage.test.ts (62), captureHdrFrameShared.dom-mask.test.ts (1).
  • oxlint, oxfmt --check, fallow audit --base origin/main --fail-on-issues, and tsc --noEmit across engine, producer, cli, core, and scripts all green.

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 993ff0f53f. I read the source changes and new tests at 1eb02aa818, then the delta to this head (two field comments removed, nothing else).

Verdict: approve. No blocker.

Need: yes, one refused screenshot should not end the pool · Size: ok · Reuse: the refusal pattern stays single-owner in captureFailure.ts and the existing segment recycle is reused · Simpler: already minimal, one sendCaptureScreenshot helper for all four call sites · Structure: fine (checked TRANSIENT_BROWSER_ERROR_PATTERNS and isTargetLossError; no split)

Risk: low — bounded retry (2 tries, 150 ms and 300 ms) on one exact message; other errors pass through unchanged.

What I checked

  • Every production Page.captureScreenshot send now goes through sendCaptureScreenshot (pageScreenshotCapture, captureAlphaPng, captureScreenshotWithAlpha, the 1x1 compositing shot in frameCapture.ts). The only raw sends left are test fixtures.
  • Both captureAlphaPng callers (hdrCompositor.ts, captureHdrFrameShared.ts) pass the retry callback.
  • captureScreenshotWithAlpha retries inside its try, so the background override is restored once.
  • The refusal was already classed as transient_browser, so the whole-render fallback still sits behind the in-place retry and the segment recycle. Worst case is bounded.

"fails without the fix:" line is present. I turned the fix off locally and reran:

  • retry disabled in sendCaptureScreenshot: 4 engine tests fail (3 in screenshotService.test.ts, 1 in frameCapture-screenshotRetry.test.ts).
  • refusal removed from isTargetLossError: 2 producer tests fail (segmentRecycle.test.ts, the new captureSegmentedStage.test.ts case).
  • restored: engine 71/71 (3 files), producer 44/44 (2 files), cli 99/99 (2 files).

Not run: a real browser render, the sibling suites listed in the body, lint and typecheck (CI was still pending when I posted).

Nits, none blocking

  • screenshot_retries sits next to capture_transient_retries; capture_screenshot_retries would match the neighbours.
  • A recycled segment after a refusal logs "browser target lost". The target was not lost; a neutral message would read better in a log.
  • The backoff sleep does not watch the abort signal (at most 450 ms).

— Rames

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 275 (base branch 275), smooth 52 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Quarantined, measured but not gated (0)

@miguel-heygen
miguel-heygen added this pull request to the merge queue Dec 1, 2026
Any commits made after this event will not be merged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants