@@ -799,13 +799,28 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
799799 const areSessionsSupported = topologyDescription . logicalSessionTimeoutMinutes != null ;
800800
801801 if ( areSessionsSupported ) {
802+ await endSessions ( this ) ;
803+ }
804+
805+ // clear out references to old topology
806+ const topology = this . topology ;
807+ this . topology = undefined ;
808+
809+ topology . close ( ) ;
810+
811+ const { encrypter } = this . options ;
812+ if ( encrypter ) {
813+ await encrypter . close ( this ) ;
814+ }
815+
816+ async function endSessions ( client : MongoClient ) {
802817 // If we would attempt to select a server and get nothing back we short circuit
803818 // to avoid the server selection timeout.
804819 const selector = readPreferenceServerSelector ( ReadPreference . primaryPreferred ) ;
805820 const serverDescriptions = Array . from ( topologyDescription . servers . values ( ) ) ;
806821 const servers = selector ( topologyDescription , serverDescriptions ) ;
807822 if ( servers . length !== 0 ) {
808- const endSessions = Array . from ( this . s . sessionPool . sessions , ( { id } ) => id ) ;
823+ const endSessions = Array . from ( client . s . sessionPool . sessions , ( { id } ) => id ) ;
809824 if ( endSessions . length !== 0 ) {
810825 try {
811826 class EndSessionsOperation extends AbstractOperation < void > {
@@ -827,24 +842,13 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
827842 return 'endSessions' ;
828843 }
829844 }
830- await executeOperation ( this , new EndSessionsOperation ( ) ) ;
845+ await executeOperation ( client , new EndSessionsOperation ( ) ) ;
831846 } catch ( error ) {
832847 squashError ( error ) ;
833848 }
834849 }
835850 }
836851 }
837-
838- // clear out references to old topology
839- const topology = this . topology ;
840- this . topology = undefined ;
841-
842- topology . close ( ) ;
843-
844- const { encrypter } = this . options ;
845- if ( encrypter ) {
846- await encrypter . close ( this ) ;
847- }
848852 }
849853
850854 /**
0 commit comments