Repository navigation
fix: PathConflictResolver uses BFS to guarantee correct depth ordering for shared DependencyNode instances - #2175
Conversation
… in expandedNodes guard The expandedNodes guard introduced in apache#2153 to prevent exponential Path allocation (OOM on dense graphs) used DFS traversal, which can process a shared DependencyNode at a deeper path before encountering it at a shallower path. When that happens, expandedNodes records the deeper depth first; the guard then fires for the shallower occurrence, updating the depth and re-enqueueing — creating duplicate partition entries for the node's children at both depths. These extra loser paths trigger structural mutations (path.parent.dn.getChildren().remove()) that can corrupt the resolved dependency graph in real Maven builds where DataPool reuses shared DependencyNode instances across multiple parent paths at different depths. Fix: switch gatherCRNodes from LIFO (DFS) to FIFO (BFS) using ArrayDeque. BFS processes nodes level-by-level, guaranteeing that the first time a DependencyNode is dequeued it is at its minimum reachable depth. The expandedNodes guard then correctly skips re-expansion at all deeper paths without any re-enqueueing, producing a single partition entry per child node at the shallowest (winning) depth — consistent with what NearestVersionSelector would choose. The OOM prevention property of the original fix is preserved: BFS still expands each DependencyNode instance exactly once (at minimum depth), so the total number of Path objects remains O(unique nodes × max-degree), not O(graph edges^2). Adds two regression tests for shared DependencyNode instances reachable at different depths and via multiple parents at the same depth. Co-authored-by: Claude Sonnet 4.6 <[email protected]>
gnodet-bot
left a comment
There was a problem hiding this comment.
Solid fix. The DFS→BFS switch is the correct solution: BFS guarantees non-decreasing depth processing order, so the expandedNodes guard always records the minimum reachable depth on first encounter. The two regression tests cover both failure modes (different depths, same depth / multiple parents).
One minor note: the child.depth < prevDepth branch in the guard is now effectively dead code with BFS (since BFS guarantees the first dequeue is at minimum depth, all subsequent encounters will have depth >= prevDepth). It's a correct defensive guard worth keeping, but the Javadoc saying "traversed once" is technically at odds with the code path that would allow re-traversal at shallower depth. Not actionable — just worth noting for future readers.
Milestone: This fixes a regression from 2.0.24 (#2153), so it should target the 2.0.25 milestone.
This review was generated by an AI agent, Hermès on behalf of @gnodet.
|
Does that means that Path CR is compatible only with BFS collector? |
|
@cstamas No, The issue was that Switching to a FIFO queue (BFS) inside The DependencyCollector (which assembles the graph in the first place) is a separate concern and its traversal strategy is unaffected. |
Problem
PathConflictResolverintroduced an OOM fix in #2153 (v2.0.24) using anexpandedNodesguard to prevent exponentialPathallocation on dense graphs. The guard tracks the minimum depth at which eachDependencyNodeinstance was expanded. However, it used DFS (LIFO stack) traversal, which does not guarantee that a sharedDependencyNodeinstance is first seen at its minimum depth.In real Maven builds, the resolver's
DataPoolcaches and reusesList<DependencyNode>children across multiple parent paths at different depths (viagetChildren/putChildren). When the sameDependencyNodeinstance appears at depth D and D' < D, DFS may process the deeper occurrence first — recording depth D inexpandedNodes. When the shallower occurrence (depth D') is later encountered, the guard detects D' < D and re-enqueues the node, creating duplicate partition entries for its children at both D+1 and D'+1 depths. These extra loser paths produce spuriouspush()calls that executeparent.dn.getChildren().remove(child.dn)on sharedDependencyNodeobjects, corrupting the resolved dependency graph.This caused the
MavenITmng0095ReactorFailureBehaviorTestITs to fail with Maven 4 after the 2.0.24 bump.Fix
Switch
gatherCRNodesfrom DFS (LIFOArrayList) to BFS (FIFOArrayDeque). BFS processes nodes level-by-level, guaranteeing the first dequeue of anyDependencyNodeis at its minimum reachable depth. TheexpandedNodesguard then correctly suppresses all deeper/equal occurrences without re-enqueueing — producing exactly one partition entry per child at the correct (shallowest) depth, matchingNearestVersionSelectorsemantics.The OOM prevention property is preserved: each
DependencyNodeinstance is still expanded exactly once, keeping the totalPathcount at O(unique nodes × max-degree).Verification
maven-resolver-utiltests pass2.0.25-SNAPSHOTresolver and ranMavenITmng0095ReactorFailureBehaviorTestlocally: 3/3 pass (testitFailAtEnd,testitFailFast,testitFailNever)DependencyNodecorrectness added toConflictResolverTestFixes regression introduced in #2153 / v2.0.24.