Skip to content

Commit ec937f3

Browse files
author
Divjot Arora
authored
GODRIVER-1556 Ensure underlying net.Conn is not set in error cases (#372)
1 parent e78313a commit ec937f3

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

x/mongo/driver/topology/connection.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,16 @@ func (c *connection) connect(ctx context.Context) {
9797
ctx, c.cancelConnectContext = context.WithCancel(ctx)
9898
close(c.connectContextMade)
9999

100+
// Assign the result of DialContext to a temporary net.Conn to ensure that c.nc is not set in an error case.
100101
var err error
101-
c.nc, err = c.config.dialer.DialContext(ctx, c.addr.Network(), c.addr.String())
102+
var tempNc net.Conn
103+
tempNc, err = c.config.dialer.DialContext(ctx, c.addr.Network(), c.addr.String())
102104
if err != nil {
103105
atomic.StoreInt32(&c.connected, disconnected)
104106
c.connectErr = ConnectionError{Wrapped: err, init: true}
105107
return
106108
}
109+
c.nc = tempNc
107110

108111
if c.config.tlsConfig != nil {
109112
tlsConfig := c.config.tlsConfig.Clone()

0 commit comments

Comments
 (0)