Skip to content

Commit 2ee2b4a

Browse files
committed
8231454: File lock in Windows on a loaded jar due to a leak in Introspector::getBeanInfo
Reviewed-by: kizune
1 parent 42a6ead commit 2ee2b4a

File tree

3 files changed

+112
-4
lines changed

3 files changed

+112
-4
lines changed

Diff for: src/java.desktop/share/classes/com/sun/beans/introspect/ClassInfo.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -22,14 +22,15 @@
2222
* or visit www.oracle.com if you need additional information or have any
2323
* questions.
2424
*/
25-
package com.sun.beans.introspect;
2625

27-
import com.sun.beans.util.Cache;
26+
package com.sun.beans.introspect;
2827

2928
import java.lang.reflect.Method;
3029
import java.util.List;
3130
import java.util.Map;
3231

32+
import com.sun.beans.util.Cache;
33+
3334
import static sun.reflect.misc.ReflectUtil.checkPackageAccess;
3435

3536
public final class ClassInfo {
@@ -54,6 +55,14 @@ public static ClassInfo get(Class<?> type) {
5455
}
5556
}
5657

58+
public static void clear() {
59+
CACHE.clear();
60+
}
61+
62+
public static void remove(Class<?> clz) {
63+
CACHE.remove(clz);
64+
}
65+
5766
private final Object mutex = new Object();
5867
private final Class<?> type;
5968
private List<Method> methods;

Diff for: src/java.desktop/share/classes/java/beans/Introspector.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -364,6 +364,7 @@ public static void setBeanInfoSearchPath(String[] path) {
364364
*/
365365
public static void flushCaches() {
366366
ThreadGroupContext.getContext().clearBeanInfoCache();
367+
ClassInfo.clear();
367368
}
368369

369370
/**
@@ -387,6 +388,7 @@ public static void flushFromCaches(Class<?> clz) {
387388
throw new NullPointerException();
388389
}
389390
ThreadGroupContext.getContext().removeBeanInfo(clz);
391+
ClassInfo.remove(clz);
390392
}
391393

392394
//======================================================================
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.beans.IntrospectionException;
25+
import java.beans.Introspector;
26+
import java.lang.ref.Reference;
27+
import java.lang.ref.WeakReference;
28+
import java.net.URL;
29+
import java.net.URLClassLoader;
30+
31+
/**
32+
* @test
33+
* @bug 8231454
34+
* @summary Tests cache cleanup by the Introspector.flushXXX()
35+
*/
36+
public final class FlushClassInfoCache {
37+
38+
public static void main(String[] args) throws Exception {
39+
verify(getLoader("testClass"));
40+
verify(getLoader("testAll"));
41+
Reference<ClassLoader> loader = getLoader("test");
42+
// Clear the cache in com.sun.beans.introspect.ClassInfo::CACHE
43+
Introspector.flushCaches();
44+
verify(loader);
45+
}
46+
47+
private static void verify(Reference<?> loader) throws Exception {
48+
int attempt = 0;
49+
while (loader.get() != null) {
50+
if (++attempt > 10) {
51+
throw new RuntimeException("Too many attempts: " + attempt);
52+
}
53+
// Cannot generate OOM here, it will clear the CACHE as well
54+
System.gc();
55+
Thread.sleep(1000);
56+
System.out.println("Not freed :(");
57+
}
58+
}
59+
60+
public static void test() {
61+
try {
62+
Introspector.getBeanInfo(FlushClassInfoCache.class);
63+
} catch (IntrospectionException e) {
64+
throw new RuntimeException(e);
65+
}
66+
}
67+
68+
public static void testClass() {
69+
try {
70+
Introspector.getBeanInfo(FlushClassInfoCache.class);
71+
// Clear the cache in com.sun.beans.introspect.ClassInfo::CACHE
72+
Introspector.flushFromCaches(FlushClassInfoCache.class);
73+
} catch (IntrospectionException e) {
74+
throw new RuntimeException(e);
75+
}
76+
}
77+
78+
public static void testAll() {
79+
try {
80+
Introspector.getBeanInfo(FlushClassInfoCache.class);
81+
// Clear the cache in com.sun.beans.introspect.ClassInfo::CACHE
82+
Introspector.flushCaches();
83+
} catch (IntrospectionException e) {
84+
throw new RuntimeException(e);
85+
}
86+
}
87+
88+
private static Reference<ClassLoader> getLoader(String m) throws Exception {
89+
URL url = FlushClassInfoCache.class.getProtectionDomain()
90+
.getCodeSource().getLocation();
91+
URLClassLoader loader = new URLClassLoader(new URL[]{url}, null);
92+
Class<?> cls = Class.forName("FlushClassInfoCache", true, loader);
93+
cls.getDeclaredMethod(m).invoke(null);
94+
loader.close();
95+
return new WeakReference<>(loader);
96+
}
97+
}

0 commit comments

Comments
 (0)