Commit e2c1447
committed
fix(NODE-5225): eagerly clear MongoClient.topology in MongoClient.close()
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#L5301 parent 325c4bc commit e2c1447
File tree
2 files changed
+13
-5
lines changed- src
- test/integration/node-specific
2 files changed
+13
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
504 | 504 | | |
505 | 505 | | |
506 | 506 | | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
507 | 511 | | |
508 | 512 | | |
509 | 513 | | |
510 | | - | |
| 514 | + | |
511 | 515 | | |
512 | 516 | | |
513 | 517 | | |
| |||
522 | 526 | | |
523 | 527 | | |
524 | 528 | | |
525 | | - | |
526 | | - | |
527 | | - | |
528 | | - | |
529 | 529 | | |
530 | 530 | | |
531 | 531 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
385 | 385 | | |
386 | 386 | | |
387 | 387 | | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
388 | 396 | | |
389 | 397 | | |
390 | 398 | | |
| |||
0 commit comments