@@ -21,6 +21,7 @@ import { PromiseProvider } from './promise_provider';
2121import type { ReadConcern , ReadConcernLevel , ReadConcernLike } from './read_concern' ;
2222import { ReadPreference , ReadPreferenceMode } from './read_preference' ;
2323import type { TagSet } from './sdam/server_description' ;
24+ import { readPreferenceServerSelector } from './sdam/server_selection' ;
2425import type { SrvPoller } from './sdam/srv_polling' ;
2526import type { Topology , TopologyEvents } from './sdam/topology' ;
2627import { ClientSession , ClientSessionOptions , ServerSessionPool } from './sessions' ;
@@ -500,6 +501,19 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
500501
501502 Promise . all ( activeSessionEnds )
502503 . 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+
503517 const endSessions = Array . from ( this . s . sessionPool . sessions , ( { id } ) => id ) ;
504518 if ( endSessions . length === 0 ) return ;
505519 return this . db ( 'admin' )
@@ -511,7 +525,7 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
511525 } )
512526 . then ( ( ) => {
513527 if ( this . topology == null ) {
514- return callback ( ) ;
528+ return ;
515529 }
516530 // clear out references to old topology
517531 const topology = this . topology ;
0 commit comments