Skip to content

Commit 955b7d8

Browse files
chore: account for cursor close errors
1 parent 8601c05 commit 955b7d8

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/common/logger.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const LogId = {
4444
mongodbConnectFailure: mongoLogId(1_004_001),
4545
mongodbDisconnectFailure: mongoLogId(1_004_002),
4646
mongodbConnectTry: mongoLogId(1_004_003),
47+
mongodbCursorCloseError: mongoLogId(1_004_004),
4748

4849
toolUpdateFailure: mongoLogId(1_005_001),
4950
resourceUpdateFailure: mongoLogId(1_005_002),

src/tools/mongodb/read/aggregate.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { collectCursorUntilMaxBytesLimit } from "../../../helpers/collectCursorU
1212
import { operationWithFallback } from "../../../helpers/operationWithFallback.js";
1313
import { AGG_COUNT_MAX_TIME_MS_CAP, ONE_MB, CURSOR_LIMITS_TO_LLM_TEXT } from "../../../helpers/constants.js";
1414
import { zEJSON } from "../../args.js";
15+
import { LogId } from "../../../common/logger.js";
1516

1617
export const AggregateArgs = {
1718
pipeline: z.array(zEJSON()).describe("An array of aggregation stages to execute"),
@@ -88,10 +89,25 @@ export class AggregateTool extends MongoDBToolBase {
8889
),
8990
};
9091
} finally {
92+
if (aggregationCursor) {
93+
void this.safeCloseCursor(aggregationCursor);
94+
}
9195
await aggregationCursor?.close();
9296
}
9397
}
9498

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+
95111
private assertOnlyUsesPermittedStages(pipeline: Record<string, unknown>[]): void {
96112
const writeOperations: OperationType[] = ["update", "create", "delete"];
97113
let writeStageForbiddenError = "";

src/tools/mongodb/read/find.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { collectCursorUntilMaxBytesLimit } from "../../../helpers/collectCursorU
1010
import { operationWithFallback } from "../../../helpers/operationWithFallback.js";
1111
import { ONE_MB, QUERY_COUNT_MAX_TIME_MS_CAP, CURSOR_LIMITS_TO_LLM_TEXT } from "../../../helpers/constants.js";
1212
import { zEJSON } from "../../args.js";
13+
import { LogId } from "../../../common/logger.js";
1314

1415
export const FindArgs = {
1516
filter: zEJSON()
@@ -101,7 +102,21 @@ export class FindTool extends MongoDBToolBase {
101102
),
102103
};
103104
} finally {
104-
await findCursor?.close();
105+
if (findCursor) {
106+
void this.safeCloseCursor(findCursor);
107+
}
108+
}
109+
}
110+
111+
private async safeCloseCursor(cursor: FindCursor<unknown>): Promise<void> {
112+
try {
113+
await cursor.close();
114+
} catch (error) {
115+
this.session.logger.warning({
116+
id: LogId.mongodbCursorCloseError,
117+
context: "find tool",
118+
message: `Error when closing the cursor - ${error instanceof Error ? error.message : String(error)}`,
119+
});
105120
}
106121
}
107122

0 commit comments

Comments
 (0)