gnodet-bot commented on code in PR #14133:
URL: https://github.com/apache/maven/pull/14133#discussion_r4728450722


##########
impl/maven-impl/src/main/java/org/apache/maven/impl/MutableProject.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.impl;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CopyOnWriteArraySet;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Predicate;
+
+import org.apache.maven.api.Project;
+import org.apache.maven.api.SourceRoot;
+import org.apache.maven.api.annotations.Nonnull;
+import org.apache.maven.api.annotations.Nullable;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Internal stable identity for a project whose state is represented by 
immutable snapshots.
+ *
+ * @param <A> the legacy attached-artifact representation
+ */
+public final class MutableProject<A> {
+    private final AtomicReference<Project> project = new AtomicReference<>();
+    private final CopyOnWriteArraySet<SourceRoot> sourceRoots = new 
CopyOnWriteArraySet<>();
+    private final CopyOnWriteArrayList<A> attachedArtifacts = new 
CopyOnWriteArrayList<>();
+    private volatile Properties properties = new Properties();
+
+    public void initialize(@Nonnull Project project) {
+        Project value = requireNonNull(project, "project cannot be null");
+        if (!this.project.compareAndSet(null, value) && this.project.get() != 
value) {
+            throw new IllegalStateException("project has already been 
initialized");
+        }
+    }
+
+    @Nonnull
+    public Project project() {
+        return requireNonNull(project.get(), "project has not been 
initialized");
+    }
+
+    @Nullable
+    public Project projectOrNull() {
+        return project.get();
+    }
+
+    public void replace(@Nonnull Project current, @Nonnull Project 
replacement) {
+        requireNonNull(current, "current project cannot be null");
+        requireNonNull(replacement, "replacement project cannot be null");
+        if (!project.compareAndSet(current, replacement)) {
+            throw new IllegalStateException("project changed while it was 
being replaced");
+        }
+    }
+
+    public synchronized boolean addSourceRoot(@Nonnull SourceRoot sourceRoot) {
+        return sourceRoots.add(requireNonNull(sourceRoot, "sourceRoot cannot 
be null"));
+    }
+
+    public synchronized boolean removeSourceRoot(@Nonnull SourceRoot 
sourceRoot) {
+        return sourceRoots.remove(requireNonNull(sourceRoot, "sourceRoot 
cannot be null"));
+    }
+
+    public synchronized boolean removeSourceRoots(@Nonnull 
Predicate<SourceRoot> filter) {
+        return sourceRoots.removeIf(requireNonNull(filter, "filter cannot be 
null"));
+    }
+
+    public synchronized boolean replaceSourceRoot(
+            @Nonnull Predicate<SourceRoot> filter, @Nonnull SourceRoot 
replacement) {
+        requireNonNull(filter, "filter cannot be null");
+        requireNonNull(replacement, "replacement cannot be null");
+        List<SourceRoot> roots = List.copyOf(sourceRoots);
+        for (int i = 0; i < roots.size(); i++) {
+            if (filter.test(roots.get(i))) {
+                sourceRoots.clear();
+                sourceRoots.addAll(roots.subList(0, i));
+                sourceRoots.add(replacement);
+                sourceRoots.addAll(roots.subList(i + 1, roots.size()));
+                return true;
+            }
+        }
+        return false;
+    }
+
+    public synchronized void setSourceRoots(@Nonnull Collection<SourceRoot> 
sourceRoots) {
+        this.sourceRoots.clear();
+        this.sourceRoots.addAll(requireNonNull(sourceRoots, "sourceRoots 
cannot be null"));
+    }
+
+    @Nonnull
+    public Collection<SourceRoot> sourceRoots() {
+        return Collections.unmodifiableSet(sourceRoots);
+    }

Review Comment:
   ⚠️ **Concurrency gap: `sourceRoots()` is not synchronized but 
`replaceSourceRoot()` and `setSourceRoots()` are.**
   
   `replaceSourceRoot()` holds `this` as a monitor and executes 
`sourceRoots.clear()` then `sourceRoots.addAll()` — two separate write 
operations. A concurrent reader calling `sourceRoots()` between `clear()` and 
`addAll()` observes an empty collection. The `synchronized` on the mutators 
implies a stronger contract than the `CopyOnWriteArraySet` alone would provide, 
but `sourceRoots()` bypasses the lock entirely.
   
   The same gap applies to `attachedArtifacts()` (line 129) with respect to 
`setAttachedArtifacts()`.
   
   Fix: either add `synchronized` to both readers, or drop the `synchronized` 
on the multi-step mutators and use a plain `CopyOnWriteArraySet` for 
iteration-safe reads while accepting that bulk mutations are not atomic:
   
   ```suggestion
       @Nonnull
       public synchronized Collection<SourceRoot> sourceRoots() {
           return Collections.unmodifiableSet(sourceRoots);
       }
   ```



##########
impl/maven-impl/src/main/java/org/apache/maven/impl/MutableProject.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.impl;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CopyOnWriteArraySet;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Predicate;
+
+import org.apache.maven.api.Project;
+import org.apache.maven.api.SourceRoot;
+import org.apache.maven.api.annotations.Nonnull;
+import org.apache.maven.api.annotations.Nullable;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Internal stable identity for a project whose state is represented by 
immutable snapshots.
+ *
+ * @param <A> the legacy attached-artifact representation
+ */
+public final class MutableProject<A> {
+    private final AtomicReference<Project> project = new AtomicReference<>();
+    private final CopyOnWriteArraySet<SourceRoot> sourceRoots = new 
CopyOnWriteArraySet<>();
+    private final CopyOnWriteArrayList<A> attachedArtifacts = new 
CopyOnWriteArrayList<>();
+    private volatile Properties properties = new Properties();
+
+    public void initialize(@Nonnull Project project) {
+        Project value = requireNonNull(project, "project cannot be null");
+        if (!this.project.compareAndSet(null, value) && this.project.get() != 
value) {
+            throw new IllegalStateException("project has already been 
initialized");
+        }
+    }
+
+    @Nonnull
+    public Project project() {
+        return requireNonNull(project.get(), "project has not been 
initialized");
+    }
+
+    @Nullable
+    public Project projectOrNull() {
+        return project.get();
+    }
+
+    public void replace(@Nonnull Project current, @Nonnull Project 
replacement) {
+        requireNonNull(current, "current project cannot be null");
+        requireNonNull(replacement, "replacement project cannot be null");
+        if (!project.compareAndSet(current, replacement)) {
+            throw new IllegalStateException("project changed while it was 
being replaced");
+        }
+    }
+
+    public synchronized boolean addSourceRoot(@Nonnull SourceRoot sourceRoot) {
+        return sourceRoots.add(requireNonNull(sourceRoot, "sourceRoot cannot 
be null"));
+    }
+
+    public synchronized boolean removeSourceRoot(@Nonnull SourceRoot 
sourceRoot) {
+        return sourceRoots.remove(requireNonNull(sourceRoot, "sourceRoot 
cannot be null"));
+    }
+
+    public synchronized boolean removeSourceRoots(@Nonnull 
Predicate<SourceRoot> filter) {
+        return sourceRoots.removeIf(requireNonNull(filter, "filter cannot be 
null"));
+    }
+
+    public synchronized boolean replaceSourceRoot(
+            @Nonnull Predicate<SourceRoot> filter, @Nonnull SourceRoot 
replacement) {
+        requireNonNull(filter, "filter cannot be null");
+        requireNonNull(replacement, "replacement cannot be null");
+        List<SourceRoot> roots = List.copyOf(sourceRoots);
+        for (int i = 0; i < roots.size(); i++) {
+            if (filter.test(roots.get(i))) {
+                sourceRoots.clear();
+                sourceRoots.addAll(roots.subList(0, i));
+                sourceRoots.add(replacement);
+                sourceRoots.addAll(roots.subList(i + 1, roots.size()));
+                return true;
+            }
+        }
+        return false;
+    }
+
+    public synchronized void setSourceRoots(@Nonnull Collection<SourceRoot> 
sourceRoots) {
+        this.sourceRoots.clear();
+        this.sourceRoots.addAll(requireNonNull(sourceRoots, "sourceRoots 
cannot be null"));
+    }
+
+    @Nonnull
+    public Collection<SourceRoot> sourceRoots() {
+        return Collections.unmodifiableSet(sourceRoots);
+    }
+
+    public synchronized boolean addAttachedArtifact(@Nonnull A artifact) {
+        requireNonNull(artifact, "artifact cannot be null");
+        int index = attachedArtifacts.indexOf(artifact);
+        if (index >= 0) {
+            attachedArtifacts.set(index, artifact);
+            return true;
+        }
+        attachedArtifacts.add(artifact);
+        return false;
+    }
+
+    public synchronized void setAttachedArtifacts(@Nonnull Collection<? 
extends A> artifacts) {
+        attachedArtifacts.clear();
+        attachedArtifacts.addAll(requireNonNull(artifacts, "artifacts cannot 
be null"));
+    }
+
+    @Nonnull
+    public List<A> attachedArtifacts() {
+        return Collections.unmodifiableList(attachedArtifacts);

Review Comment:
   ⚠️ **Same concurrency gap as `sourceRoots()`: `attachedArtifacts()` is not 
synchronized.**
   
   `setAttachedArtifacts()` does `clear()` then `addAll()` under the monitor. A 
concurrent reader calling `attachedArtifacts()` between those two steps sees an 
empty list.
   
   ```suggestion
       @Nonnull
       public synchronized List<A> attachedArtifacts() {
           return Collections.unmodifiableList(attachedArtifacts);
       }
   ```



##########
impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultSession.java:
##########
@@ -117,9 +162,54 @@ public List<Project> getProjects(List<MavenProject> 
projects) {
 
     @Override
     public Project getProject(MavenProject project) {
-        return project != null && project.getBasedir() != null
-                ? allProjects.computeIfAbsent(project.getId(), id -> new 
DefaultProject(this, project))
-                : null;
+        if (project == null || project.getBasedir() == null) {
+            return null;
+        }
+        synchronized (projects) {
+            MutableProject<org.apache.maven.artifact.Artifact> mutableProject 
= projects.allProjects.get(project);
+            if (mutableProject == null) {
+                mutableProject = project.getMutableProject();
+                if (mutableProject.projectOrNull() == null) {
+                    
mutableProject.initialize(LegacyProjectAdapter.snapshot(this, project));
+                }
+                projects.allProjects.put(project, mutableProject);
+                projects.legacyProjects.put(mutableProject, project);
+                projects.projectStates.put(mutableProject.project(), 
mutableProject);
+            }
+            return mutableProject.project();
+        }
+    }
+
+    @Override
+    public MavenProject getMavenProject(Project project) {
+        synchronized (projects) {
+            MutableProject<org.apache.maven.artifact.Artifact> mutableProject 
= projects.projectStates.get(project);
+            return requireNonNull(
+                    projects.legacyProjects.get(mutableProject), "project is 
not associated with session");
+        }
+    }
+
+    @Override
+    public MutableProject<org.apache.maven.artifact.Artifact> 
getMutableProject(Project project) {
+        synchronized (projects) {
+            return requireNonNull(projects.projectStates.get(project), 
"project is not associated with session");
+        }
+    }
+
+    @Override
+    public Project replaceProjectModel(Project project, Model model) {
+        requireNonNull(project, "project cannot be null");
+        requireNonNull(model, "model cannot be null");
+        synchronized (projects) {
+            MutableProject<org.apache.maven.artifact.Artifact> mutableProject =
+                    requireNonNull(projects.projectStates.get(project), 
"project is not associated with session");
+            DefaultProject replacement = ((DefaultProject) 
project).withModel(model);
+            mutableProject.replace(project, replacement);
+            projects.projectStates.put(replacement, mutableProject);
+            requireNonNull(projects.legacyProjects.get(mutableProject), 
"project is not associated with session")
+                    .setModel(new org.apache.maven.model.Model(model));
+            return replacement;
+        }

Review Comment:
   💡 **`projectStates` stale entry accumulation in `replaceProjectModel`.** The 
old `project` key is left in `projectStates` after replacement (line 209 adds 
`replacement`, but there is no `projectStates.remove(project)`). This is 
intentional if callers that hold a reference to the old `DefaultProject` should 
still be able to route to the `MutableProject` via `getMutableProject(old)`. 
However, over repeated `ProjectTransformer` passes, `projectStates` accumulates 
one stale entry per replacement per project. If that is an acceptable trade-off 
(e.g. because transformer passes are few), add a brief comment explaining the 
deliberate retention; otherwise remove the old key:
   
   ```suggestion
               DefaultProject replacement = ((DefaultProject) 
project).withModel(model);
               mutableProject.replace(project, replacement);
               projects.projectStates.remove(project);  // remove stale entry
               projects.projectStates.put(replacement, mutableProject);
   ```



-- 
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