blob: a2855ffe4a779b4b2d432f7fdd101311d3c88b37 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2001-2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @bug 4404702
26 * @summary When the RMI runtime (lazily) spawns system threads that could
27 * outlive the application context in which they were (happened to be)
28 * created, such threads should not inherit (thread local) data specific to
29 * such an application context for various isolation reasons (see 4219095).
30 * While there is not yet a practical means for a general solution to this
31 * problem, the particular problem documented in 4404702-- the inheritance
32 * of the parent thread's context class loader, preventing that loader from
33 * being garbage collected in the future-- can be easily fixed. This test
34 * verifies that the context class loader in effect when the first remote
35 * object is exported (and thus when some long-lived RMI daemon threads are
36 * created) can be garbage collected after the remote object has been
37 * unexported. [Note that this test is somewhat at the mercy of other J2SE
38 * subsystems also not holding on to the loader in their daemon threads.]
39 * @author Peter Jones
40 *
41 * @build RuntimeThreadInheritanceLeak
42 * @build RuntimeThreadInheritanceLeak_Stub
43 * @run main/othervm RuntimeThreadInheritanceLeak
44 */
45
46import java.lang.ref.Reference;
47import java.lang.ref.ReferenceQueue;
48import java.lang.ref.WeakReference;
49import java.net.URL;
50import java.net.URLClassLoader;
51import java.rmi.Remote;
52import java.rmi.RemoteException;
53import java.rmi.server.UnicastRemoteObject;
54import java.util.Iterator;
55import java.util.Map;
56
57public class RuntimeThreadInheritanceLeak implements Remote {
58
59 private static final int TIMEOUT = 20000;
60
61 public static void main(String[] args) {
62
63 System.err.println("\nRegression test for bug 4404702\n");
64
65 /*
66 * HACK: Work around the fact that java.util.logging.LogManager's
67 * (singleton) construction also has this bug-- it will register a
68 * "shutdown hook", i.e. a thread, which will inherit and pin the
69 * current thread's context class loader for the lifetime of the VM--
70 * by causing the LogManager to be initialized now, instead of by
71 * RMI when our special context class loader is set.
72 */
73 java.util.logging.LogManager.getLogManager();
74
75 /*
76 * HACK: Work around the fact that the non-native, thread-based
77 * SecureRandom seed generator (ThreadedSeedGenerator) seems to
78 * have this bug too (which had been causing this test to fail
79 * when run with jtreg on Windows XP-- see 4910382).
80 */
81 (new java.security.SecureRandom()).nextInt();
82
83 RuntimeThreadInheritanceLeak obj = new RuntimeThreadInheritanceLeak();
84
85 try {
86 ClassLoader loader = URLClassLoader.newInstance(new URL[0]);
87 ReferenceQueue refQueue = new ReferenceQueue();
88 Reference loaderRef = new WeakReference(loader, refQueue);
89 System.err.println("created loader: " + loader);
90
91 Thread.currentThread().setContextClassLoader(loader);
92 UnicastRemoteObject.exportObject(obj);
93 Thread.currentThread().setContextClassLoader(
94 ClassLoader.getSystemClassLoader());
95 System.err.println(
96 "exported remote object with loader as context class loader");
97
98 loader = null;
99 System.err.println("nulled strong reference to loader");
100
101 UnicastRemoteObject.unexportObject(obj, true);
102 System.err.println("unexported remote object");
103
104 /*
105 * HACK: Work around the fact that the sun.misc.GC daemon thread
106 * also has this bug-- it will have inherited our loader as its
107 * context class loader-- by giving it a chance to pass away.
108 */
109 Thread.sleep(2000);
110 System.gc();
111
112 System.err.println(
113 "waiting to be notified of loader being weakly reachable...");
114 Reference dequeued = refQueue.remove(TIMEOUT);
115 if (dequeued == null) {
116 System.err.println(
117 "TEST FAILED: loader not deteced weakly reachable");
118 dumpThreads();
119 throw new RuntimeException(
120 "TEST FAILED: loader not detected weakly reachable");
121 }
122
123 System.err.println(
124 "TEST PASSED: loader detected weakly reachable");
125 dumpThreads();
126
127 } catch (RuntimeException e) {
128 throw e;
129 } catch (Exception e) {
130 throw new RuntimeException("TEST FAILED: unexpected exception", e);
131 } finally {
132 try {
133 UnicastRemoteObject.unexportObject(obj, true);
134 } catch (RemoteException e) {
135 }
136 }
137 }
138
139 /**
140 * Dumps information about all live threads to System.err,
141 * including their context class loaders.
142 **/
143 private static void dumpThreads() {
144 System.err.println(
145 "current live threads and their context class loaders:");
146 Map threads = Thread.getAllStackTraces();
147 for (Iterator iter = threads.entrySet().iterator(); iter.hasNext();) {
148 Map.Entry e = (Map.Entry) iter.next();
149 Thread t = (Thread) e.getKey();
150 System.err.println(" thread: " + t);
151 System.err.println(" context class loader: " +
152 t.getContextClassLoader());
153 StackTraceElement[] trace = (StackTraceElement[]) e.getValue();
154 for (int i = 0; i < trace.length; i++) {
155 System.err.println(" " + trace[i]);
156 }
157 }
158 }
159}