RuntimeThreadInheritanceLeak.java revision 9330:8b1f1c2a400f
1/* 2 * Copyright (c) 2001, 2012, 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/* @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_Stub 42 * @run main/othervm RuntimeThreadInheritanceLeak 43 */ 44 45import java.lang.ref.Reference; 46import java.lang.ref.ReferenceQueue; 47import java.lang.ref.WeakReference; 48import java.net.URL; 49import java.net.URLClassLoader; 50import java.rmi.Remote; 51import java.rmi.RemoteException; 52import java.rmi.server.UnicastRemoteObject; 53import java.util.Iterator; 54import java.util.Map; 55 56public class RuntimeThreadInheritanceLeak implements Remote { 57 58 private static final int TIMEOUT = 20000; 59 60 public static void main(String[] args) { 61 62 System.err.println("\nRegression test for bug 4404702\n"); 63 64 /* 65 * HACK: Work around the fact that java.util.logging.LogManager's 66 * (singleton) construction also has this bug-- it will register a 67 * "shutdown hook", i.e. a thread, which will inherit and pin the 68 * current thread's context class loader for the lifetime of the VM-- 69 * by causing the LogManager to be initialized now, instead of by 70 * RMI when our special context class loader is set. 71 */ 72 java.util.logging.LogManager.getLogManager(); 73 74 /* 75 * HACK: Work around the fact that the non-native, thread-based 76 * SecureRandom seed generator (ThreadedSeedGenerator) seems to 77 * have this bug too (which had been causing this test to fail 78 * when run with jtreg on Windows XP-- see 4910382). 79 */ 80 (new java.security.SecureRandom()).nextInt(); 81 82 RuntimeThreadInheritanceLeak obj = new RuntimeThreadInheritanceLeak(); 83 84 try { 85 ClassLoader loader = URLClassLoader.newInstance(new URL[0]); 86 ReferenceQueue refQueue = new ReferenceQueue(); 87 Reference loaderRef = new WeakReference(loader, refQueue); 88 System.err.println("created loader: " + loader); 89 90 Thread.currentThread().setContextClassLoader(loader); 91 UnicastRemoteObject.exportObject(obj); 92 Thread.currentThread().setContextClassLoader( 93 ClassLoader.getSystemClassLoader()); 94 System.err.println( 95 "exported remote object with loader as context class loader"); 96 97 loader = null; 98 System.err.println("nulled strong reference to loader"); 99 100 UnicastRemoteObject.unexportObject(obj, true); 101 System.err.println("unexported remote object"); 102 103 /* 104 * HACK: Work around the fact that the sun.misc.GC daemon thread 105 * also has this bug-- it will have inherited our loader as its 106 * context class loader-- by giving it a chance to pass away. 107 */ 108 Thread.sleep(2000); 109 System.gc(); 110 111 System.err.println( 112 "waiting to be notified of loader being weakly reachable..."); 113 Reference dequeued = refQueue.remove(TIMEOUT); 114 if (dequeued == null) { 115 System.err.println( 116 "TEST FAILED: loader not deteced weakly reachable"); 117 dumpThreads(); 118 throw new RuntimeException( 119 "TEST FAILED: loader not detected weakly reachable"); 120 } 121 122 System.err.println( 123 "TEST PASSED: loader detected weakly reachable"); 124 dumpThreads(); 125 126 } catch (RuntimeException e) { 127 throw e; 128 } catch (Exception e) { 129 throw new RuntimeException("TEST FAILED: unexpected exception", e); 130 } finally { 131 try { 132 UnicastRemoteObject.unexportObject(obj, true); 133 } catch (RemoteException e) { 134 } 135 } 136 } 137 138 /** 139 * Dumps information about all live threads to System.err, 140 * including their context class loaders. 141 **/ 142 private static void dumpThreads() { 143 System.err.println( 144 "current live threads and their context class loaders:"); 145 Map threads = Thread.getAllStackTraces(); 146 for (Iterator iter = threads.entrySet().iterator(); iter.hasNext();) { 147 Map.Entry e = (Map.Entry) iter.next(); 148 Thread t = (Thread) e.getKey(); 149 System.err.println(" thread: " + t); 150 System.err.println(" context class loader: " + 151 t.getContextClassLoader()); 152 StackTraceElement[] trace = (StackTraceElement[]) e.getValue(); 153 for (int i = 0; i < trace.length; i++) { 154 System.err.println(" " + trace[i]); 155 } 156 } 157 } 158} 159