Skip to content

Commit

Permalink
Properly close zk.Conn while dereferencing it
Browse files Browse the repository at this point in the history
  • Loading branch information
wayt committed Oct 25, 2024
1 parent f10e224 commit b02cef8
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions go/vt/topo/zk2topo/zk_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ func (c *ZkConn) withRetry(ctx context.Context, action func(conn *zk.Conn) error
c.conn = nil
}
c.mu.Unlock()
conn.Close()
}
return
}
Expand Down Expand Up @@ -327,23 +328,17 @@ func (c *ZkConn) maybeAddAuth(ctx context.Context) {
// clears out the connection record.
func (c *ZkConn) handleSessionEvents(conn *zk.Conn, session <-chan zk.Event) {
for event := range session {
closeRequired := false

switch event.State {
case zk.StateExpired, zk.StateConnecting:
closeRequired = true
fallthrough
case zk.StateDisconnected:
case zk.StateDisconnected, zk.StateExpired, zk.StateConnecting:
c.mu.Lock()
if c.conn == conn {
// The ZkConn still references this
// connection, let's nil it.
c.conn = nil
}
c.mu.Unlock()
if closeRequired {
conn.Close()
}
conn.Close()
log.Infof("zk conn: session for addr %v ended: %v", c.addr, event)
return
}
Expand Down

0 comments on commit b02cef8

Please sign in to comment.