Skip to content

Commit a426bf4

Browse files
author
Divjot Arora
committed
GODRIVER-1556 Ensure underlying net.Conn is not set in error cases (#372)
1 parent 76bc82c commit a426bf4

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
@@ -96,13 +96,16 @@ func (c *connection) connect(ctx context.Context) {
9696
ctx, c.cancelConnectContext = context.WithCancel(ctx)
9797
close(c.connectContextMade)
9898

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

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

0 commit comments

Comments
 (0)