You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by ja...@apache.org on 2021/09/26 04:39:22 UTC

[ant] branch master updated: bz-65424 Prevent potential deadlocks in IntrospectionHelper

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

jaikiran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ant.git


The following commit(s) were added to refs/heads/master by this push:
     new 0089151  bz-65424 Prevent potential deadlocks in IntrospectionHelper
0089151 is described below

commit 0089151938cf5ece0769d99e84d1299bbca75f24
Author: Jaikiran Pai <ja...@apache.org>
AuthorDate: Sun Sep 26 10:09:04 2021 +0530

    bz-65424 Prevent potential deadlocks in IntrospectionHelper
---
 WHATSNEW                                           |  3 ++
 .../org/apache/tools/ant/IntrospectionHelper.java  | 34 +++++++++++++++-------
 2 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/WHATSNEW b/WHATSNEW
index 5898c65..7239a33 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -13,6 +13,9 @@ Fixed bugs:
    the shorter alias names.
    Bugzilla Report 65539
 
+ * Prevent potential deadlocks in org.apache.tools.ant.IntrospectionHelper.
+   Bugzilla Report 65424
+
 Other changes:
 --------------
 
diff --git a/src/main/org/apache/tools/ant/IntrospectionHelper.java b/src/main/org/apache/tools/ant/IntrospectionHelper.java
index d4bf771..ff32f95 100644
--- a/src/main/org/apache/tools/ant/IntrospectionHelper.java
+++ b/src/main/org/apache/tools/ant/IntrospectionHelper.java
@@ -320,7 +320,7 @@ public final class IntrospectionHelper {
      *
      * @return a helper for the specified class
      */
-    public static synchronized IntrospectionHelper getHelper(final Class<?> c) {
+    public static IntrospectionHelper getHelper(final Class<?> c) {
         return getHelper(null, c);
     }
 
@@ -337,19 +337,31 @@ public final class IntrospectionHelper {
      *
      * @return a helper for the specified class
      */
-    public static synchronized IntrospectionHelper getHelper(final Project p, final Class<?> c) {
+    public static IntrospectionHelper getHelper(final Project p, final Class<?> c) {
+        if (p == null) {
+            // #30162: do *not* use cache if there is no project, as we
+            // cannot guarantee that the cache will be cleared.
+            return new IntrospectionHelper(c);
+        }
         IntrospectionHelper ih = HELPERS.get(c.getName());
+        if (ih != null && ih.bean == c) {
+            return ih;
+        }
         // If a helper cannot be found, or if the helper is for another
-        // classloader, create a new IH
-        if (ih == null || ih.bean != c) {
-            ih = new IntrospectionHelper(c);
-            if (p != null) {
-                // #30162: do *not* cache this if there is no project, as we
-                // cannot guarantee that the cache will be cleared.
-                HELPERS.put(c.getName(), ih);
+        // classloader, create a new IH and cache it
+        // Note: This new instance of IntrospectionHelper is intentionally
+        // created without holding a lock, to prevent potential deadlocks.
+        // See bz-65424 for details
+        ih = new IntrospectionHelper(c);
+        synchronized (HELPERS) {
+            IntrospectionHelper cached = HELPERS.get(c.getName());
+            if (cached != null && cached.bean == c) {
+                return cached;
             }
+            // cache the recently created one
+            HELPERS.put(c.getName(), ih);
+            return ih;
         }
-        return ih;
     }
 
     /**
@@ -1499,7 +1511,7 @@ public final class IntrospectionHelper {
     /**
      * Clears the static cache of on build finished.
      */
-    public static synchronized void clearCache() {
+    public static void clearCache() {
         HELPERS.clear();
     }