{"record":{"id":"230742a4fe46b391","repo":"gorilla/websocket","slug":"websocket-internal-error-unexpected-text-or-bina","errorCode":null,"errorMessage":"websocket: internal error, unexpected text or binary in Reader","messagePattern":"websocket: internal error, unexpected text or binary in Reader","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"conn.go","lineNumber":1085,"sourceCode":"\t\t\trem -= int64(n)\n\t\t\t_ = c.setReadRemaining(rem) // rem is guaranteed to be >= 0\n\t\t\tif c.readRemaining > 0 && c.readErr == io.EOF {\n\t\t\t\tc.readErr = errUnexpectedEOF\n\t\t\t}\n\t\t\treturn n, c.readErr\n\t\t}\n\n\t\tif c.readFinal {\n\t\t\tc.messageReader = nil\n\t\t\treturn 0, io.EOF\n\t\t}\n\n\t\tframeType, err := c.advanceFrame()\n\t\tswitch {\n\t\tcase err != nil:\n\t\t\tc.readErr = err\n\t\tcase frameType == TextMessage || frameType == BinaryMessage:\n\t\t\tc.readErr = errors.New(\"websocket: internal error, unexpected text or binary in Reader\")\n\t\t}\n\t}\n\n\terr := c.readErr\n\tif err == io.EOF && c.messageReader == r {\n\t\terr = errUnexpectedEOF\n\t}\n\treturn 0, err\n}\n\nfunc (r *messageReader) Close() error {\n\treturn nil\n}\n\n// ReadMessage is a helper method for getting a reader using NextReader and\n// reading from that reader to a buffer.\nfunc (c *Conn) ReadMessage() (messageType int, p []byte, err error) {\n\tvar r io.Reader","sourceCodeStart":1067,"sourceCodeEnd":1103,"githubUrl":"https://github.com/gorilla/websocket/blob/e064f32e3674d9d79a8fd417b5bc06fa5c6cad8f/conn.go#L1067-L1103","documentation":"This internal consistency-check error occurs inside the message Reader loop: advanceFrame returned a TextMessage or BinaryMessage frame type in a state where only control frames or continuation frames were expected. It indicates an internal state-machine bug, since ReadMessage/NextReader should never surface data frames through this path.","triggerScenarios":"Calling Read()/NextReader and having the internal readErr state machine receive a text/binary frame type it believed it already consumed; not reachable through correct library internals.","commonSituations":"Custom forks of gorilla/websocket with modified frame-advance logic; virtually never seen with the stock library.","solutions":["Use the official gorilla/websocket release instead of a modified fork","If you fork the library, review the advanceFrame/Reader state machine so data frame types are consumed in the correct state","Check whether the connection is being shared concurrently between goroutines, which can corrupt the read state; use one reader goroutine per connection"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use official library releases","Never share a Conn's read side between goroutines","Report internal errors upstream with a reproduction"],"tags":["websocket","internal-error","reader"],"backgroundTag":"websocket-internal-error","analyzedSha":"e064f32e3674d9d79a8fd417b5bc06fa5c6cad8f","analyzedAt":"2026-08-31T12:40:58.222Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}