@@ -16,6 +16,7 @@ import {
1616 MongoErrorLabel ,
1717 MongoExpiredSessionError ,
1818 MongoInvalidArgumentError ,
19+ MongoOperationTimeoutError ,
1920 MongoRuntimeError ,
2021 MongoServerError ,
2122 MongoTransactionError ,
@@ -29,6 +30,7 @@ import { ReadConcernLevel } from './read_concern';
2930import { ReadPreference } from './read_preference' ;
3031import { type AsyncDisposable , configureResourceManagement } from './resource_management' ;
3132import { _advanceClusterTime , type ClusterTime , TopologyType } from './sdam/common' ;
33+ import { type TimeoutContext } from './timeout' ;
3234import {
3335 isTransactionCommand ,
3436 Transaction ,
@@ -101,6 +103,9 @@ export interface EndSessionOptions {
101103 error ?: AnyError ;
102104 force ?: boolean ;
103105 forceClear ?: boolean ;
106+
107+ /** @internal */
108+ timeoutMS ?: number ;
104109}
105110
106111/**
@@ -118,7 +123,7 @@ export class ClientSession
118123 /** @internal */
119124 sessionPool : ServerSessionPool ;
120125 hasEnded : boolean ;
121- clientOptions ? : MongoOptions ;
126+ clientOptions : MongoOptions ;
122127 supports : { causalConsistency : boolean } ;
123128 clusterTime ?: ClusterTime ;
124129 operationTime ?: Timestamp ;
@@ -140,6 +145,9 @@ export class ClientSession
140145 /** @internal */
141146 timeoutMS ?: number ;
142147
148+ /** @internal */
149+ public timeoutContext : TimeoutContext | null = null ;
150+
143151 /**
144152 * Create a client session.
145153 * @internal
@@ -152,7 +160,7 @@ export class ClientSession
152160 client : MongoClient ,
153161 sessionPool : ServerSessionPool ,
154162 options : ClientSessionOptions ,
155- clientOptions ? : MongoOptions
163+ clientOptions : MongoOptions
156164 ) {
157165 super ( ) ;
158166
@@ -272,7 +280,11 @@ export class ClientSession
272280 async endSession ( options ?: EndSessionOptions ) : Promise < void > {
273281 try {
274282 if ( this . inTransaction ( ) ) {
275- await this . abortTransaction ( ) ;
283+ if ( typeof options ?. timeoutMS === 'number' ) {
284+ await this . abortTransaction ( { timeoutMS : options . timeoutMS } ) ;
285+ } else {
286+ await this . abortTransaction ( ) ;
287+ }
276288 }
277289 if ( ! this . hasEnded ) {
278290 const serverSession = this [ kServerSession ] ;
@@ -291,6 +303,7 @@ export class ClientSession
291303 }
292304 } catch ( error ) {
293305 // spec indicates that we should ignore all errors for `endSessions`
306+ if ( MongoOperationTimeoutError . is ( error ) ) throw error ;
294307 squashError ( error ) ;
295308 } finally {
296309 maybeClearPinnedConnection ( this , { force : true , ...options } ) ;
@@ -444,6 +457,8 @@ export class ClientSession
444457
445458 /**
446459 * Commits the currently active transaction in this session.
460+ *
461+ * @param options - Optional options, can be used to override `defaultTimeoutMS`.
447462 */
448463 async commitTransaction ( ) : Promise < void > {
449464 if ( this . transaction . state === TxnState . NO_TRANSACTION ) {
@@ -538,6 +553,8 @@ export class ClientSession
538553
539554 /**
540555 * Aborts the currently active transaction in this session.
556+ *
557+ * @param options - Optional options, can be used to override `defaultTimeoutMS`.
541558 */
542559 async abortTransaction ( ) : Promise < void > {
543560 if ( this . transaction . state === TxnState . NO_TRANSACTION ) {
0 commit comments