grafana/k6 · error
creating a new %s: %w
Error message
creating a new %s: %w
What it means
Returned by Browser.onAttachedToTarget when NewPage fails for an incoming target (the '%s' is the target type, e.g. 'page' or 'background_page') and the error is not an ignorable page-attachment failure (browser closing races are ignored separately). This error is delivered asynchronously: initEvents' event loop handles it via k6ext.Panicf('browser is attaching to target: ...'), which aborts the VU iteration. It usually means a page/iframe target appeared while the CDP session or context was in a bad state.
Source
Thrown at internal/js/modules/k6/browser/common/browser.go:330
)
// Opener is nil for the initial page.
if isPage {
b.pagesMu.RLock()
if t, ok := b.pages[targetPage.OpenerID]; ok {
opener = t
}
b.pagesMu.RUnlock()
}
p, err := NewPage(b.vuCtx, session, browserCtx, targetPage.TargetID, opener, isPage, b.logger)
if err != nil && b.isPageAttachmentErrorIgnorable(ev, session, err) {
if b.closing.Load() {
b.logger.Debugf("Browser:onAttachedToTarget", "new page failed; browser is closing: sid:%v", ev.SessionID)
detachSession(b.browserCtx, session)
}
return nil // Ignore this page.
}
if err != nil {
return fmt.Errorf("creating a new %s: %w", targetPage.Type, err)
}
// This prevents a race where Close() sets closing and snapshots
// pages, but a new page is inserted outside that snapshot.
if err := b.attachNewPage(p, ev); err != nil {
if !errors.Is(err, errBrowserClosing) {
return fmt.Errorf("attaching new page: %w", err)
}
b.logger.Debugf(
"Browser:onAttachedToTarget",
"rejected page attachment; browser is closing: sid:%v tid:%v",
ev.SessionID, ev.TargetInfo.TargetID,
)
if closeErr := p.Close(); closeErr != nil {
b.logger.Debugf(
"Browser:onAttachedToTarget",
"closing rejected page: %v", closeErr,View on GitHub (pinned to 93accf6570)
Solutions
- Read the full chain ('browser is attaching to target: creating a new page: <cause>') to see whether it is a session/closed error or a CDP protocol error
- Avoid triggering unhandled popups: block them via launch args or handle window.open targets explicitly in the test flow
- If it happens under load, increase K6_BROWSER_TIMEOUT and reduce concurrent browser activity against the same Chrome instance
- Ensure you are not closing pages/contexts concurrently while new targets appear (serialize close and open operations in the script)
- Update k6 — attachment-error classification (isPageAttachmentErrorIgnorable) has been improved over releases; report reproductions to k6 issues
Example fix
// before
page.click('#open-popup'); // popup target arrives while script closes context
browser.close();
// after
const popupPromise = page.waitForEvent('popup');
page.click('#open-popup');
const popup = await popupPromise;
popup.close();
browser.close(); Defensive patterns
Strategy: try-catch
Try / catch
// The error surfaces as an iteration panic ('browser is attaching to target: creating a new page: ...').
// Guard the flows that spawn targets:
try {
const popupPromise = page.waitForEvent('popup');
await page.click('#open-popup');
const popup = await popupPromise;
await popup.close();
} catch (e) {
if (String(e.message).includes('creating a new')) {
console.error('A target attached while the session/context was unstable; serialize opens/closes:', e.message);
}
throw e;
} Prevention
- Handle popups and window.open targets explicitly with waitForEvent('popup') instead of letting them arrive unmanaged
- Serialize page/context open and close operations; never close while targets may attach
- Upgrade k6 regularly — target-attachment error handling keeps improving
- Keep remote Chrome load within limits so sessions do not drop mid-attach
When it happens
Trigger: A new page or popup attaching while the connection is degraded or the session was already detached; NewPage's internal setup CDP calls failing (e.g. target crashed concurrently); pages created by the site under test (window.open, popups, OAuth flows) arriving during teardown races that are not classified as ignorable; Chrome extensions spawning background pages with malformed targets.
Common situations: Tests clicking links that trigger popups/OAuth redirects while k6 is also navigating/closing; remote Chrome under heavy load dropping sessions; site under test spawning many targets; usually surfaces as an iteration abort with 'browser is attaching to target: creating a new page: ...'.
Related errors
- parent frame has been detached
- disposing browser context ID %s: %w
- attaching new page: %w
- adding init script to browser context: %w
- updating geo location in target ID %s: %w
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/e18f163ac632c3d7.
Report an issue: GitHub.