grafana/k6 · error
restoring viewport to
Error message
restoring viewport to %s: %w
What it means
Raised at the end of screenshotPage when a full-page screenshot required enlarging the viewport (overriddenViewportSize != nil) and restoring the original viewport via restoreViewport fails. The '%s' is the original viewport size being restored. This is a cleanup-path failure: the screenshot bytes were already captured but the CDP call to reset the device metrics override was rejected, commonly because the page or CDP session closed mid-operation, so the error replaces the successful result.
Solutions
- Keep the page and browser alive until the screenshot call fully completes
- Retry the screenshot on a healthy page/session
- Avoid extremely large full-page captures that stress the target during viewport restore
- Check the wrapped CDP error for the precise override-reset failure
Defensive patterns
Strategy: retry
When it happens
Trigger: Thrown at internal/js/modules/k6/browser/common/screenshotter.go:410 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-09-20).
Data as JSON: /api/errors/358db0b40c110b6f.
Report an issue: GitHub.
Appendix: source
Thrown at internal/js/modules/k6/browser/common/screenshotter.go:400
}
originalSize := p.emulatedSize
if opts.FullPage { //nolint:nestif
fullPageSize, sizeErr := s.fullPageSize(p)
if sizeErr != nil {
return nil, fmt.Errorf("getting full page size: %w", sizeErr)
}
documentRect := &Rect{
X: 0,
Y: 0,
Width: fullPageSize.Width,
Height: fullPageSize.Height,
}
fitsViewport := fullPageSize.Width <= viewportSize.Width && fullPageSize.Height <= viewportSize.Height
if !fitsViewport {
defer func() {
if restoreErr := s.restoreViewport(p, originalSize); restoreErr != nil {
err = errors.Join(err, fmt.Errorf("restoring viewport to %s: %w",
viewportSize, restoreErr))
}
}()
if err := p.setViewportSize(s.ctx, fullPageSize); err != nil {
return nil, fmt.Errorf("setting viewport size to %s: %w",
fullPageSize, err)
}
}
if opts.Clip != nil {
documentRect, err = s.trimClipToSize(&Rect{
X: opts.Clip.X,
Y: opts.Clip.Y,
Width: opts.Clip.Width,
Height: opts.Clip.Height,
}, &Size{Width: documentRect.Width, Height: documentRect.Height})
if err != nil {
return nil, fmt.Errorf("trimming clip to size: %w", err)
}View on GitHub (pinned to 3fcf5388d7)