{"record":{"id":"ccaf2000e8d34345","repo":"t8y2/dbx","slug":"invalid-zookeeper-buffer-length-d","errorCode":null,"errorMessage":"invalid ZooKeeper buffer length %d","messagePattern":"invalid ZooKeeper buffer length (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"agents/drivers/argo-go/zookeeper_protocol.go","lineNumber":491,"sourceCode":"\nfunc (decoder *zooKeeperDecoder) int64() (int64, error) {\n\tvalue, err := decoder.take(8)\n\tif err != nil {\n\t\treturn 0, err\n\t}\n\treturn int64(binary.BigEndian.Uint64(value)), nil\n}\n\nfunc (decoder *zooKeeperDecoder) bytes() ([]byte, error) {\n\tlength, err := decoder.int32()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tif length == -1 {\n\t\treturn nil, nil\n\t}\n\tif length < -1 {\n\t\treturn nil, fmt.Errorf(\"invalid ZooKeeper buffer length %d\", length)\n\t}\n\tvalue, err := decoder.take(int(length))\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn append([]byte(nil), value...), nil\n}\n\nfunc (decoder *zooKeeperDecoder) string() (string, error) {\n\tvalue, err := decoder.bytes()\n\treturn string(value), err\n}\n\nfunc (decoder *zooKeeperDecoder) strings() ([]string, error) {\n\tlength, err := decoder.int32()\n\tif err != nil {\n\t\treturn nil, err\n\t}","sourceCodeStart":473,"sourceCodeEnd":509,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/argo-go/zookeeper_protocol.go#L473-L509","documentation":"When decoding a length-prefixed byte buffer from a ZooKeeper response, a negative length other than the legal -1 (NULL) sentinel means the stream is corrupt or misaligned. The decoder rejects the value in decoder.bytes rather than attempting a take() with a negative size, which would otherwise fail opaquely as io.ErrUnexpectedEOF.","triggerScenarios":"Parsing any response containing a buffer field (node data, SASL payload, stat-associated fields) where the length int32 is < -1: reading from a desynchronized stream, a response produced by a non-ZooKeeper peer, or frame bytes corrupted in transit.","commonSituations":"Reusing a connection after a previous read timed out mid-frame (offset shifted into the payload); a MITM proxy or port-forward that alters traffic; connecting to a service speaking a different protocol on the ZooKeeper port; driver/server protocol-version mismatch producing shifted fields.","solutions":["Discard the client connection and re-dial — a corrupt length means the byte stream is misaligned and cannot be recovered on the same socket.","Verify the endpoint is a real ZooKeeper server on port 2181 (or your configured port) and that TLS settings match the server.","Check for concurrent use of one client/connection from multiple goroutines — interleaved reads corrupt the framing; serialize access or use one client per goroutine.","Capture the raw bytes (or enable protocol-level logging) around the failure to identify where the stream diverged."],"exampleFix":"// before\nresp, err := client.request(payload) // client reused across goroutines\n\n// after\nmu.Lock()\ndefer mu.Unlock()\nresp, err := client.request(payload) // serialize access to the shared connection\nif err != nil {\n    client.Close() // framing is unrecoverable; force re-dial\n    return err\n}","handlingStrategy":"retry","validationCode":"// verify the endpoint speaks ZooKeeper before parsing responses\nconn, err := net.DialTimeout(\"tcp\", addr, 3*time.Second)\nif err != nil {\n    return err\n}\nconn.SetDeadline(time.Now().Add(3 * time.Second))\n// a four-byte probe read of a real ZooKeeper reply will never start with an\n// implausible length; garbage on connect means wrong service\nconn.Close()","typeGuard":"func isFrameCorruption(err error) bool {\n    if err == nil {\n        return false\n    }\n    return strings.Contains(err.Error(), \"invalid ZooKeeper buffer length\") ||\n        errors.Is(err, io.ErrUnexpectedEOF)\n}","tryCatchPattern":"value, err := client.Get(path)\nif isFrameCorruption(err) {\n    client.Close()\n    client = redial() // stream is misaligned; a fresh connection is required\n    value, err = client.Get(path)\n}\nreturn value, err","preventionTips":["Re-dial on any decoder error; framing cannot self-heal mid-stream.","Serialize all reads/writes on a shared client with a mutex, or use one client per goroutine.","Confirm TLS/plaintext parity with the server before connecting.","Watch for proxies or port-forwards altering traffic on the ZooKeeper port."],"tags":["zookeeper","decoder","protocol","stream-corruption"],"backgroundTag":"malformed-response-frame","analyzedSha":"c0390bff16418b651f4728520d99adf8ce48829a","analyzedAt":"2026-09-05T23:05:10.900Z","contentChangedAt":"2026-09-05T23:05:10.900Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}