@@ -21,6 +21,7 @@ import { PromiseProvider } from './promise_provider';
21
21
import type { ReadConcern , ReadConcernLevel , ReadConcernLike } from './read_concern' ;
22
22
import { ReadPreference , ReadPreferenceMode } from './read_preference' ;
23
23
import type { TagSet } from './sdam/server_description' ;
24
+ import { readPreferenceServerSelector } from './sdam/server_selection' ;
24
25
import type { SrvPoller } from './sdam/srv_polling' ;
25
26
import type { Topology , TopologyEvents } from './sdam/topology' ;
26
27
import { ClientSession , ClientSessionOptions , ServerSessionPool } from './sessions' ;
@@ -500,6 +501,19 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
500
501
501
502
Promise . all ( activeSessionEnds )
502
503
. then ( ( ) => {
504
+ if ( this . topology == null ) {
505
+ return ;
506
+ }
507
+ // If we would attempt to select a server and get nothing back we short circuit
508
+ // to avoid the server selection timeout.
509
+ const selector = readPreferenceServerSelector ( ReadPreference . primaryPreferred ) ;
510
+ const topologyDescription = this . topology . description ;
511
+ const serverDescriptions = Array . from ( topologyDescription . servers . values ( ) ) ;
512
+ const servers = selector ( topologyDescription , serverDescriptions ) ;
513
+ if ( servers . length === 0 ) {
514
+ return ;
515
+ }
516
+
503
517
const endSessions = Array . from ( this . s . sessionPool . sessions , ( { id } ) => id ) ;
504
518
if ( endSessions . length === 0 ) return ;
505
519
return this . db ( 'admin' )
@@ -511,7 +525,7 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
511
525
} )
512
526
. then ( ( ) => {
513
527
if ( this . topology == null ) {
514
- return callback ( ) ;
528
+ return ;
515
529
}
516
530
// clear out references to old topology
517
531
const topology = this . topology ;
0 commit comments