Skip to content

Conversation

@gnodet
Copy link
Contributor

@gnodet gnodet commented Oct 9, 2025

This PR backports the fix from #11106 to the maven-4.0.x branch.

Problem

When Maven encounters a cyclic dependency in parent POM resolution (e.g., A -> B -> A), it would enter an infinite recursion loop, eventually causing a StackOverflowError. This made Maven crash ungracefully without providing useful feedback to the user.

Solution

The fix implements cycle detection in the DefaultModelBuilder by:

  1. Tracking visited parents: Maintains a set of visited parent coordinates during resolution
  2. Cycle detection: When a parent that has already been visited is encountered again, it indicates a cycle
  3. Graceful error handling: Throws a ModelBuildingException with a clear error message instead of crashing

Changes Made

  • Modified DefaultModelBuilder: Added cycle detection logic in parent POM resolution methods
  • Added unit tests: Comprehensive test case (ParentCycleDetectionTest) that verifies the fix works correctly
  • Added integration test: Integration test (MavenITmng11009StackOverflowParentResolutionTest) with test resources
  • Test resources: Created test POMs with cyclic parent dependencies

Testing

The fix includes comprehensive tests that:

  • Create scenarios with cyclic parent dependencies (A -> B -> A)
  • Verify that StackOverflowError no longer occurs
  • Confirm that an appropriate cycle detection error is thrown
  • Ensure Maven fails gracefully with a meaningful error message
  • Test that multiple modules with the same parent don't cause false cycle detection

Backward Compatibility

This change is backward compatible as it only affects error handling for invalid POM structures. Valid POMs continue to work as before, while invalid cyclic structures now fail gracefully instead of crashing.

Related Issues


Pull Request opened by Augment Code with guidance from the PR author

This commit backports the fix from PR apache#11106 to the maven-4.0.x branch.

The fix addresses issue apache#11009 where Maven would encounter a StackOverflowError
when resolving parent POMs that form a cycle.

## Problem

When Maven encounters a cyclic dependency in parent POM resolution (e.g., A -> B
-> A), it would enter an infinite recursion loop, eventually causing a StackOverflowError.
This made Maven crash ungracefully without providing useful feedback to the user.

## Solution

The fix implements cycle detection in the DefaultModelBuilder by:

1. **Tracking visited parents**: Maintains a set of visited parent coordinates during
   resolution

2. **Cycle detection**: When a parent that has already been visited is encountered
   again, it indicates a cycle

3. **Graceful error handling**: Throws a ModelBuildingException with a clear error
   message instead of crashing

## Changes Made

- **Modified DefaultModelBuilder**: Added cycle detection logic in parent POM
  resolution methods

- **Added integration test**: Comprehensive test case that verifies the fix works
  correctly

- **Test resources**: Created test POMs with cyclic parent dependencies

## Testing

The fix includes a comprehensive integration test (MavenITmng11009StackOverflowParentResolutionTest)
that:

- Creates a scenario with cyclic parent dependencies (A -> B -> A)
- Verifies that StackOverflowError no longer occurs
- Confirms that an appropriate cycle detection error is thrown
- Ensures Maven fails gracefully with a meaningful error message

## Backward Compatibility

This change is backward compatible as it only affects error handling for invalid
POM structures. Valid POMs continue to work as before, while invalid cyclic structures
now fail gracefully instead of crashing.
@gnodet gnodet added bug Something isn't working backport labels Oct 9, 2025
@gnodet gnodet changed the title Backport #11106: Fix StackOverflowError in parent POM resolution to maven-4.0.x [maven-4.0.x] #11106: Fix StackOverflowError in parent POM resolution to maven-4.0.x Oct 9, 2025
@gnodet gnodet changed the title [maven-4.0.x] #11106: Fix StackOverflowError in parent POM resolution to maven-4.0.x [maven-4.0.x] Fix StackOverflowError in parent POM resolution (backport #11106) Oct 9, 2025
@gnodet gnodet merged commit e552422 into apache:maven-4.0.x Oct 9, 2025
37 of 38 checks passed
@github-actions github-actions bot added this to the 4.0.0 milestone Oct 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant