{"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":204,"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":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/grafana/k6/blob/94169daab29df52c1ed2ee0549657a577666b855/internal/js/modules/k6/browser/common/connection.go#L186-L222","documentation":"Connection.close uses this error as the context-cancellation reason after the websocket to the browser is closed with the given close code. It propagates through cancelCtx so pending CDP calls fail fast instead of hanging; it is the standard teardown signal for the browser websocket connection.","triggerScenarios":"Thrown at internal/js/modules/k6/browser/common/connection.go:201 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Treat as expected if it appears during browser shutdown; avoid using the browser/connection after close","Check the websocket close code for abnormal reasons (e.g. 1006) indicating a crash of the browser process","Ensure browser.close() is called and not raced with active operations"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"94169daab29df52c1ed2ee0549657a577666b855","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}