{"record":{"id":"6427a8d503899270","repo":"grafana/k6","slug":"capturing-screenshot-w","errorCode":null,"errorMessage":"capturing screenshot: %w","messagePattern":"capturing screenshot: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/screenshotter.go","lineNumber":243,"sourceCode":"\tscale := 1.0\n\tif viewport != nil {\n\t\tscale = visualViewportScale\n\t}\n\tclip = &cdppage.Viewport{\n\t\tX:      doc.X,\n\t\tY:      doc.Y,\n\t\tWidth:  doc.Width,\n\t\tHeight: doc.Height,\n\t\tScale:  scale,\n\t}\n\tif clip.Width > 0 && clip.Height > 0 {\n\t\tcapture = capture.WithClip(clip)\n\t}\n\n\t// Capture screenshot\n\tbuf, err = capture.Do(cdp.WithExecutor(s.ctx, sess))\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"capturing screenshot: %w\", err)\n\t}\n\n\t// Save screenshot capture to file\n\tif path != \"\" {\n\t\tif err := s.persister.Persist(s.ctx, path, bytes.NewBuffer(buf)); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"persisting screenshot: %w\", err)\n\t\t}\n\t}\n\n\treturn buf, nil\n}\n\nfunc getViewPortDimensions(ctx context.Context, sess session, logger *log.Logger) (float64, float64, float64, error) {\n\tvisualViewportScale := 1.0\n\tvisualViewportPageX, visualViewportPageY := 0.0, 0.0\n\n\t// Add clip region\n\t//nolint:dogsled","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/screenshotter.go#L225-L261","documentation":"The core screenshot failure: the CDP Page.captureScreenshot command (with any accumulated quality/format/clip options) returned an error. Everything before it (background override, layout metrics, clip computation) succeeded; the capture itself failed. The wrapped error carries the CDP reason, most commonly 'target closed' after a renderer crash.","triggerScenarios":"page.screenshot() / elementHandle.screenshot() when the renderer process crashes (OOM), the page navigates away mid-capture, the target is closed by the script or teardown, or Chrome hits an internal error producing the image (e.g. zero-size capture on some GPU-less headless setups).","commonSituations":"Docker/CI Chrome crashes from small /dev/shm or memory limits on large full-page screenshots; screenshotting during redirect churn; GPU rendering issues in headless containers requiring software rendering flags.","solutions":["Read the wrapped CDP error: 'target closed'/'session detached' → renderer died or page closed; fix memory/shm (--disable-dev-shm-usage, bigger container) or guard with page.isClosed().","Retry the screenshot once after a wait — navigation races are often transient.","For huge full-page captures, cap dimensions by clipping or reduce viewport to avoid renderer OOM.","Launch Chromium with --disable-gpu / --disable-dev-shm-usage in containers.","Avoid screenshotting while triggering navigations in the same tick."],"exampleFix":"// before (bare chromium, small shm container)\nawait page.screenshot({ path: 'big.png', fullPage: true });\n\n// after\nconst browser = chromium.launch({\n  args: ['--disable-dev-shm-usage', '--disable-gpu'],\n});\nawait page.screenshot({ path: 'big.png', fullPage: true });","handlingStrategy":"retry","validationCode":"if (page.isClosed()) throw new Error('page closed before screenshot');\nawait page.waitForLoadState('load');","typeGuard":null,"tryCatchPattern":"try {\n  buf = await page.screenshot({ path: 's.png' });\n} catch (e) {\n  if (/capturing screenshot/i.test(String(e)) && !/target closed/i.test(String(e))) {\n    await page.waitForTimeout(500);\n    buf = await page.screenshot({ path: 's.png' });\n  } else throw e; // target closed: renderer/page gone, no point retrying\n}","preventionTips":["Launch Chromium with --disable-dev-shm-usage (and often --disable-gpu) in containers.","Don't screenshot during navigations or teardown.","Cap full-page screenshot size to avoid renderer OOM.","Distinguish retryable capture races from fatal 'target closed' errors before retrying."],"tags":["k6","browser","screenshot","cdp","renderer-crash"],"backgroundTag":"cdp-command-failed","analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-09-20T22:01:45.163Z","contentChangedAt":"2026-09-20T22:01:45.163Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}