Skip to content

Commit 0505163

Browse files
committed
fix(query-builder): prevent infinite recursion for circular eager chains
Use a per-branch visited Set (eagerLoadChain) to break circular eager chains (e.g. Publisher→Magazine→Journalist→Publisher). Each child query builder gets a copy of its parent's chain with the current source entity added, so parallel branches don't interfere. Only eager relations are tracked — explicit relations are unaffected.
1 parent 0f1d5ca commit 0505163

1 file changed

Lines changed: 42 additions & 38 deletions

File tree

‎src/query-builder/SelectQueryBuilder.ts‎

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export class SelectQueryBuilder<Entity extends ObjectLiteral>
7171
nulls?: "NULLS FIRST" | "NULLS LAST"
7272
}[] = []
7373
protected relationMetadatas: RelationMetadata[] = []
74+
protected eagerLoadChain: Set<string> = new Set()
7475

7576
// -------------------------------------------------------------------------
7677
// Public Implemented Methods
@@ -3628,18 +3629,14 @@ export class SelectQueryBuilder<Entity extends ObjectLiteral>
36283629
this.findOptions.loadEagerRelations,
36293630
)
36303631

3631-
// Track visited eager relations to prevent infinite recursion
3632-
// from circular eager chains (e.g. A→B→C→A)
3633-
const isRootEagerLoad = !queryRunner.data.eagerLoadVisited
3634-
const eagerLoadVisited: Set<string> =
3635-
(queryRunner.data.eagerLoadVisited ??= new Set())
3636-
36373632
await Promise.all(
36383633
this.relationMetadatas.map(async (relation) => {
3634+
// Prevent infinite recursion from circular eager
3635+
// chains (e.g. A→B→C→A). Each branch maintains its
3636+
// own visited set so parallel branches don't interfere.
36393637
if (relation.isEager) {
3640-
const visitKey = `${relation.entityMetadata.name}:${relation.propertyPath}`
3641-
if (eagerLoadVisited.has(visitKey)) return
3642-
eagerLoadVisited.add(visitKey)
3638+
const targetEntity = relation.inverseEntityMetadata.name
3639+
if (this.eagerLoadChain.has(targetEntity)) return
36433640
}
36443641

36453642
const relationTarget = relation.inverseEntityMetadata.target
@@ -3649,31 +3646,42 @@ export class SelectQueryBuilder<Entity extends ObjectLiteral>
36493646
const queryBuilder = this.createQueryBuilder(queryRunner)
36503647
.select(relationAlias)
36513648
.from(relationTarget, relationAlias)
3652-
.setFindOptions({
3653-
select: this.findOptions.select
3654-
? OrmUtils.deepValue(
3655-
this.findOptions.select,
3656-
relation.propertyPath,
3657-
)
3658-
: undefined,
3659-
order: this.findOptions.order
3660-
? OrmUtils.deepValue(
3661-
this.findOptions.order,
3662-
relation.propertyPath,
3663-
)
3664-
: undefined,
3665-
relations: this.findOptions.relations
3666-
? OrmUtils.deepValue(
3667-
this.findOptions.relations,
3668-
relation.propertyPath,
3669-
)
3670-
: undefined,
3671-
withDeleted: this.findOptions.withDeleted,
3672-
relationLoadStrategy:
3673-
this.findOptions.relationLoadStrategy,
3674-
loadEagerRelations:
3675-
this.findOptions.loadEagerRelations,
3676-
})
3649+
3650+
// Propagate eager load chain with current entity
3651+
// added, so the child detects cycles in its branch
3652+
if (relation.isEager) {
3653+
queryBuilder.eagerLoadChain = new Set(
3654+
this.eagerLoadChain,
3655+
)
3656+
queryBuilder.eagerLoadChain.add(
3657+
relation.entityMetadata.name,
3658+
)
3659+
}
3660+
3661+
queryBuilder.setFindOptions({
3662+
select: this.findOptions.select
3663+
? OrmUtils.deepValue(
3664+
this.findOptions.select,
3665+
relation.propertyPath,
3666+
)
3667+
: undefined,
3668+
order: this.findOptions.order
3669+
? OrmUtils.deepValue(
3670+
this.findOptions.order,
3671+
relation.propertyPath,
3672+
)
3673+
: undefined,
3674+
relations: this.findOptions.relations
3675+
? OrmUtils.deepValue(
3676+
this.findOptions.relations,
3677+
relation.propertyPath,
3678+
)
3679+
: undefined,
3680+
withDeleted: this.findOptions.withDeleted,
3681+
relationLoadStrategy:
3682+
this.findOptions.relationLoadStrategy,
3683+
loadEagerRelations: this.findOptions.loadEagerRelations,
3684+
})
36773685
if (entities.length > 0) {
36783686
const relatedEntityGroups: any[] =
36793687
await queryStrategyRelationIdLoader.loadManyToManyRelationIdsAndGroup(
@@ -3697,10 +3705,6 @@ export class SelectQueryBuilder<Entity extends ObjectLiteral>
36973705
}
36983706
}),
36993707
)
3700-
3701-
if (isRootEagerLoad) {
3702-
delete queryRunner.data.eagerLoadVisited
3703-
}
37043708
}
37053709

37063710
return {

0 commit comments

Comments
 (0)