Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(replay): classify chrome target closed as a typed retryable error (#88356)

posthog[bot]b880f6e3d946
diff --git a/nodejs/src/session-replay/recording-rasterizer/__tests__/activities.test.ts b/nodejs/src/session-replay/recording-rasterizer/__tests__/activities.test.ts
index 649d59cc2a8e..812dcb4a067c 100644
--- a/nodejs/src/session-replay/recording-rasterizer/__tests__/activities.test.ts
+++ b/nodejs/src/session-replay/recording-rasterizer/__tests__/activities.test.ts
@@ -238,6 +238,24 @@ describe('rasterizeRecordingActivity', () => {
expect(ApplicationFailure.nonRetryable).not.toHaveBeenCalled()
})
+ it('classifies a raw puppeteer "Target closed" as a retryable TARGET_CLOSED failure', async () => {
+ // The Chrome target dying mid-render rejects the in-flight CDP call untyped; the activity
+ // boundary must turn it into one retryable code so it stops fragmenting into a fresh
+ // error-tracking issue per CDP method.
+ mockedRasterizeRecording.mockRejectedValue(
+ new Error('Protocol error (Page.captureScreenshot): Target closed')
+ )
+
+ await expect(rasterizeRecordingActivity(baseInput())).rejects.toThrow('chrome target closed mid-render')
+
+ expect(ApplicationFailure.retryable).toHaveBeenCalledWith(
+ 'chrome target closed mid-render',
+ 'TARGET_CLOSED',
+ expect.objectContaining({ code: 'TARGET_CLOSED' })
+ )
+ expect(ApplicationFailure.nonRetryable).not.toHaveBeenCalled()
+ })
+
it('re-throws unknown Error as-is (Temporal retries by default)', async () => {
const error = new Error('unexpected failure')
mockedRasterizeRecording.mockRejectedValue(error)
diff --git a/nodejs/src/session-replay/recording-rasterizer/__tests__/errors.test.ts b/nodejs/src/session-replay/recording-rasterizer/__tests__/errors.test.ts
index 218500ebe5c7..533184f88d1a 100644
--- a/nodejs/src/session-replay/recording-rasterizer/__tests__/errors.test.ts
+++ b/nodejs/src/session-replay/recording-rasterizer/__tests__/errors.test.ts
@@ -1,4 +1,4 @@
-import { RasterizationError } from '~/session-replay/recording-rasterizer/errors'
+import { RasterizationError, asRasterizationError } from '~/session-replay/recording-rasterizer/errors'
describe('RasterizationError', () => {
it('sets name, message, retryable, and code', () => {
@@ -44,4 +44,38 @@ describe('RasterizationError', () => {
expect(json).not.toHaveProperty('stack')
})
})
+
+ describe('asRasterizationError', () => {
+ it('returns a RasterizationError unchanged', () => {
+ const err = new RasterizationError('boom', false, 'NO_SNAPSHOTS')
+ expect(asRasterizationError(err)).toBe(err)
+ })
+
+ // One case per way puppeteer words a dead target, because that is what varies. The CDP
+ // method name in the message does not: the classifier never reads it, so a case per method
+ // would be the same assertion three times.
+ it.each([
+ ['bare rejection from the in-flight callback', 'Target closed', 'Error'],
+ [
+ 'CDP session already closed',
+ 'Protocol error (Page.captureScreenshot): Session closed. Most likely the page has been closed.',
+ 'Error',
+ ],
+ ['a wording only the error name catches', 'Page closed!', 'TargetCloseError'],
+ ])('classifies %s as a retryable TARGET_CLOSED', (_label, message, name) => {
+ const raw = new Error(message)
+ raw.name = name
+ const classified = asRasterizationError(raw)
+ expect(classified).toMatchObject({
+ code: 'TARGET_CLOSED',
+ retryable: true,
+ message: 'chrome target closed mid-render',
+ })
+ expect(classified?.cause).toBe(raw)
+ })
+
+ it('returns null for an unrelated error', () => {
+ expect(asRasterizationError(new Error('S3 access denied'))).toBeNull()
+ })
+ })
})
diff --git a/nodejs/src/session-replay/recording-rasterizer/errors.ts b/nodejs/src/session-replay/recording-rasterizer/errors.ts
index 095449db4463..0f9b029c1a7c 100644
--- a/nodejs/src/session-replay/recording-rasterizer/errors.ts
+++ b/nodejs/src/session-replay/recording-rasterizer/errors.ts
@@ -6,6 +6,7 @@ export const RASTERIZATION_ERROR_CODES = [
'OTHER',
'TIMEOUT',
'CAPTURE_ABORTED',
+ 'TARGET_CLOSED',
'BEGINFRAME_DEADLOCK',
'INVALID_INPUT',
'BLOCK_LISTING_FAILED',
@@ -45,3 +46,26 @@ export class RasterizationError extends Error {
}
}
}
+
+// Puppeteer rejects whatever CDP call is in flight when the Chrome target dies mid-render. Some of
+// those rejections name the CDP method ("Protocol error (Page.captureScreenshot): Session closed.
+// Most likely the page has been closed."), so left raw they mint a distinct error-tracking
+// fingerprint per method. Classify once, at the activity boundary, to a single retryable code with a
+// stable message, keeping the original as the cause for logs.
+//
+// Match on the error name first: puppeteer raises every one of these as TargetCloseError but words
+// the message six different ways ("Target closed", "Session closed. Most likely the ... has been
+// closed.", "Page closed!", "Frame detached."). Matching one wording is what let the earlier
+// variants through. The message checks stay as a fallback for a wrapped or re-thrown rejection that
+// lost the prototype.
+export function asRasterizationError(err: unknown): RasterizationError | null {
+ if (err instanceof RasterizationError) {
+ return err
+ }
+ const name = err instanceof Error ? err.name : ''
+ const message = err instanceof Error ? err.message : String(err)
+ if (name === 'TargetCloseError' || message.includes('Target closed') || message.includes('Session closed')) {
+ return new RasterizationError('chrome target closed mid-render', true, 'TARGET_CLOSED', err)
+ }
+ return null
+}
diff --git a/nodejs/src/session-replay/recording-rasterizer/temporal/activities.ts b/nodejs/src/session-replay/recording-rasterizer/temporal/activities.ts
index 71eb2c4aa195..1cc72c818b54 100644
--- a/nodejs/src/session-replay/recording-rasterizer/temporal/activities.ts
+++ b/nodejs/src/session-replay/recording-rasterizer/temporal/activities.ts
@@ -8,7 +8,7 @@ import * as path from 'path'
import { BrowserPool } from '~/session-replay/recording-rasterizer/capture/browser-pool'
import { rasterizeRecording } from '~/session-replay/recording-rasterizer/capture/recorder'
import { config } from '~/session-replay/recording-rasterizer/config'
-import { RasterizationError } from '~/session-replay/recording-rasterizer/errors'
+import { asRasterizationError } from '~/session-replay/recording-rasterizer/errors'
import { createLogger } from '~/session-replay/recording-rasterizer/logger'
import { RasterizationMetrics } from '~/session-replay/recording-rasterizer/metrics'
import { computeVideoTimestamps } from '~/session-replay/recording-rasterizer/postprocess'
@@ -22,14 +22,15 @@ import {
import { elapsed } from '~/session-replay/recording-rasterizer/utils'
function toActivityError(err: unknown): Error {
- if (err instanceof RasterizationError) {
+ const rasterizationError = asRasterizationError(err)
+ if (rasterizationError) {
// The code travels as the failure type either way, so a caller can tell a recording that can never render
// (NO_SNAPSHOTS) from one that merely ran out of retries. Retryability stays the player's call: NO_SNAPSHOTS is
// retryable while a recording is still being ingested, so the code is only conclusive once Temporal has spent
// the attempts.
- return err.retryable
- ? ApplicationFailure.retryable(err.message, err.code, err)
- : ApplicationFailure.nonRetryable(err.message, err.code, err)
+ return rasterizationError.retryable
+ ? ApplicationFailure.retryable(rasterizationError.message, rasterizationError.code, rasterizationError)
+ : ApplicationFailure.nonRetryable(rasterizationError.message, rasterizationError.code, rasterizationError)
}
return err instanceof Error ? err : new Error(String(err))
}
@@ -192,12 +193,21 @@ async function rasterizeRecordingActivity(
} else {
RasterizationMetrics.observeUpload('error', failedStageS)
}
- if (err instanceof RasterizationError) {
- RasterizationMetrics.incrementError(err.code, err.retryable)
+ const rasterizationError = asRasterizationError(err)
+ if (rasterizationError) {
+ RasterizationMetrics.incrementError(rasterizationError.code, rasterizationError.retryable)
} else {
RasterizationMetrics.incrementError('UNKNOWN', true)
}
- throw toActivityError(err)
+ // Classification collapses the raw rejection into a stable ApplicationFailure message that
+ // drops the CDP method and stack. Log the original here, the last point that still holds it,
+ // so a setup-phase death (browser-pool logs nothing at error level) stays diagnosable.
+ // Non-retryable codes are the recording's own fault (NO_SNAPSHOTS, INVALID_INPUT) and are
+ // routine, so they stay off the error level that infra alerts on.
+ const logAtErrorLevel = !rasterizationError || rasterizationError.retryable
+ const logFailure = logAtErrorLevel ? log.error.bind(log) : log.warn.bind(log)
+ logFailure({ err, code: rasterizationError?.code ?? 'UNKNOWN' }, 'rasterization failed')
+ throw toActivityError(rasterizationError ?? err)
} finally {
clearInterval(heartbeatInterval)
ctx.cancellationSignal.removeEventListener('abort', onCancel)
diff --git a/posthog/temporal/tests/session_replay/rasterize_recording/test_record_failure.py b/posthog/temporal/tests/session_replay/rasterize_recording/test_record_failure.py
index 660d935015bc..0de5389d3829 100644
--- a/posthog/temporal/tests/session_replay/rasterize_recording/test_record_failure.py
+++ b/posthog/temporal/tests/session_replay/rasterize_recording/test_record_failure.py
@@ -51,6 +51,7 @@ class TestRecordRasterizationFailure:
("compositor_deadlock", "BEGINFRAME_DEADLOCK", FAILURE_TYPE_TIMEOUT_GENERATION),
("no_snapshots", "NO_SNAPSHOTS", FAILURE_TYPE_USER),
("upload_failed", "S3_UPLOAD_UNDECODABLE_RESPONSE", FAILURE_TYPE_SYSTEM),
+ ("target_closed", "TARGET_CLOSED", FAILURE_TYPE_SYSTEM),
("worker_death_timeout", "ACTIVITY_TIMEOUT", FAILURE_TYPE_TIMEOUT_GENERATION),
# The renderer's catch-alls have their own buckets so they can't hide in "unknown".
("renderer_crash", "UNKNOWN", FAILURE_TYPE_RENDERER_UNKNOWN),
diff --git a/products/exports/backend/tasks/failure_handler.py b/products/exports/backend/tasks/failure_handler.py
index 915feca6ea94..128011fa87cc 100644
--- a/products/exports/backend/tasks/failure_handler.py
+++ b/products/exports/backend/tasks/failure_handler.py
@@ -71,8 +71,10 @@ RASTERIZATION_CODE_TO_FAILURE_TYPE: dict[str, str] = {
# Reaching the recording's data failed, which nobody exporting it can do anything about.
"DATA_LOAD_FAILED": FAILURE_TYPE_SYSTEM,
"S3_UPLOAD_UNDECODABLE_RESPONSE": FAILURE_TYPE_SYSTEM,
+ "S3_UPLOAD_FAILED": FAILURE_TYPE_SYSTEM,
"INIT_FAILED": FAILURE_TYPE_SYSTEM,
"BLOCK_LISTING_FAILED": FAILURE_TYPE_SYSTEM,
+ "TARGET_CLOSED": FAILURE_TYPE_SYSTEM,
# The render activity died without producing a code at all: heartbeat or start-to-close timeout
# from a lost or wedged worker. Not the renderer's own TIMEOUT, but still a render that ran out
# of time. Resolved in the workflow's _record_failure, not a rasterizer code.
@@ -93,8 +95,10 @@ _RASTERIZATION_MESSAGES: dict[str, str] = {
"INVALID_INPUT": "This export request was not valid. Contact support if it keeps happening.",
"DATA_LOAD_FAILED": "We could not load this recording's data. Try the export again in a few minutes.",
"S3_UPLOAD_UNDECODABLE_RESPONSE": "The finished video could not be saved. Try the export again.",
+ "S3_UPLOAD_FAILED": "The finished video could not be saved. Try the export again.",
"INIT_FAILED": "The video renderer could not start. Try the export again.",
"BLOCK_LISTING_FAILED": "We could not read this recording. Try the export again in a few minutes.",
+ "TARGET_CLOSED": "The video renderer stopped before it finished. Try the export again.",
"ACTIVITY_TIMEOUT": "This recording took too long to render. Try exporting a shorter part of it.",
}