{"record":{"id":"00c7d1bf10e33547","repo":"grafana/k6","slug":"connection-closed-with-websocket-code-d","errorCode":null,"errorMessage":"connection closed with websocket code: %d","messagePattern":"connection closed with websocket code: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/connection.go","lineNumber":201,"sourceCode":"\t\terrorCh:                  make(chan error),\n\t\tdone:                     make(chan struct{}),\n\t\tclosing:                  make(chan struct{}),\n\t\tmsgIDGen:                 &msgID{},\n\t\tsessions:                 make(map[target.SessionID]*Session),\n\t\tonTargetAttachedToTarget: onTargetAttachedToTarget,\n\t}\n\n\tgo c.recvLoop()\n\tgo c.sendLoop()\n\n\treturn &c, nil\n}\n\nfunc (c *Connection) close(code int) error {\n\tc.logger.Debugf(\"Connection:close\", \"code:%d\", code)\n\n\tdefer func() {\n\t\tc.cancelCtx(fmt.Errorf(\"connection closed with websocket code: %d\", code))\n\t}()\n\n\tvar err error\n\tc.shutdownOnce.Do(func() {\n\t\tdefer func() {\n\t\t\t// Stop the main control loop\n\t\t\tclose(c.done)\n\t\t\t_ = c.conn.Close()\n\t\t}()\n\n\t\tc.closeAllSessions()\n\n\t\terr = c.conn.WriteControl(websocket.CloseMessage,\n\t\t\twebsocket.FormatCloseMessage(code, \"\"),\n\t\t\ttime.Now().Add(time.Second),\n\t\t)\n\n\t\t// According to the WS RFC[1], we might want to wait for a response","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/connection.go#L183-L219","documentation":"Connection.close records the websocket close code as the cancel cause for the connection context, so every pending or future operation on that connection fails with this message via ContextErr. The %d is the RFC 6455 close code: 1000/1001 are orderly shutdowns, 1006 means an abnormal drop (no close frame), others indicate protocol-level failures.","triggerScenarios":"Browser (or target) closes the websocket during a CDP call: browser.Close(), page/target destroyed, browser crash (1006), or k6 itself closing the connection after receiving a close frame — the code then propagates to all in-flight sends/waits.","commonSituations":"Test tears down while a navigation or evaluation is still pending; chromium crashes mid-run; remote browser restarted under k6; the code appearing repeatedly in logs usually means an operation raced the close instead of being awaited first.","solutions":["Map the code: 1000/1001 = expected shutdown (usually harmless if during teardown); 1006 = crash/network drop worth investigating.","Await all pending operations (await page.goto(...)) before closing pages/browser so nothing races the close.","For 1006, check the browser process lifetime and memory limits — it was killed or dropped.","Move cleanup into the iteration teardown and guard it so a normal close cannot interrupt in-flight work."],"exampleFix":"// before\npage.goto(url);          // not awaited\nawait browser.close();   // races the navigation\n\n// after\nawait page.goto(url);    // fully awaited\nawait browser.close();","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await page.evaluate('1+1');\n} catch (e) {\n  const m = String(e).match(/connection closed with websocket code: (\\d+)/);\n  if (m) {\n    const code = Number(m[1]);\n    if (code === 1000 || code === 1001) {\n      // orderly shutdown (test teardown) — ignore\n    } else {\n      // abnormal close (e.g. 1006): browser crashed or connection dropped — investigate\n      console.error('abnormal websocket close', code);\n    }\n  } else {\n    throw e;\n  }\n}","preventionTips":["Await every browser operation before closing pages/browser to avoid racing the close.","Treat 1006 as a browser-crash signal: check process logs and memory, not the k6 script.","Keep teardown idempotent so double-close paths cannot surprise in-flight calls."],"tags":["websocket","connection","close","shutdown","browser"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}