{"record":{"id":"12e46d5bf0883a76","repo":"grafana/k6","slug":"attaching-worker-target-id-v-to-session-id-v-w","errorCode":null,"errorMessage":"attaching worker target ID %v to session ID %v: %w","messagePattern":"attaching worker target ID (.+?) to session ID (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/frame_session.go","lineNumber":1082,"sourceCode":"\t\t}\n\t\treturn err\n\t}\n\n\treturn nil\n}\n\n// attachWorkerToTarget attaches a Worker target to a given session.\nfunc (fs *FrameSession) attachWorkerToTarget(ti *target.Info, session *Session) error {\n\tif fs.page.isClosing() {\n\t\tfs.logger.Debugf(\"FrameSession:attachWorkerToTarget\",\n\t\t\t\"rejected worker; page is closing: tid=%v\", ti.TargetID)\n\t\tdetachSession(fs.teardownCtx, session)\n\t\treturn nil\n\t}\n\n\tw, err := NewWorker(fs.ctx, session, ti.TargetID, ti.URL)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"attaching worker target ID %v to session ID %v: %w\",\n\t\t\tti.TargetID, session.ID(), err)\n\t}\n\tfs.page.addWorker(session.ID(), w)\n\n\treturn nil\n}\n\nfunc (fs *FrameSession) onDetachedFromTarget(event *target.EventDetachedFromTarget) {\n\tfs.logger.Debugf(\"FrameSession:onDetachedFromTarget\",\n\t\t\"sid:%v tid:%v esid:%v\",\n\t\tfs.session.ID(), fs.targetID, event.SessionID)\n\n\tfs.page.removeWorker(event.SessionID)\n}\n\nfunc (fs *FrameSession) onTargetCrashed() {\n\tfs.logger.Debugf(\"FrameSession:onTargetCrashed\", \"sid:%v tid:%v\", fs.session.ID(), fs.targetID)\n","sourceCodeStart":1064,"sourceCodeEnd":1100,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/frame_session.go#L1064-L1100","documentation":"k6 automatically attaches to every CDP target a page creates, including web and service workers. attachWorkerToTarget builds a Worker via NewWorker, which enables Log, Network and Runtime domains on the worker's session (worker.go initEvents). This error means that protocol initialization failed, and the failure is wrapped with the worker target ID and session ID. It usually indicates the worker target died between attach and init, or the page/context was closing (fs.ctx canceled).","triggerScenarios":"The page under test spawns a Web Worker (new Worker(...)), SharedWorker or service worker; the browser emits Target.attachedToTarget; NewWorker runs log.Enable/network.Enable/RunIfWaitingForDebugger on the worker session and one of those CDP commands fails because the worker terminated immediately, the session is invalid, or fs.ctx is canceled during page/context teardown.","commonSituations":"Testing sites that offload work to workers or register service workers; worker creation racing page.close() or browserContext.close(); short-lived workers (spawn, postMessage, terminate) that finish before k6 finishes attaching; browser crash or OOM mid-run.","solutions":["Keep the page and browser context open until workers have fully loaded; avoid closing while the application spawns workers","Re-run the iteration: this is frequently a race between worker creation and teardown and does not reproduce consistently","Check k6/browser logs for the underlying 'protocol error while initializing worker' cause and fix that first (crashed browser, out of memory, disconnected DevTools connection)","Upgrade k6 to a recent release: worker and session lifecycle handling has been hardened over time"],"exampleFix":"// before: context torn down while app spawns workers\nawait page.goto('https://app.example.com');\nawait context.close(); // workers still starting\n\n// after: let workers settle before teardown\nawait page.goto('https://app.example.com');\nawait page.waitForTimeout(2000); // or wait for an app-ready signal\nawait context.close();","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await page.goto(workerHeavyUrl);\n} catch (e) {\n  if (/attaching worker target ID/.test(String(e.message))) {\n    // worker raced teardown or worker target died; retry iteration or ignore\n  } else { throw e; }\n}","preventionTips":["Keep the page and context open until all workers have loaded","Avoid closing the browser context while the application spawns workers","Watch for the underlying 'protocol error while initializing worker' message to distinguish browser crashes from races"],"tags":["browser","cdp","web-worker","race-condition","lifecycle"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}