diff --git a/mongo/client.go b/mongo/client.go index b1ce77129a..d80fd1d55e 100644 --- a/mongo/client.go +++ b/mongo/client.go @@ -281,6 +281,10 @@ func (c *Client) Disconnect(ctx context.Context) error { ctx = context.Background() } + if c.httpClient == internal.DefaultHTTPClient { + defer internal.CloseIdleHTTPConnections(c.httpClient) + } + c.endSessions(ctx) if c.mongocryptdFLE != nil { if err := c.mongocryptdFLE.disconnect(ctx); err != nil { @@ -312,10 +316,6 @@ func (c *Client) Disconnect(ctx context.Context) error { return replaceErrors(disconnector.Disconnect(ctx)) } - if c.httpClient == internal.DefaultHTTPClient { - internal.CloseIdleHTTPConnections(c.httpClient) - } - return nil }