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): fail rasterizer startup on a non-headless-shell browser (#95849)

posthog[bot]0907605f2ca3
diff --git a/nodejs/src/session-replay/recording-rasterizer/__tests__/browser-pool.test.ts b/nodejs/src/session-replay/recording-rasterizer/__tests__/browser-pool.test.ts
index 83b7b34e9b7a..508847d29f56 100644
--- a/nodejs/src/session-replay/recording-rasterizer/__tests__/browser-pool.test.ts
+++ b/nodejs/src/session-replay/recording-rasterizer/__tests__/browser-pool.test.ts
@@ -24,9 +24,10 @@ const puppeteerCapture = require('puppeteer-capture')
const ORIGINAL_ENV = process.env
-function mockBrowser(): jest.Mocked<Browser> {
+function mockBrowser(spawnfile = '/usr/local/bin/chrome-headless-shell'): jest.Mocked<Browser> {
const handlers: Record<string, () => void> = {}
return {
+ process: jest.fn(() => ({ spawnfile })),
newPage: jest.fn(),
close: jest.fn(),
on: jest.fn((event: string, handler: () => void) => {
@@ -96,6 +97,22 @@ describe('BrowserPool', () => {
expect(pool.stats.activePages).toBe(0)
})
+ it('rejects launch() and closes the browser when it is not chrome-headless-shell', async () => {
+ const browser = mockBrowser('/usr/bin/chromium')
+ puppeteerCapture.launch.mockResolvedValue(browser)
+
+ pool = new BrowserPool(100)
+ await expect(pool.launch()).rejects.toThrow('/usr/bin/chromium')
+ expect(browser.close).toHaveBeenCalled()
+
+ // The refused browser must not stay in the idle pool for getPage to hand out.
+ const good = mockBrowser()
+ good.newPage.mockResolvedValue(mockPage())
+ puppeteerCapture.launch.mockResolvedValue(good)
+ await pool.getPage()
+ expect(puppeteerCapture.launch).toHaveBeenCalledTimes(2)
+ })
+
it('closes the browser instead of orphaning it when newPage throws', async () => {
const browser = mockBrowser()
browser.newPage.mockRejectedValue(new Error('Target closed'))
diff --git a/nodejs/src/session-replay/recording-rasterizer/capture/browser-pool.ts b/nodejs/src/session-replay/recording-rasterizer/capture/browser-pool.ts
index 89af56efa549..c340b502259c 100644
--- a/nodejs/src/session-replay/recording-rasterizer/capture/browser-pool.ts
+++ b/nodejs/src/session-replay/recording-rasterizer/capture/browser-pool.ts
@@ -103,10 +103,21 @@ export class BrowserPool {
}
}
+ // puppeteer-capture only rejects a non-chrome-headless-shell binary when a capture attaches. This check
+ // runs before the worker reports ready, so a bad PUPPETEER_EXECUTABLE_PATH fails the pod instead of every render.
async launch(): Promise<void> {
- if (this.idle.length === 0) {
- this.idle.push(await this.launchBrowser())
+ if (this.idle.length > 0) {
+ return
+ }
+ const slot = await this.launchBrowser()
+ const spawnfile = slot.browser.process()?.spawnfile
+ if (!spawnfile?.includes('chrome-headless-shell')) {
+ await this.closeBrowser(slot)
+ throw new Error(
+ `Browser is not chrome-headless-shell: "${spawnfile ?? 'unknown'}". Set PUPPETEER_EXECUTABLE_PATH to a chrome-headless-shell binary`
+ )
}
+ this.idle.push(slot)
}
async getPage(): Promise<Page> {