{"record":{"id":"cf6c1f790310d7ee","repo":"go-sql-driver/mysql","slug":"unexpected-read-from-socket","errorCode":null,"errorMessage":"unexpected read from socket","messagePattern":"unexpected read from socket","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"conncheck.go","lineNumber":20,"sourceCode":"//\n// Copyright 2019 The Go-MySQL-Driver Authors. All rights reserved.\n//\n// This Source Code Form is subject to the terms of the Mozilla Public\n// License, v. 2.0. If a copy of the MPL was not distributed with this file,\n// You can obtain one at http://mozilla.org/MPL/2.0/.\n\n//go:build linux || darwin || dragonfly || freebsd || netbsd || openbsd || solaris || illumos\n\npackage mysql\n\nimport (\n\t\"errors\"\n\t\"io\"\n\t\"net\"\n\t\"syscall\"\n)\n\nvar errUnexpectedRead = errors.New(\"unexpected read from socket\")\n\nfunc connCheck(conn net.Conn) error {\n\tvar sysErr error\n\n\tsysConn, ok := conn.(syscall.Conn)\n\tif !ok {\n\t\treturn nil\n\t}\n\trawConn, err := sysConn.SyscallConn()\n\tif err != nil {\n\t\treturn err\n\t}\n\n\terr = rawConn.Read(func(fd uintptr) bool {\n\t\tvar buf [1]byte\n\t\tn, err := syscall.Read(int(fd), buf[:])\n\t\tswitch {\n\t\tcase n == 0 && err == nil:","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/go-sql-driver/mysql/blob/03d76c7e07908e255ce62d126d07ede3f2365d86/conncheck.go#L2-L38","documentation":"Error \"unexpected read from socket\" thrown in go-sql-driver/mysql.","triggerScenarios":"The driver performs a non-destructive readability check on the raw socket (via syscall.RawConn and a peek read) before handing the connection to the pool, and the kernel reports pending data or an error on a connection that should be idle.","commonSituations":"Typically happens when the server (or a proxy/middlebox) sent unexpected bytes on an idle pooled connection, or the connection was half-closed. The driver discards the connection and retries on a fresh one, so it is usually transient; frequent occurrences point to network middleboxes killing idle connections or a mismatched wait_timeout.","solutions":["Check that the remote MySQL server or proxy is not writing unexpected data to the connection; this usually indicates a broken or hijacked TCP connection, so reconnect and retry the operation.","Verify that no intermediary (proxy, load balancer, or middleware) is injecting bytes into the socket, and that the connection is a genuine MySQL protocol connection.","Avoid sharing a single connection across goroutines outside database/sql semantics; let database/sql manage connection pooling and recreate the connection if it becomes invalid."],"exampleFix":"// Recreate the connection instead of reusing a corrupted one\ndb, err := sql.Open(\"mysql\", dsn)\nif err != nil {\n    return err\n}\nif err := db.Ping(); err != nil {\n    db.Close()\n    db, err = sql.Open(\"mysql\", dsn) // retry with a fresh connection\n}","handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"03d76c7e07908e255ce62d126d07ede3f2365d86","analyzedAt":"2026-08-07T10:39:17.340Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}