{"record":{"id":"77d28634cd8468a6","repo":"grafana/k6","slug":"updating-geo-location-in-target-id-s-w","errorCode":null,"errorMessage":"updating geo location in target ID %s: %w","messagePattern":"updating geo location in target ID (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser_context.go","lineNumber":301,"sourceCode":"// SetDefaultTimeout sets the default maximum timeout in milliseconds.\nfunc (b *BrowserContext) SetDefaultTimeout(timeout int64) {\n\tb.logger.Debugf(\"BrowserContext:SetDefaultTimeout\", \"bctxid:%v timeout:%d\", b.id, timeout)\n\n\tb.timeoutSettings.setDefaultTimeout(time.Duration(timeout) * time.Millisecond)\n}\n\n// SetGeolocation overrides the geo location of the user.\nfunc (b *BrowserContext) SetGeolocation(g *Geolocation) error {\n\tb.logger.Debugf(\"BrowserContext:SetGeolocation\", \"bctxid:%v\", b.id)\n\n\tif err := g.Validate(); err != nil {\n\t\treturn fmt.Errorf(\"validating geo location: %w\", err)\n\t}\n\n\tb.opts.Geolocation = g\n\tfor _, p := range b.browser.getPages() {\n\t\tif err := p.updateGeolocation(); err != nil {\n\t\t\treturn fmt.Errorf(\"updating geo location in target ID %s: %w\", p.targetID, err)\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// SetHTTPCredentials sets username/password credentials to use for HTTP authentication.\n//\n// Deprecated: Create a new BrowserContext with httpCredentials instead.\n// See for details:\n// - https://github.com/microsoft/playwright/issues/2196#issuecomment-627134837\n// - https://github.com/microsoft/playwright/pull/2763\nfunc (b *BrowserContext) SetHTTPCredentials(hc Credentials) error {\n\tb.logger.Warnf(\"setHTTPCredentials\", \"setHTTPCredentials is deprecated.\"+\n\t\t\" Create a new BrowserContext with httpCredentials instead.\")\n\tb.logger.Debugf(\"BrowserContext:SetHTTPCredentials\", \"bctxid:%v\", b.id)\n\n\tb.opts.HTTPCredentials = hc","sourceCodeStart":283,"sourceCodeEnd":319,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/browser/common/browser_context.go#L283-L319","documentation":"The geolocation passed validation and was stored on the context (b.opts.Geolocation = g), but propagating it to one of the already-open pages failed: each page runs Emulation.setGeolocationOverride on its own session, and the page named by targetID rejected the command. Pages processed before the failing one are already updated; the failing page keeps its old override.","triggerScenarios":"Calling setGeolocation while some pages in the context are closed or closing (their sessions are gone), their targets crashed, or the browser connection dropped mid-loop.","commonSituations":"setGeolocation called mid-test while parallel iterations close their pages; geolocation changes in teardown after pages died; long-running tests where a tab crashed earlier.","solutions":["Set geolocation when creating the context — browser.newContext({ geolocation: {...} }) — so it applies before pages exist","Ensure all pages you expect to update are alive; close and reopen pages rather than reusing stale ones","Retry the call after the page churn settles if it was a transient teardown race","Inspect the wrapped error to distinguish a dead session from a dead browser"],"exampleFix":"// before\nconst ctx = browser.newContext();\nconst page = ctx.newPage();\n// ... later, page already closed\nctx.setGeolocation({ latitude: 35.68, longitude: 139.69 }); // fails on dead page\n\n// after\nconst ctx = browser.newContext({ geolocation: { latitude: 35.68, longitude: 139.69 } });\nconst page = ctx.newPage(); // page is born with the override","handlingStrategy":"try-catch","validationCode":"// avoid mid-test propagation entirely: set geolocation at context creation\nconst ctx = browser.newContext({\n  geolocation: { latitude: 35.68, longitude: 139.69 },\n});","typeGuard":null,"tryCatchPattern":"try {\n  ctx.setGeolocation(geo);\n} catch (e) {\n  if (String(e.message).includes('updating geo location in target ID')) {\n    // one page's session was dead: close stale pages and retry once\n    for (const p of ctx.pages()) { try { p.close(); } catch (_) {} }\n    ctx.setGeolocation(geo);\n  } else {\n    throw e;\n  }\n}","preventionTips":["Prefer the geolocation context option over setGeolocation mid-test","Ensure all pages in the context are alive before context-wide updates","Don't change geolocation during teardown"],"tags":["browser","cdp","geolocation","browser-context"],"backgroundTag":"cdp-protocol-error","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}