{"record":{"id":"40ab09f7642f1fe8","repo":"t8y2/dbx","slug":"zookeeper-connection-is-nil-40ab09","errorCode":null,"errorMessage":"ZooKeeper connection is nil","messagePattern":"ZooKeeper connection is nil","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"agents/drivers/hive-go/zookeeper_protocol.go","lineNumber":143,"sourceCode":"\t\tevents := make(chan zk.Event, 1)\n\t\tevents <- zk.Event{State: zk.StateHasSession, Server: address}\n\t\tclose(events)\n\t\treturn client, events, nil\n\t}\n\treturn nil, nil, fmt.Errorf(\"connect and authenticate to ZooKeeper: %s\", strings.Join(failures, \"; \"))\n}\n\ntype protocolZooKeeperClient struct {\n\tconnection net.Conn\n\ttimeout    time.Duration\n\txid        int32\n\tmutex      sync.Mutex\n\tclosed     bool\n}\n\nfunc newProtocolZooKeeperClient(connection net.Conn, timeout time.Duration) (*protocolZooKeeperClient, error) {\n\tif connection == nil {\n\t\treturn nil, errors.New(\"ZooKeeper connection is nil\")\n\t}\n\tif timeout <= 0 {\n\t\ttimeout = defaultConnectTimeout\n\t}\n\tclient := &protocolZooKeeperClient{connection: connection, timeout: timeout}\n\trequest := &zooKeeperEncoder{}\n\trequest.int32(zooKeeperProtocolVersion)\n\trequest.int64(0)\n\trequest.int32(zooKeeperTimeoutMillis(timeout))\n\trequest.int64(0)\n\trequest.bytes(make([]byte, 16))\n\tif err := client.writeFrame(request.data()); err != nil {\n\t\treturn nil, fmt.Errorf(\"send ZooKeeper connect request: %w\", err)\n\t}\n\tresponse, err := client.readFrame()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"read ZooKeeper connect response: %w\", err)\n\t}","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/hive-go/zookeeper_protocol.go#L125-L161","documentation":"newProtocolZooKeeperClient wraps an established net.Conn into the protocol-level ZooKeeper client and immediately rejects a nil connection. This guards against constructing a client whose subsequent writes would panic on nil. Note the same error symbol exists in both hive-go and argo-go driver copies.","triggerScenarios":"Calling newProtocolZooKeeperClient with a nil net.Conn — usually because the preceding dial/ZooKeeper connect returned (nil, nil, err) and the error was ignored or the nil conn was passed through unconditionally.","commonSituations":"Caller does `conn, _, _ := connectKerberosZooKeeper(...)` discarding the error and then builds the client; a helper returns nil conn on a non-error path by mistake; test scaffolding passes a stub that is nil.","solutions":["Always check the error from the dial/connect step before calling newProtocolZooKeeperClient","Fix callers that ignore the (conn, err) pair from connectKerberosZooKeeper/connectZooKeeper","Ensure dial helpers never return nil conn with nil error","In tests, pass a real or stub net.Conn (e.g. net.Pipe) rather than nil"],"exampleFix":"// before\nconn, _, _ := connectKerberosZooKeeper(ctx, servers, timeout, tls, cfg)\nclient, err := newProtocolZooKeeperClient(conn, timeout)\n// after\nconn, _, err := connectKerberosZooKeeper(ctx, servers, timeout, tls, cfg)\nif err != nil {\n    return err\n}\nclient, err := newProtocolZooKeeperClient(conn, timeout)","handlingStrategy":"type-guard","validationCode":"// Go\nif conn == nil {\n    return errors.New(\"cannot create ZooKeeper client: connection is nil\")\n}","typeGuard":"// Go\nfunc isLiveConn(c net.Conn) bool { return c != nil }\n\nif isLiveConn(conn) {\n    client, err := newProtocolZooKeeperClient(conn, timeout)\n    ...\n}","tryCatchPattern":null,"preventionTips":["Never discard errors from dial/connect functions with _","Return early on connect errors instead of passing conn through","Keep dial helpers contract: non-nil error implies nil conn"],"tags":["zookeeper","nil-pointer","go","protocol"],"backgroundTag":"nil-connection","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"}