You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2021/03/13 09:46:20 UTC

[maven] 02/02: Deadlock when reading pom

This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a commit to branch MNG-7111
in repository https://gitbox.apache.org/repos/asf/maven.git

commit bb39b723ce97b78e50157eeea4be6645831153bc
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Mon Mar 8 09:49:42 2021 +0100

    Deadlock when reading pom
---
 .../maven/model/building/DefaultModelBuilder.java  |  7 ++-
 .../model/building/DefaultTransformerContext.java  | 64 ++++++++++++++++++++--
 2 files changed, 65 insertions(+), 6 deletions(-)

diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
index 845480c..51d2b06 100644
--- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
+++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
@@ -1571,13 +1571,16 @@ public class DefaultModelBuilder
                 public Model getRawModel( String gId, String aId )
                 {
                     return context.modelByGA.computeIfAbsent( new DefaultTransformerContext.GAKey( gId, aId ),
-                                                              k -> findRawModel( gId, aId ) );
+                                                              k -> new DefaultTransformerContext.Holder() )
+                            .computeIfAbsent( () -> findRawModel( gId, aId ) );
                 }
 
                 @Override
                 public Model getRawModel( Path path )
                 {
-                    return context.modelByPath.computeIfAbsent( path, k -> findRawModel( path ) );
+                    return context.modelByPath.computeIfAbsent( path,
+                                                                k -> new DefaultTransformerContext.Holder() )
+                            .computeIfAbsent( () -> findRawModel( path ) );
                 }
 
                 private Model findRawModel( String groupId, String artifactId )
diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultTransformerContext.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultTransformerContext.java
index 9c7da72..3c70b3c 100644
--- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultTransformerContext.java
+++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultTransformerContext.java
@@ -23,6 +23,7 @@ import java.nio.file.Path;
 import java.util.Map;
 import java.util.Objects;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Supplier;
 
 import org.apache.maven.model.Model;
 
@@ -35,9 +36,64 @@ class DefaultTransformerContext implements TransformerContext
 {
     final Map<String, String> userProperties = new ConcurrentHashMap<>();
 
-    final Map<Path, Model> modelByPath = new ConcurrentHashMap<>();
+    final Map<Path, Holder> modelByPath = new ConcurrentHashMap<>();
 
-    final Map<GAKey, Model> modelByGA = new ConcurrentHashMap<>();
+    final Map<GAKey, Holder> modelByGA = new ConcurrentHashMap<>();
+
+    public static class Holder
+    {
+        private volatile boolean set;
+        private volatile Model model;
+
+        Holder()
+        {
+        }
+
+        public static Model deref( Holder holder )
+        {
+            return holder != null ? holder.get() : null;
+        }
+
+        public Model get()
+        {
+            if ( !set )
+            {
+                synchronized ( this )
+                {
+                    if ( !set )
+                    {
+                        try
+                        {
+                            this.wait();
+                        }
+                        catch ( InterruptedException e )
+                        {
+                            // Ignore
+                        }
+                    }
+                }
+            }
+            return model;
+        }
+
+        public Model computeIfAbsent( Supplier<Model> supplier )
+        {
+            if ( !set )
+            {
+                synchronized ( this )
+                {
+                    if ( !set )
+                    {
+                        this.set = true;
+                        this.model = supplier.get();
+                        this.notifyAll();
+                    }
+                }
+            }
+            return model;
+        }
+
+    }
 
     @Override
     public String getUserProperty( String key )
@@ -48,13 +104,13 @@ class DefaultTransformerContext implements TransformerContext
     @Override
     public Model getRawModel( Path p )
     {
-        return modelByPath.get( p );
+        return Holder.deref( modelByPath.get( p ) );
     }
 
     @Override
     public Model getRawModel( String groupId, String artifactId )
     {
-        return modelByGA.get( new GAKey( groupId, artifactId ) );
+        return Holder.deref( modelByGA.get( new GAKey( groupId, artifactId ) ) );
     }
 
     static class GAKey