{"record":{"id":"64afcd66f29d4b21","repo":"go-sql-driver/mysql","slug":"invalid-compressed-packet-uncompressed-length-in","errorCode":null,"errorMessage":"invalid compressed packet: uncompressed length in header is %d, actual %d","messagePattern":"invalid compressed packet: uncompressed length in header is (.+?), actual (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compress.go","lineNumber":141,"sourceCode":"\tif err != nil {\n\t\treturn err\n\t}\n\n\t// if payload is uncompressed, its length will be specified as zero, and its\n\t// true length is contained in comprLength\n\tif uncompressedLength == 0 {\n\t\tc.buff.Write(comprData)\n\t\treturn nil\n\t}\n\n\t// use existing capacity in bytesBuf if possible\n\tc.buff.Grow(uncompressedLength)\n\tnread, err := zDecompress(comprData, &c.buff)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif nread != uncompressedLength {\n\t\treturn fmt.Errorf(\"invalid compressed packet: uncompressed length in header is %d, actual %d\",\n\t\t\tuncompressedLength, nread)\n\t}\n\treturn nil\n}\n\nconst minCompressLength = 150\nconst maxPayloadLen = maxPacketSize - 4\n\n// writePackets sends one or some packets with compression.\n// Use this instead of mc.netConn.Write() when mc.compress is true.\nfunc (c *compIO) writePackets(packets []byte) (int, error) {\n\ttotalBytes := len(packets)\n\tblankHeader := make([]byte, 7)\n\tbuf := &c.buff\n\n\tfor len(packets) > 0 {\n\t\tpayloadLen := min(maxPayloadLen, len(packets))\n\t\tpayload := packets[:payloadLen]","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/go-sql-driver/mysql/blob/03d76c7e07908e255ce62d126d07ede3f2365d86/compress.go#L123-L159","documentation":"Returned by the compressed-protocol reader (compress.go:141) after inflating a packet whose decompressed byte count (nread) does not equal the uncompressedLength the server declared in the 7-byte compression header. It signals corruption or non-conformance in the compressed stream, which is only active when the DSN sets compress=true.","triggerScenarios":"Using compress=true and receiving a frame whose stated uncompressed size disagrees with the actual zlib output — truncated bytes, a buggy/intermediary re-compressor, network corruption, or a server/proxy that miscomputes the length field.","commonSituations":"Enabling wire compression over lossy/intercepted links; a proxy that recompresses MySQL traffic but writes wrong length headers; server versions with compression framing bugs.","solutions":["Disable compression (remove compress=true) to confirm the issue is compression-specific; if it clears, the fault is in compression framing.","Remove or fix any proxy that rewrites MySQL compressed frames.","Upgrade the server/proxy to a version with correct compressed-packet length handling.","Check for packet loss / MTU issues truncating the compressed stream."],"exampleFix":"// before\ndsn := \"user:pass@tcp(mysql:3306)/db?compress=true\"\n// -> \"invalid compressed packet: ...\"\n\n// after (diagnose): turn compression off\ndsn := \"user:pass@tcp(mysql:3306)/db\"\n// if the error disappears, the fault is in the compression path/proxy","handlingStrategy":"retry","validationCode":"// Make compression opt-in only where the link is clean and verified; default off.\nuseCompress := cleanInternalLink\ndsn := \"user:pass@tcp(host:3306)/db\"\nif useCompress { dsn += \"?compress=true\" }","typeGuard":"func isInvalidCompressedPacket(err error) bool {\n    return err != nil && strings.Contains(err.Error(), \"invalid compressed packet\")\n}","tryCatchPattern":"if isInvalidCompressedPacket(err) {\n    // retry without compression to isolate the fault; if it clears, the\n    // issue is in the compression framing/proxy.\n}","preventionTips":["Enable compress=true only on verified, low-corruption links.","Keep MySQL proxies fully transparent to compressed frames.","Reproduce connection drops first with compression off."],"tags":["compression","protocol","data-corruption","config"],"backgroundTag":null,"analyzedSha":"03d76c7e07908e255ce62d126d07ede3f2365d86","analyzedAt":"2026-08-07T10:39:17.340Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}