{"record":{"id":"91615bebb56e5bc0","repo":"grafana/k6","slug":"browser-on-promise-rejected-w","errorCode":null,"errorMessage":"browser.on promise rejected: %w","messagePattern":"browser\\.on promise rejected: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/js/modules/k6/browser/common/browser.go","lineNumber":778,"sourceCode":"\tif err != nil {\n\t\treturn nil, spanRecordError(span, err)\n\t}\n\n\treturn page, nil\n}\n\n// On returns a Promise that is resolved when the browser process is disconnected.\n// The only accepted event value is \"disconnected\".\nfunc (b *Browser) On(event string) (bool, error) {\n\tif event != EventBrowserDisconnected {\n\t\treturn false, fmt.Errorf(\"unknown browser event: %q, must be %q\", event, EventBrowserDisconnected)\n\t}\n\n\tselect {\n\tcase <-b.browserProc.lostConnection:\n\t\treturn true, nil\n\tcase <-b.vuCtx.Done():\n\t\treturn false, fmt.Errorf(\"browser.on promise rejected: %w\", ContextErr(b.vuCtx))\n\t}\n}\n\n// UserAgent returns the controlled browser's user agent string.\nfunc (b *Browser) UserAgent() string {\n\treturn b.version.userAgent\n}\n\n// Version returns the controlled browser's version.\nfunc (b *Browser) Version() string {\n\tproduct := b.version.product\n\t_, after, ok := strings.Cut(product, \"/\")\n\tif !ok {\n\t\treturn product\n\t}\n\treturn after\n}\n","sourceCodeStart":760,"sourceCodeEnd":796,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/browser.go#L760-L796","documentation":"browser.on('disconnected') blocks until the browser's WS connection is lost, but it also watches the VU context. This error fires when the iteration context finishes first — the test ended, was aborted, or hit its deadline — wrapping the context.Canceled/DeadlineExceeded via ContextErr. It usually means the script was still waiting for a disconnect that never came before teardown.","triggerScenarios":"Awaiting browser.on('disconnected') in a script whose iteration is canceled: end of scenario duration, aborted run (SIGINT), graceful-stop timeout, or a per-iteration timeout firing while the browser stays connected.","commonSituations":"Scripts that block on disconnected indefinitely while the browser stays alive; long tests stopped by CI timeouts; k6 aborting the VU during teardown while a promise is pending.","solutions":["Do not await 'disconnected' unbounded — close the browser explicitly so the event fires before iteration end","Treat this error during teardown as a normal exit (the test is over), not a failure","Raise scenario/iteration timeouts if waiting for disconnect is intentional","Structure scripts to await browser.close() rather than waiting for external disconnects"],"exampleFix":"// before\nbrowser.on('disconnected'); // blocks; VU canceled first -> promise rejected\n// script ends without closing browser\n\n// after\nawait browser.close(); // deterministic teardown; disconnect fires immediately","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  browser.on('disconnected');\n} catch (e) {\n  if (/browser\\.on promise rejected/.test(String(e))) {\n    // VU context ended before disconnect — normal during teardown; exit gracefully\n    console.warn('iteration ended while waiting for browser disconnect');\n  } else throw e;\n}","preventionTips":["Always await browser.close() at script end instead of waiting for external disconnects","Do not block on 'disconnected' in iterations with tight deadlines","Treat context-cancellation errors during teardown as expected"],"tags":["browser","events","context-canceled","teardown","promise"],"backgroundTag":"context-deadline-exceeded","analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}