Skip to content

fix: PathConflictResolver uses BFS to guarantee correct depth ordering for shared DependencyNode instances - #2175

Merged
cstamas merged 1 commit into
apache:masterfrom
gnodet:fix/path-conflict-resolver-shared-node-corruption
Oct 12, 2026
Merged

cstamas merged 1 commit into
apache:masterfrom
gnodet:fix/path-conflict-resolver-shared-node-corruption

Conversation

@gnodet

@gnodet gnodet commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Problem

PathConflictResolver introduced an OOM fix in #2153 (v2.0.24) using an expandedNodes guard to prevent exponential Path allocation on dense graphs. The guard tracks the minimum depth at which each DependencyNode instance was expanded. However, it used DFS (LIFO stack) traversal, which does not guarantee that a shared DependencyNode instance is first seen at its minimum depth.

In real Maven builds, the resolver's DataPool caches and reuses List<DependencyNode> children across multiple parent paths at different depths (via getChildren/putChildren). When the same DependencyNode instance appears at depth D and D' < D, DFS may process the deeper occurrence first — recording depth D in expandedNodes. 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 spurious push() calls that execute parent.dn.getChildren().remove(child.dn) on shared DependencyNode objects, corrupting the resolved dependency graph.

This caused the MavenITmng0095ReactorFailureBehaviorTest ITs to fail with Maven 4 after the 2.0.24 bump.

Fix

Switch gatherCRNodes from DFS (LIFO ArrayList) to BFS (FIFO ArrayDeque). BFS processes nodes level-by-level, guaranteeing the first dequeue of any DependencyNode is at its minimum reachable depth. The expandedNodes guard then correctly suppresses all deeper/equal occurrences without re-enqueueing — producing exactly one partition entry per child at the correct (shallowest) depth, matching NearestVersionSelector semantics.

The OOM prevention property is preserved: each DependencyNode instance is still expanded exactly once, keeping the total Path count at O(unique nodes × max-degree).

Verification

  • All 467 maven-resolver-util tests pass
  • Built Maven 4.1.0-SNAPSHOT with 2.0.25-SNAPSHOT resolver and ran MavenITmng0095ReactorFailureBehaviorTest locally: 3/3 pass (testitFailAtEnd, testitFailFast, testitFailNever)
  • Two new regression tests for shared DependencyNode correctness added to ConflictResolverTest

Fixes regression introduced in #2153 / v2.0.24.

… 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 gnodet-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@cstamas

cstamas commented Oct 8, 2026

Copy link
Copy Markdown
Member

Does that means that Path CR is compatible only with BFS collector?

@gnodet

gnodet commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

@cstamas No, PathConflictResolver is independent of how the collector traverses the graph. The BFS here refers exclusively to the traversal order inside gatherCRNodes — the internal method that walks the already-collected dependency graph to build the set of conflict-resolution nodes.

The issue was that gatherCRNodes used a LIFO stack (DFS), which does not guarantee that a shared DependencyNode instance is first encountered at its minimum reachable depth. When the resolver's DataPool reuses the same DependencyNode instance across multiple parent paths at different depths, DFS can process a deeper occurrence first — recording the wrong depth in the expandedNodes guard — which then causes duplicate partition entries and corrupts the resolved graph.

Switching to a FIFO queue (BFS) inside gatherCRNodes guarantees that the first time any DependencyNode is dequeued it is at its minimum reachable depth, so the guard correctly suppresses all deeper/equal occurrences without re-enqueueing.

The DependencyCollector (which assembles the graph in the first place) is a separate concern and its traversal strategy is unaffected.

@cstamas cstamas added this to the 2.0.25 milestone Oct 12, 2026
@cstamas cstamas added enhancement New feature or request bug Something isn't working and removed enhancement New feature or request labels Oct 12, 2026
@cstamas
cstamas merged commit 9add368 into apache:master Oct 12, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants