Browse Source

bz-65424 Prevent potential deadlocks in IntrospectionHelper

master
Jaikiran Pai 3 years ago
parent
commit
0089151938
2 changed files with 26 additions and 11 deletions
  1. +3
    -0
      WHATSNEW
  2. +23
    -11
      src/main/org/apache/tools/ant/IntrospectionHelper.java

+ 3
- 0
WHATSNEW View File

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



+ 23
- 11
src/main/org/apache/tools/ant/IntrospectionHelper.java View File

@@ -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();
}



Loading…
Cancel
Save