@@ -12,6 +12,7 @@ import { collectCursorUntilMaxBytesLimit } from "../../../helpers/collectCursorU
12
12
import { operationWithFallback } from "../../../helpers/operationWithFallback.js" ;
13
13
import { AGG_COUNT_MAX_TIME_MS_CAP , ONE_MB , CURSOR_LIMITS_TO_LLM_TEXT } from "../../../helpers/constants.js" ;
14
14
import { zEJSON } from "../../args.js" ;
15
+ import { LogId } from "../../../common/logger.js" ;
15
16
16
17
export const AggregateArgs = {
17
18
pipeline : z . array ( zEJSON ( ) ) . describe ( "An array of aggregation stages to execute" ) ,
@@ -88,10 +89,25 @@ export class AggregateTool extends MongoDBToolBase {
88
89
) ,
89
90
} ;
90
91
} finally {
92
+ if ( aggregationCursor ) {
93
+ void this . safeCloseCursor ( aggregationCursor ) ;
94
+ }
91
95
await aggregationCursor ?. close ( ) ;
92
96
}
93
97
}
94
98
99
+ private async safeCloseCursor ( cursor : AggregationCursor < unknown > ) : Promise < void > {
100
+ try {
101
+ await cursor . close ( ) ;
102
+ } catch ( error ) {
103
+ this . session . logger . warning ( {
104
+ id : LogId . mongodbCursorCloseError ,
105
+ context : "aggregate tool" ,
106
+ message : `Error when closing the cursor - ${ error instanceof Error ? error . message : String ( error ) } ` ,
107
+ } ) ;
108
+ }
109
+ }
110
+
95
111
private assertOnlyUsesPermittedStages ( pipeline : Record < string , unknown > [ ] ) : void {
96
112
const writeOperations : OperationType [ ] = [ "update" , "create" , "delete" ] ;
97
113
let writeStageForbiddenError = "" ;
0 commit comments