Skip to content

Backport #12446: Fix deadlock in AbstractRequestCache - #12468

Merged
gnodet merged 1 commit into
apache:maven-4.0.xfrom
gnodet:backport/12446-to-4.0.x
Jul 11, 2026
Merged

gnodet merged 1 commit into
apache:maven-4.0.xfrom
gnodet:backport/12446-to-4.0.x

Conversation

@gnodet

@gnodet gnodet commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Summary

Backport of #12446, which fixes #12445.

Cherry-pick of squash-merge commit c6de104 from master to maven-4.0.x.

The fix replaces the wait/notify pattern in AbstractRequestCache.requests() with direct value setting via CachingSupplier.complete(), preventing deadlocks when re-entrant requests() calls occur (e.g., parent POM resolution during artifact resolution). It also adds ThreadLocal re-entrancy detection and setBatchResolving() so concurrent threads wait for in-progress batch results while same-thread re-entrant calls use a fallback supplier.

Conflicts were resolved during cherry-pick (the 4.0.x branch had a CyclicCacheAccessException-based approach that was replaced by the new batch-resolution pattern).

Test plan

  • CI passes on maven-4.0.x
  • AbstractRequestCacheTest passes (included in this backport)

🤖 Generated with Claude Code

… parent POMs (apache#12446)

* Fix apache#12445: Fix deadlock in AbstractRequestCache when resolving parent POMs

Replace the wait/notify pattern in AbstractRequestCache.requests() with
direct value setting via CachingSupplier.complete(). The old pattern
stored a wait-based individualSupplier in CachingSupplier instances
cached across calls. When a re-entrant requests() call (e.g., parent
POM resolution during artifact resolution) retrieved a CachingSupplier
from the cache, it would invoke the outer call's individualSupplier,
which waited on a HashMap that would never be notified — deadlock.

The fix:
- Add CachingSupplier.complete() to set values without invoking the supplier
- Pass a single-item fallback supplier to doCache() so newly created
  CachingSuppliers can independently resolve requests in re-entrant scenarios
- After batch resolution, directly set results via complete()
- Remove the synchronized wait/notify on HashMap entirely

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* Fix apache#12445: Prevent duplicate concurrent resolutions via wait/notifyAll coordination

Add ThreadLocal re-entrancy detection and CachingSupplier.setBatchResolving()
so that concurrent threads wait for an in-progress batch result (via
Object.wait/notifyAll) instead of resolving the same request independently,
while same-thread re-entrant calls still use the fallback supplier to
avoid deadlock. Also fix CachingTestRequestCache to use ConcurrentHashMap,
clean up duplicate javadoc, and add concurrent resolution test.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* Fix apache#12445: Use IdentityHashMap for reqToIndex to handle unstable hashCode

ResolverRequest.hashCode() can change during batch resolution because
RequestTrace includes mutable ModelBuilderRequest data (identified in
PR apache#12166). Since reqToIndex always uses the same object references
for put and get, IdentityHashMap is safe and avoids missed lookups
that would cause redundant individual resolutions.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* Fix apache#12445: Add test for batch resolution with unstable hashCode

Verify that batch results are correctly delivered via complete() even
when request hashCode() changes during resolution (simulating
ResolverRequest with mutable RequestTrace/ModelBuilderRequest data).
The IdentityHashMap-based reqToIndex ensures lookups succeed by
reference identity regardless of hashCode mutations.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* Fix apache#12445: Address Copilot review — consistent error handling and safe unblocking

- catch(Throwable) now falls through to the collection loop and produces
  a proper BatchRequestException with per-request results, consistent
  with the MavenExecutionException path.
- setBatchResolving(false) now calls notifyAll() to unblock any threads
  still waiting in apply() when complete() was never called (e.g., batch
  supplier returned fewer results than expected).
- Add test for partial batch result edge case.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
@gnodet
gnodet marked this pull request as ready for review July 11, 2026 16:52

@gnodet gnodet left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Review Summary

Clean backport of #12446 (deadlock fix in AbstractRequestCache) to the 4.0.x branch.

Observations:

  • Single-commit cherry-pick of the squash-merge commit — clean and reviewable
  • Final file states for AbstractRequestCache.java and CachingSupplier.java are bit-identical to master (matching blob SHAs), confirming a faithful port
  • The 4.0.x baseline had a CyclicCacheAccessException-based re-entrancy approach which the backport correctly replaces with the new ThreadLocal-based re-entrancy detection and batchResolving/complete() coordination pattern
  • All 5 new tests (deadlock re-entrancy, concurrent deduplication, unstable hashCode, partial batch unblocking, batch result caching) plus 3 helper classes are included, matching the original PR exactly

LGTM — own PR, cannot approve.

Claude Code on behalf of Guillaume Nodet
This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.

@gnodet
gnodet merged commit 0c8d2b4 into apache:maven-4.0.x Jul 11, 2026
22 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

@gnodet Please assign appropriate label to PR according to the type of change.

@github-actions github-actions Bot added this to the 4.0.0-rc-6 milestone Jul 11, 2026
@gnodet gnodet added the bug Something isn't working label Jul 24, 2026
@gnodet
gnodet deleted the backport/12446-to-4.0.x branch October 26, 2026 07:29
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.

1 participant