{"record":{"id":"ba2f2ce0c60bf19c","repo":"t8y2/dbx","slug":"zookeeper-connection-timed-out-before-a-session-wa-ba2f2c","errorCode":null,"errorMessage":"ZooKeeper connection timed out before a session was established","messagePattern":"ZooKeeper connection timed out before a session was established","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"agents/drivers/hive-go/discovery.go","lineNumber":198,"sourceCode":"\t\tif value := strings.Trim(part, \"/\"); value != \"\" {\n\t\t\tcleaned = append(cleaned, value)\n\t\t}\n\t}\n\tif len(cleaned) == 0 {\n\t\treturn \"/\"\n\t}\n\treturn \"/\" + strings.Join(cleaned, \"/\")\n}\n\nfunc waitForZooKeeperSession(ctx context.Context, events <-chan zk.Event, timeout time.Duration) error {\n\ttimer := time.NewTimer(timeout)\n\tdefer timer.Stop()\n\tfor {\n\t\tselect {\n\t\tcase <-ctx.Done():\n\t\t\treturn ctx.Err()\n\t\tcase <-timer.C:\n\t\t\treturn errors.New(\"ZooKeeper connection timed out before a session was established\")\n\t\tcase event, ok := <-events:\n\t\t\tif !ok {\n\t\t\t\treturn errors.New(\"ZooKeeper event stream closed before a session was established\")\n\t\t\t}\n\t\t\tif event.Err != nil {\n\t\t\t\treturn fmt.Errorf(\"ZooKeeper connection event: %w\", event.Err)\n\t\t\t}\n\t\t\tswitch event.State {\n\t\t\tcase zk.StateHasSession:\n\t\t\t\treturn nil\n\t\t\tcase zk.StateAuthFailed:\n\t\t\t\treturn errors.New(\"ZooKeeper authentication failed\")\n\t\t\tcase zk.StateExpired:\n\t\t\t\treturn errors.New(\"ZooKeeper session expired during connection\")\n\t\t\t}\n\t\t}\n\t}\n}","sourceCodeStart":180,"sourceCodeEnd":216,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/hive-go/discovery.go#L180-L216","documentation":"waitForZooKeeperSession waits for a connected/synced session event within a timeout; if the timer fires before a session is established, it aborts discovery with this error. The ZooKeeper ensemble was never reachable or never confirmed the session in time.","triggerScenarios":"Calling Endpoints against an unreachable or overloaded ZooKeeper quorum where no StateHasSession event arrives before the configured timeout.","commonSituations":"Wrong zkQuorum host/port, network partition or firewall blocking port 2181, ZooKeeper ensemble down or in the middle of a leader election, timeout set too low for a large ensemble.","solutions":["Verify the ZooKeeper hosts/ports are reachable (nc/zkCli smoke test)","Increase the discovery timeout in the connector configuration","Check ZooKeeper ensemble health and re-run if a leader election was in progress","Confirm network/firewall rules allow egress to port 2181"],"exampleFix":"// before\ndiscovery, _ := NewZooKeeperDiscovery(\"zk:2181\", path, time.Second) // too short\n// after\ndiscovery, _ := NewZooKeeperDiscovery(\"zk:2181\", path, 30*time.Second)","handlingStrategy":"retry","validationCode":"// preflight: ensure the quorum accepts TCP connections before Endpoints\nfor _, host := range strings.Split(quorum, \",\") {\n    conn, err := net.DialTimeout(\"tcp\", host, 3*time.Second)\n    if err != nil { return fmt.Errorf(\"zookeeper unreachable: %s\", host) }\n    conn.Close()\n}","typeGuard":null,"tryCatchPattern":"endpoints, err := discovery.Endpoints(ctx)\nif err != nil && strings.Contains(err.Error(), \"timed out before a session\") {\n    // increase timeout and retry with backoff\n    return retryWithBackoff(ctx, func() error { return connectWithLongerTimeout() })\n}\nreturn err","preventionTips":["Size the session/discovery timeout above worst-case network RTT","Monitor ZooKeeper ensemble health and leader elections","Preflight host:port reachability before starting discovery"],"tags":["zookeeper","timeout","network","connection"],"backgroundTag":"connection-timeout","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"}