Skip to content

Commit 2dc9624

Browse files
authored
fix: mongodb resolves leaked cursor (#10316)
This change resolves a leaked cursor used to avoid a stack overflow during entity transformation. Closes: #10315
1 parent 3af891a commit 2dc9624

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/entity-manager/MongoEntityManager.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,11 +1025,11 @@ export class MongoEntityManager extends EntityManager {
10251025
cursor: FindCursor<Entity> | AggregationCursor<Entity>,
10261026
) {
10271027
const queryRunner = this.mongoQueryRunner
1028-
cursor.toArray = () =>
1029-
cursor
1030-
.clone()
1031-
.toArray()
1032-
.then(async (results: Entity[]) => {
1028+
1029+
;(cursor as any)["__to_array_func"] = cursor.toArray
1030+
cursor.toArray = async () =>
1031+
((cursor as any)["__to_array_func"] as CallableFunction)().then(
1032+
async (results: Entity[]) => {
10331033
const transformer = new DocumentToEntityTransformer()
10341034
const entities = transformer.transformAll(results, metadata)
10351035
// broadcast "load" events
@@ -1039,13 +1039,12 @@ export class MongoEntityManager extends EntityManager {
10391039
entities,
10401040
)
10411041
return entities
1042-
})
1043-
1044-
cursor.next = () =>
1045-
cursor
1046-
.clone()
1047-
.next()
1048-
.then(async (result: Entity) => {
1042+
},
1043+
)
1044+
;(cursor as any)["__next_func"] = cursor.next
1045+
cursor.next = async () =>
1046+
((cursor as any)["__next_func"] as CallableFunction)().then(
1047+
async (result: Entity) => {
10491048
if (!result) {
10501049
return result
10511050
}
@@ -1056,7 +1055,8 @@ export class MongoEntityManager extends EntityManager {
10561055
entity,
10571056
])
10581057
return entity
1059-
})
1058+
},
1059+
)
10601060
}
10611061

10621062
protected filterSoftDeleted<Entity>(

0 commit comments

Comments
 (0)