fix(NODE-5225): eagerly clear MongoClient.topology in MongoClient.close()#3652
Open
alecgibson wants to merge 1 commit intomongodb:mainfrom
Open
fix(NODE-5225): eagerly clear MongoClient.topology in MongoClient.close()#3652alecgibson wants to merge 1 commit intomongodb:mainfrom
alecgibson wants to merge 1 commit intomongodb:mainfrom
Conversation
…se() At the moment, calling `MongoClient.close()` in quick succession leads to an error: ``` TypeError: Cannot read properties of undefined (reading 'close') at node-mongodb-native/src/mongo_client.ts:530:16 at new Promise (<anonymous>) at LegacyMongoClient.close (src/mongo_client.ts:529:11) at processTicksAndRejections (node:internal/process/task_queues:95:5) at async Promise.all (index 1) at async Context.<anonymous> (test/integration/node-specific/mongo_client.test.ts:393:5) ``` This happens because: 1. The first call attempts to asynchronously [close][1] the session pool sessions 2. While this is happening, the second call passes the topology [null check][2] (since `this.topology` hasn't yet been [unset][3], and tries to also close session pool sessions 3. Both calls then set [`const topology = this.topology`][4] but the second call will get undefined since the first call [unset][3] `this.topology` 4. The second call now tries to call [`close()`][5] on an `undefined` `topology` and throws This change fixes the issue by moving the `topology` unset immediately after the `null` check, so we know it's always correctly set. [1]: https://github.com/mongodb/node-mongodb-native/blob/325c4bc37decdf12e957bfad8bd4ee4d28b1bf95/src/mongo_client.ts#L516 [2]: https://github.com/mongodb/node-mongodb-native/blob/325c4bc37decdf12e957bfad8bd4ee4d28b1bf95/src/mongo_client.ts#L503 [3]: https://github.com/mongodb/node-mongodb-native/blob/325c4bc37decdf12e957bfad8bd4ee4d28b1bf95/src/mongo_client.ts#L527 [4]: https://github.com/mongodb/node-mongodb-native/blob/325c4bc37decdf12e957bfad8bd4ee4d28b1bf95/src/mongo_client.ts#L526 [5]: https://github.com/mongodb/node-mongodb-native/blob/325c4bc37decdf12e957bfad8bd4ee4d28b1bf95/src/mongo_client.ts#L530
|
Why not do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
At the moment, calling
MongoClient.close()in quick succession leads to an error:This happens because:
this.topologyhasn't yet been unset, and tries to also close session pool sessionsconst topology = this.topologybut the second call will get undefined since the first call unsetthis.topologyclose()on anundefinedtopologyand throwsWhat is changing?
This change fixes the issue by moving the
topologyunset immediately after thenullcheck, so we know it's always correctly set.Is there new documentation needed for these changes?
No
Double check the following
npm run check:lintscripttype(NODE-xxxx)[!]: descriptionfeat(NODE-1234)!: rewriting everything in coffeescript