t8y2/dbx · error

ZooKeeper connection is nil

Error message

ZooKeeper connection is nil

What it means

newProtocolZooKeeperClient guards against constructing a protocol client around a nil net.Conn and returns this error immediately. A nil connection would panic later on first read/write, so the constructor converts it into a clear error. It can also be used defensively in tests/fakes that pass nil connections.

Source

Thrown at agents/drivers/argo-go/zookeeper_protocol.go:143

		events := make(chan zk.Event, 1)
		events <- zk.Event{State: zk.StateHasSession, Server: address}
		close(events)
		return client, events, nil
	}
	return nil, nil, fmt.Errorf("connect and authenticate to ZooKeeper: %s", strings.Join(failures, "; "))
}

type protocolZooKeeperClient struct {
	connection net.Conn
	timeout    time.Duration
	xid        int32
	mutex      sync.Mutex
	closed     bool
}

func newProtocolZooKeeperClient(connection net.Conn, timeout time.Duration) (*protocolZooKeeperClient, error) {
	if connection == nil {
		return nil, errors.New("ZooKeeper connection is nil")
	}
	if timeout <= 0 {
		timeout = defaultConnectTimeout
	}
	client := &protocolZooKeeperClient{connection: connection, timeout: timeout}
	request := &zooKeeperEncoder{}
	request.int32(zooKeeperProtocolVersion)
	request.int64(0)
	request.int32(zooKeeperTimeoutMillis(timeout))
	request.int64(0)
	request.bytes(make([]byte, 16))
	if err := client.writeFrame(request.data()); err != nil {
		return nil, fmt.Errorf("send ZooKeeper connect request: %w", err)
	}
	response, err := client.readFrame()
	if err != nil {
		return nil, fmt.Errorf("read ZooKeeper connect response: %w", err)
	}

View on GitHub (pinned to c0390bff16)

Solutions

  1. Fix the dialer/connection provider so it never returns a nil conn together with a nil error
  2. Check the error from the dial step before constructing the protocol client
  3. In tests, supply a real net.Pipe() or stub conn instead of nil
  4. If you construct the client manually, validate the conn before calling

Example fix

// before
conn, err := dialer.Dial("tcp", addr) // may return nil, nil
client, err := newProtocolZooKeeperClient(conn, timeout)
// after
conn, err := dialer.Dial("tcp", addr)
if err != nil {
    return fmt.Errorf("dial zookeeper: %w", err)
}
if conn == nil {
    return errors.New("dialer returned nil connection")
}
client, err := newProtocolZooKeeperClient(conn, timeout)
Defensive patterns

Strategy: type-guard

Validate before calling

if conn == nil {
    return errors.New("dialer returned nil connection")
}

Type guard

func validZooKeeperConn(c net.Conn) bool { return c != nil }

Try / catch

client, err := newProtocolZooKeeperClient(conn, timeout)
if err != nil {
    return fmt.Errorf("zookeeper client init: %w", err)
}

Prevention

When it happens

Trigger: Calling newProtocolZooKeeperClient(nil, timeout) directly or via connectKerberosZooKeeper when the underlying dialer returned (nil, nil) — e.g. a custom dial function or test fake that failed without reporting an error.

Common situations: Custom net.Dial wrappers that swallow dial errors and return a nil conn; mock/fake ZooKeeper connections in tests passing nil; refactored connection pools returning nil on exhausted connections.

Related errors


AI-assisted analysis of t8y2/dbx@c0390bff16 (2026-09-05). Data as JSON: /api/errors/b959dc6bc0f3d379. Report an issue: GitHub.