-
Type:
Bug
-
Resolution: Duplicate
-
Priority:
Major - P3
-
Affects Version/s: None
-
Component/s: None
-
None
-
Environment:OS:
node.js / npm versions:
Additional info:
-
Not Needed
-
-
Developer Tools
Summary
NodeDriverServiceProvider.resetConnectionOptions() connects a replacement MongoClient, then swallows any error from close() on the previous client and drops the only reference to it. If close() threw before the driver shut the topology down, that client's server monitors and connection pools keep running for the rest of the process. The shell reports that the option change succeeded.
This is on main at mongosh 2.14.1 (a49b3fc93), driver mongodb 7.x as depended on by that tree. It is not MONGOSH-2216 (investigate NODE-6882, closing in-use connections inside the driver) and not MONGOSH-3816 (a second Ctrl+C exits the process during an interrupt). Those can also abandon a client. This bug abandons one while the shell keeps running.
Mechanism
packages/service-provider-node-driver/src/node-driver-service-provider.ts resetConnectionOptions:
try { await this.mongoClient.close(); } catch {} this.mongoClient = client;
The new client is already connected before this try. currentClientOptions has already been replaced with the merged options. The catch has an empty body. The assignment on the next line is the last provider field that pointed at the old client.
Callers on the shell path:
- Mongo.setReadPref (packages/shell-api/src/mongo.ts)
- Mongo.setReadConcern
- Mongo.setWriteConcern
- NodeDriverServiceProvider.authenticate (db.auth())
- the resume function returned by suspend(), which Ctrl+C uses after a successful close()
Driver MongoClient.close() (mongodb/src/mongo_client.ts _close) does not treat force as meaningful. The parameter is unused. The body:
- defines hasBeenClosed as true (non-configurable) before any await
- closes checked-out connections
- copies activeCursors, clears the set, then awaits cursor.close()
- copies activeSessions, clears the set, then awaits session.endSession()
- returns early if topology is already null
- sends endSessions and swallows that command's own errors
- calls topology.close(), nulls topology, then awaits encrypter.close()
A throw from cursor.close() or session.endSession() therefore happens after the client is marked closed and after those sets have been cleared, but before topology.close(). SDAM monitors and pools are still running. A later close() on the same client does get past the hasBeenClosed redefinition (the same property descriptor can be applied again) and can still reach topology.close() when topology is set. Nothing in mongosh makes that second call. The empty catch throws the client away instead.
A throw from encrypter.close() happens after topology.close(). The pool is down, but the CSFLE client's internal MongoClient may still be open, and the reference is dropped the same way. A second close() returns at topology == null and does not retry the encrypter. Holding the reference is what still allows a later provider.close() to try again only if we have not already overwritten this.mongoClient.
suspend() does not swallow. It awaits this.close() and, on failure, Mongo._suspend returns no resume function. The half-closed client stays referenced. The swallow is specifically the replacement path.
Failure modes
1. setReadPref succeeds and leaks the previous pool
db.coll.find({}).forEach(function () {}) // or any cursor still open on the client
db.getMongo().setReadPref('secondaryPreferred')
killCursors or another cursor.close() rejects because the primary stepped down or the network blipped. resetConnectionOptions has already connected a new client. It ignores the error and publishes that client. Later commands use the new pool.
The old topology is still monitoring. Its sockets and heartbeat timers stay referenced by the client object, so the client is not collected. Effects:
- the process keeps the old server-monitoring connections open until it actually exits
- heartbeats continue against every member the old client knew about, in addition to the new client's heartbeats
- pooled sessions that had not reached endSessions stay on the server until logicalSessionTimeoutMinutes (often 30 minutes)
- cursors whose close() threw stay until cursorTimeoutMillis
- quit() / provider.close() only closes this.mongoClient, which is the new client. The old topology is not in that call
If those monitoring handles are still ref'd in the event loop, Node can also fail to exit after the shell has closed, because nothing ever called topology.close() on the client that was dropped.
2. db.auth() reports success
db.auth('appUser', 'secret')
authenticate() awaits resetConnectionOptions and then returns ok: 1. There is no check of the swallowed close() error. The same cursor or endSession failure as above still returns success. The shell is on the new client, authenticated, and the previous client's pools and monitors are still up. A user who auths during a failover can end up with two live clients and no error.
3. setWriteConcern / setReadConcern during a blip
db.getMongo().setWriteConcern({ w: 'majority', wtimeoutMS: 5000 })
db.getMongo().setReadConcern('majority')
Both go through resetConnectionOptions. A single rejected session.endSession() (for example an explicit session whose abort throws because the connection dropped) takes the empty catch. The concern appears to have changed, getWriteConcern() reads it from the new client, and the old client is leaked as in (1). activeSessions was already cleared by the driver, so the session that threw is not ended on the server by a later close of that same client either.
4. What the Ctrl+C resume path does and does not do
suspend() closes the current client first. On success, the resume callback calls resetConnectionOptions({}). That second close() sees a null topology and returns. The empty catch is not what leaks in that success path.
If suspend()'s own close() throws halfway, resume is null, onResumeExecution reports that not every connection could be re-established, and the provider still holds the half-closed client. Monitors can still be running, but the reference was not dropped. MONGOSH-3816 is the separate case where a second Ctrl+C kills the process before close finishes at all.
5. Options and client diverge if we only throw
currentClientOptions is replaced before connectMongoClient. Any fix that refuses to publish the new client has to put those options back. Otherwise a failed setReadPref('secondary') would leave the provider describing secondary while this.mongoClient is still the old primary client, or the reverse if the assignment happened and the options were rolled back.
Expected
- Try close() on the previous client. If it throws, call close() once more so a failure before topology.close() still shuts the monitors and pools down.
- Publish the replacement client only after that close has succeeded.
- If the close still fails, restore currentClientOptions, close the replacement client, and throw the original error. db.auth / setReadPref / setWriteConcern / interrupt resume then fail instead of reporting success with a leaked pool.
- If the replacement client cannot be closed either, keep it referenced and close it from provider.close(), which should use the same retry. Do not drop the last reference.
Suggested change
In resetConnectionOptions, replace the empty catch with a helper that retries close() once and rethrows the first error if the retry fails. On that failure, do not assign this.mongoClient. Restore the previous currentClientOptions. Close the client just connected, and if that close fails, retain it for provider.close().
A unit test can stub connectMongoClient and the previous client's close(): one rejection then success swaps to the new client; two rejections leave the old client in place, do not keep the new read preference, and reject resetConnectionOptions.