Skip to content

[Backport maven-3.10.x] Do not store the default current project in threads that only read it - #13803

Merged
gnodet merged 1 commit into
apache:maven-3.10.xfrom
gnodet:backport/13794-to-maven-3.10.x
Nov 3, 2026
Merged

gnodet merged 1 commit into
apache:maven-3.10.xfrom
gnodet:backport/13794-to-maven-3.10.x

Conversation

@gnodet

@gnodet gnodet commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Backport of #13794 to maven-3.10.x.

Cherry-pick of 51bee60 — required conflict resolution.

Changes:

  • MavenSession.currentProject is now a plain ThreadLocal (no withInitial), with a separate defaultProject field holding the fallback. Reading getCurrentProject() on a thread that never called setCurrentProject() returns the default without storing it in the thread's map, preventing stale references that kept the project (and the whole build graph) alive on long-lived threads (e.g. ForkJoinPool.commonPool workers in mvnd).
  • clone() copies the current project into the clone's defaultProject instead of creating a new ThreadLocal.withInitial.
  • New test MavenSessionTest.testReadingCurrentProjectOnOtherThreadDoesNotRetainProject verifies threads don't retain a strong reference to the project after the session is discarded.

Adaptation for maven-3.10.x:

  • Module path: maven-core/ (not impl/maven-core/)
  • setProjects() default project is first (first in reactor order), preserving existing maven-3.10.x behavior (master uses the execution root)
  • Test uses Collections.singletonList() instead of List.of() (Java 8 source level)
  • Test uses 4-arg constructor MavenSession(container, repoSession, request, result) (no 3-arg constructor on 3.10.x)

â€Ķapache#13794)

MavenSession kept the current project in a ThreadLocal created with
ThreadLocal.withInitial(...), and clone() gave every clone a new one.
With withInitial, a plain get() on a thread without a value stores the
initial value in that thread's ThreadLocalMap. Every thread that only
read getCurrentProject() therefore kept a strong reference to the
default project, and clone() itself reads the current project of the
original session. After the session is discarded, these stale entries
keep the project, and through it the whole build, reachable as long as
the thread lives, e.g. on ForkJoinPool.commonPool workers in mvnd.

The default project is now kept in a field, and the ThreadLocal holds
only explicitly set projects (null is stored as Optional.empty()).
Reading the current project on a thread without a value returns the
default without storing it. The behavior is unchanged.

Co-Authored-By: Claude Code (Anthropic Claude Opus 5.5, claude-opus-5-5)
@gnodet gnodet added this to the 3.10.1 milestone Nov 3, 2026
@gnodet gnodet added bug Something isn't working backport labels Nov 3, 2026
@gnodet
gnodet merged commit 7dda713 into apache:maven-3.10.x Nov 3, 2026
15 checks passed
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.

3 participants