@@ -264,10 +264,6 @@ export abstract class AbstractCursor<
264
264
throw new MongoRuntimeError ( 'Cursor must be constructed with MongoClient' ) ;
265
265
}
266
266
this . cursorClient = client ;
267
-
268
- this . cursorClient . s . activeCursors . add ( this ) ;
269
- this . once ( 'close' , removeActiveCursor ) ;
270
-
271
267
this . cursorNamespace = namespace ;
272
268
this . cursorId = null ;
273
269
this . initialized = false ;
@@ -360,6 +356,7 @@ export abstract class AbstractCursor<
360
356
} ;
361
357
362
358
this . timeoutContext = options . timeoutContext ;
359
+ this . trackCursor ( ) ;
363
360
}
364
361
365
362
/**
@@ -439,6 +436,14 @@ export abstract class AbstractCursor<
439
436
await this . close ( ) ;
440
437
}
441
438
439
+ /** Adds cursor to client's tracking so it will be closed by MongoClient.close() */
440
+ private trackCursor ( ) {
441
+ this . cursorClient . s . activeCursors . add ( this ) ;
442
+ if ( ! this . listeners ( 'close' ) . includes ( removeActiveCursor ) ) {
443
+ this . once ( 'close' , removeActiveCursor ) ;
444
+ }
445
+ }
446
+
442
447
/** Returns current buffered documents length */
443
448
bufferedCount ( ) : number {
444
449
return this . documents ?. length ?? 0 ;
@@ -832,21 +837,14 @@ export abstract class AbstractCursor<
832
837
this . isClosed = false ;
833
838
this . isKilled = false ;
834
839
this . initialized = false ;
840
+ this . trackCursor ( ) ;
835
841
836
- this . cursorClient . s . activeCursors . add ( this ) ;
837
- if ( ! this . listeners ( 'close' ) . includes ( removeActiveCursor ) ) {
838
- this . once ( 'close' , removeActiveCursor ) ;
839
- }
840
-
841
- const session = this . cursorSession ;
842
- if ( session ) {
843
- // We only want to end this session if we created it, and it hasn't ended yet
844
- if ( session . explicit === false ) {
845
- if ( ! session . hasEnded ) {
846
- session . endSession ( ) . then ( undefined , squashError ) ;
847
- }
848
- this . cursorSession = this . cursorClient . startSession ( { owner : this , explicit : false } ) ;
842
+ // We only want to end this session if we created it, and it hasn't ended yet
843
+ if ( this . cursorSession . explicit === false ) {
844
+ if ( ! this . cursorSession . hasEnded ) {
845
+ this . cursorSession . endSession ( ) . then ( undefined , squashError ) ;
849
846
}
847
+ this . cursorSession = this . cursorClient . startSession ( { owner : this , explicit : false } ) ;
850
848
}
851
849
}
852
850
@@ -982,7 +980,6 @@ export abstract class AbstractCursor<
982
980
/** @internal */
983
981
private async cleanup ( timeoutMS ?: number , error ?: Error ) {
984
982
this . isClosed = true ;
985
- const session = this . cursorSession ;
986
983
const timeoutContextForKillCursors = ( ) : CursorTimeoutContext | undefined => {
987
984
if ( timeoutMS != null ) {
988
985
this . timeoutContext ?. clear ( ) ;
@@ -1004,7 +1001,7 @@ export abstract class AbstractCursor<
1004
1001
! this . cursorId . isZero ( ) &&
1005
1002
this . cursorNamespace &&
1006
1003
this . selectedServer &&
1007
- ! session . hasEnded
1004
+ ! this . cursorSession . hasEnded
1008
1005
) {
1009
1006
this . isKilled = true ;
1010
1007
const cursorId = this . cursorId ;
@@ -1013,7 +1010,7 @@ export abstract class AbstractCursor<
1013
1010
await executeOperation (
1014
1011
this . cursorClient ,
1015
1012
new KillCursorsOperation ( cursorId , this . cursorNamespace , this . selectedServer , {
1016
- session
1013
+ session : this . cursorSession
1017
1014
} ) ,
1018
1015
timeoutContextForKillCursors ( )
1019
1016
) ;
@@ -1022,11 +1019,11 @@ export abstract class AbstractCursor<
1022
1019
squashError ( error ) ;
1023
1020
} finally {
1024
1021
try {
1025
- if ( session ?. owner === this ) {
1026
- await session . endSession ( { error } ) ;
1022
+ if ( this . cursorSession ?. owner === this ) {
1023
+ await this . cursorSession . endSession ( { error } ) ;
1027
1024
}
1028
- if ( ! session ?. inTransaction ( ) ) {
1029
- maybeClearPinnedConnection ( session , { error } ) ;
1025
+ if ( ! this . cursorSession ?. inTransaction ( ) ) {
1026
+ maybeClearPinnedConnection ( this . cursorSession , { error } ) ;
1030
1027
}
1031
1028
} finally {
1032
1029
this . emitClose ( ) ;
0 commit comments