{"record":{"id":"bb1e5606307f1fea","repo":"t8y2/dbx","slug":"zookeeper-sasl-response-xid-d-does-not-match-requ","errorCode":null,"errorMessage":"ZooKeeper SASL response xid %d does not match request xid %d","messagePattern":"ZooKeeper SASL response xid (.+?) does not match request xid (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"agents/drivers/zookeeper/sasl.go","lineNumber":137,"sourceCode":"func zooKeeperSASLRound(connection net.Conn, xid int32, token []byte) ([]byte, error) {\n\tpayload := make([]byte, 12+len(token))\n\tbinary.BigEndian.PutUint32(payload[0:4], uint32(xid))\n\tbinary.BigEndian.PutUint32(payload[4:8], uint32(zooKeeperSASLOpcode))\n\tbinary.BigEndian.PutUint32(payload[8:12], uint32(len(token)))\n\tcopy(payload[12:], token)\n\tif err := writeZooKeeperFrame(connection, payload); err != nil {\n\t\treturn nil, err\n\t}\n\tresponse, err := readZooKeeperFrame(connection)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tif len(response) < 20 {\n\t\treturn nil, errors.New(\"ZooKeeper SASL response is truncated\")\n\t}\n\tresponseXID := int32(binary.BigEndian.Uint32(response[4:8]))\n\tif responseXID != xid {\n\t\treturn nil, fmt.Errorf(\"ZooKeeper SASL response xid %d does not match request xid %d\", responseXID, xid)\n\t}\n\terrorCode := int32(binary.BigEndian.Uint32(response[16:20]))\n\tif errorCode != 0 {\n\t\treturn nil, fmt.Errorf(\"ZooKeeper SASL server returned error %d\", errorCode)\n\t}\n\tif len(response) < 24 {\n\t\treturn nil, errors.New(\"ZooKeeper SASL token is truncated\")\n\t}\n\ttokenLength := int(int32(binary.BigEndian.Uint32(response[20:24])))\n\tif tokenLength < 0 || tokenLength > zooKeeperMaximumFrameLen || 24+tokenLength > len(response) {\n\t\treturn nil, fmt.Errorf(\"ZooKeeper SASL token length %d is invalid\", tokenLength)\n\t}\n\treturn append([]byte(nil), response[24:24+tokenLength]...), nil\n}\n\nfunc readZooKeeperFrame(reader io.Reader) ([]byte, error) {\n\theader := make([]byte, 4)\n\tif _, err := io.ReadFull(reader, header); err != nil {","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/zookeeper/sasl.go#L119-L155","documentation":"Every SASL request carries an XID echoed by the server in its response. zooKeeperSASLRound compares responseXID (bytes 4:8) with the request XID; a mismatch means the reply does not correspond to this round's request — a desynchronized stream. The error includes both XIDs for diagnosis.","triggerScenarios":"negotiateSASLDigest or TestZooKeeperSASLRound calls zooKeeperSASLRound; the server (or test fake) returns a response whose big-endian uint32 at offset 4 differs from the request's xid (zooKeeperSASLXIDBase + round).","commonSituations":"A proxy multiplexing/pipelining frames incorrectly, a server bug emitting responses out of order, or a test mock replaying a stale response — also triggered deliberately by TestZooKeeperSASLRoundRejectsWrongXID.","solutions":["Check for proxies or connection sharing that could interleave responses on the same socket.","Reconnect and retry the SASL handshake to re-synchronize XIDs.","If using a mock server, make it echo the request XID exactly (zooKeeperSASLXIDBase + round).","Verify no concurrent goroutines are reading from the same connection, shifting frame boundaries."],"exampleFix":"// before (fake server)\nbinary.BigEndian.PutUint32(resp[4:8], uint32(99))\n// after\nbinary.BigEndian.PutUint32(resp[4:8], uint32(requestXID))","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"_, err := zooKeeperSASLRound(conn, xid, token)\nif err != nil && strings.Contains(err.Error(), \"does not match request xid\") {\n\tconn.Close()\n\treturn retryWithFreshConnection()\n}","preventionTips":["Never share one ZooKeeper connection across concurrent goroutines during handshake.","Make test fakes echo the exact request XID.","Re-dial on any XID mismatch; the stream cannot be resynchronized."],"tags":["sasl","protocol","zookeeper","xid-mismatch"],"backgroundTag":"protocol-xid-mismatch","analyzedSha":"c0390bff16418b651f4728520d99adf8ce48829a","analyzedAt":"2026-09-05T23:05:10.900Z","contentChangedAt":"2026-09-05T23:05:10.900Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}