Skip to content

Commit 9ec3173

Browse files
author
Divjot Arora
committed
GODRIVER-1439 Only close connections for heartbeat errors (#261)
1 parent 86bd49a commit 9ec3173

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

x/mongo/driver/topology/connection.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,19 @@ func (c *connection) connect(ctx context.Context) {
101101

102102
if c.config.tlsConfig != nil {
103103
tlsConfig := c.config.tlsConfig.Clone()
104-
c.nc, err = configureTLS(ctx, c.nc, c.addr, tlsConfig)
104+
105+
// store the result of configureTLS in a separate variable than c.nc to avoid overwriting c.nc with nil in
106+
// error cases.
107+
tlsNc, err := configureTLS(ctx, c.nc, c.addr, tlsConfig)
105108
if err != nil {
109+
if c.nc != nil {
110+
_ = c.nc.Close()
111+
}
106112
atomic.StoreInt32(&c.connected, disconnected)
107113
c.connectErr = ConnectionError{Wrapped: err, init: true}
108114
return
109115
}
116+
c.nc = tlsNc
110117
}
111118

112119
c.bumpIdleDeadline()

x/mongo/driver/topology/server.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,15 +540,16 @@ func (s *Server) heartbeat(conn *connection) (description.Server, *connection) {
540540
if err == nil {
541541
tmpDesc := op.Result(s.address)
542542
descPtr = &tmpDesc
543+
} else {
544+
// close the connection here rather than in the error check below to avoid calling Close on a net.Conn
545+
// that wasn't successfully created
546+
_ = conn.close()
543547
}
544548
}
545549

546550
// we do a retry if the server is connected, if succeed return new server desc (see below)
547551
if err != nil {
548552
saved = err
549-
if conn != nil && conn.nc != nil {
550-
conn.nc.Close()
551-
}
552553
conn = nil
553554
if _, ok := err.(ConnectionError); ok {
554555
s.pool.drain()

0 commit comments

Comments
 (0)