Skip to content

Commit 5a263a7

Browse files
author
Divjot Arora
committed
GODRIVER-1439 Only close connections for heartbeat errors (#261)
1 parent 9809a2d commit 5a263a7

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
@@ -103,12 +103,19 @@ func (c *connection) connect(ctx context.Context) {
103103

104104
if c.config.tlsConfig != nil {
105105
tlsConfig := c.config.tlsConfig.Clone()
106-
c.nc, err = configureTLS(ctx, c.nc, c.addr, tlsConfig)
106+
107+
// store the result of configureTLS in a separate variable than c.nc to avoid overwriting c.nc with nil in
108+
// error cases.
109+
tlsNc, err := configureTLS(ctx, c.nc, c.addr, tlsConfig)
107110
if err != nil {
111+
if c.nc != nil {
112+
_ = c.nc.Close()
113+
}
108114
atomic.StoreInt32(&c.connected, disconnected)
109115
c.connectErr = ConnectionError{Wrapped: err, init: true}
110116
return
111117
}
118+
c.nc = tlsNc
112119
}
113120

114121
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)