{"record":{"id":"b54261cc35e2a01a","repo":"t8y2/dbx","slug":"zookeeper-connection-timed-out-before-a-session-wa","errorCode":null,"errorMessage":"ZooKeeper connection timed out before a session was established","messagePattern":"ZooKeeper connection timed out before a session was established","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"agents/drivers/argo-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/argo-go/discovery.go#L180-L216","documentation":"waitForZooKeeperSession waits for a connection event indicating a session was established; if the timeout timer fires first, it returns this error. The ZooKeeper client connected (or attempted to) but never reached StateHasSession within the allotted time, so discovery cannot proceed.","triggerScenarios":"Endpoints() calls waitForZooKeeperSession with a context/timeout, and the events channel delivers no session event before the deadline; also hit directly in TestWaitForZooKeeperSession-style timeout tests.","commonSituations":"ZooKeeper quorum unreachable due to firewall/network issues; wrong host/port in the quorum string; heavily loaded ZooKeeper or DNS slowness exceeding the configured timeout.","solutions":["Increase the ZooKeeper connection timeout in the discovery config","Verify the ZooKeeper quorum hosts/ports are reachable (telnet/nc to 2181) and DNS resolves","Retry the discovery; if the quorum is down, restore it before reconnecting"],"exampleFix":"// before\nctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)\n// after\nctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)","handlingStrategy":"retry","validationCode":"timeout, err := net.DialTimeout(\"tcp\", zkHost+\":2181\", 3*time.Second)\nif err != nil { return fmt.Errorf(\"zookeeper unreachable: %w\", err) }\n_ = timeout.Close()","typeGuard":null,"tryCatchPattern":"endpoints, err := discovery.Endpoints(ctx)\nif err != nil {\n\tif errors.Is(err, context.DeadlineExceeded) || strings.Contains(err.Error(), \"timed out\") {\n\t\t// retry with longer timeout / backoff\n\t}\n}","preventionTips":["Pre-check ZooKeeper reachability (TCP to 2181) before discovery","Size the connection timeout above worst-case network/DNS latency","Monitor quorum health so outages are detected before clients connect"],"tags":["zookeeper","timeout","network"],"backgroundTag":"connection-timeout","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"}