gnodet commented on PR #12832:
URL: https://github.com/apache/maven/pull/12832#issuecomment-5435493220

   Thanks for the fix — the null guards are a clear improvement over the opaque 
NPE. One thing worth addressing in this PR though:
   
   ## Root cause: `HashMap` corruption under parallel builds (`-T`)
   
   The most likely real-world trigger for this NPE is **concurrent access to 
the `projects` map in `ProjectIndex`**. In 
`MojoExecutor.executeForkedExecutions()`, the `projects` map is both read 
(`.get()` at line 443) and written (`.put()` at lines 460, 472) — and in 
parallel builds, multiple threads do this concurrently through the same cached 
`ProjectIndex` instance.
   
   Plain `HashMap` is not thread-safe for concurrent read/write. The internal 
structure can silently corrupt (especially during resize), causing `.get()` to 
return `null` for a key that **is** in the map. In that scenario, the 
`projectId` is valid and present — it's the `HashMap` corruption that makes it 
appear missing.
   
   With only the null guards, the build still **fails** in this case — just 
with a better message. But it shouldn't fail at all.
   
   ### Suggested additional change
   
   In `ProjectIndex.java`, change the `projects` map from `HashMap` to 
`ConcurrentHashMap`:
   
   ```java
   public ProjectIndex(List<MavenProject> projects) {
       this.projects = new ConcurrentHashMap<>(projects.size() * 2);
       this.indices = new HashMap<>(projects.size() * 2);  // read-only after 
construction, safe as-is
       ...
   }
   ```
   
   The `indices` map can stay as `HashMap` — it's never written to after 
construction, so it's safe for concurrent reads.
   
   This way:
   - **Parallel builds**: `ConcurrentHashMap` prevents the corruption → build 
succeeds (root cause fixed)
   - **Stale cached index / extensions**: the null guards in `MojoExecutor` 
catch genuinely missing keys → clear error (your fix)
   
   Both changes together give a complete fix. What do you think?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to