169N/A/*
2362N/A * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4404702
0N/A * @summary When the RMI runtime (lazily) spawns system threads that could
0N/A * outlive the application context in which they were (happened to be)
0N/A * created, such threads should not inherit (thread local) data specific to
0N/A * such an application context for various isolation reasons (see 4219095).
0N/A * While there is not yet a practical means for a general solution to this
0N/A * problem, the particular problem documented in 4404702-- the inheritance
0N/A * of the parent thread's context class loader, preventing that loader from
0N/A * being garbage collected in the future-- can be easily fixed. This test
0N/A * verifies that the context class loader in effect when the first remote
0N/A * object is exported (and thus when some long-lived RMI daemon threads are
0N/A * created) can be garbage collected after the remote object has been
0N/A * unexported. [Note that this test is somewhat at the mercy of other J2SE
0N/A * subsystems also not holding on to the loader in their daemon threads.]
0N/A * @author Peter Jones
0N/A *
0N/A * @build RuntimeThreadInheritanceLeak_Stub
0N/A * @run main/othervm RuntimeThreadInheritanceLeak
0N/A */
0N/A
0N/Aimport java.lang.ref.Reference;
0N/Aimport java.lang.ref.ReferenceQueue;
0N/Aimport java.lang.ref.WeakReference;
0N/Aimport java.net.URL;
0N/Aimport java.net.URLClassLoader;
0N/Aimport java.rmi.Remote;
0N/Aimport java.rmi.RemoteException;
0N/Aimport java.rmi.server.UnicastRemoteObject;
0N/Aimport java.util.Iterator;
0N/Aimport java.util.Map;
0N/A
0N/Apublic class RuntimeThreadInheritanceLeak implements Remote {
0N/A
0N/A private static final int TIMEOUT = 20000;
0N/A
0N/A public static void main(String[] args) {
0N/A
169N/A System.err.println("\nRegression test for bug 4404702\n");
0N/A
169N/A /*
169N/A * HACK: Work around the fact that java.util.logging.LogManager's
169N/A * (singleton) construction also has this bug-- it will register a
169N/A * "shutdown hook", i.e. a thread, which will inherit and pin the
169N/A * current thread's context class loader for the lifetime of the VM--
169N/A * by causing the LogManager to be initialized now, instead of by
169N/A * RMI when our special context class loader is set.
169N/A */
169N/A java.util.logging.LogManager.getLogManager();
0N/A
169N/A /*
169N/A * HACK: Work around the fact that the non-native, thread-based
169N/A * SecureRandom seed generator (ThreadedSeedGenerator) seems to
169N/A * have this bug too (which had been causing this test to fail
169N/A * when run with jtreg on Windows XP-- see 4910382).
169N/A */
169N/A (new java.security.SecureRandom()).nextInt();
0N/A
169N/A RuntimeThreadInheritanceLeak obj = new RuntimeThreadInheritanceLeak();
0N/A
169N/A try {
169N/A ClassLoader loader = URLClassLoader.newInstance(new URL[0]);
169N/A ReferenceQueue refQueue = new ReferenceQueue();
169N/A Reference loaderRef = new WeakReference(loader, refQueue);
169N/A System.err.println("created loader: " + loader);
0N/A
169N/A Thread.currentThread().setContextClassLoader(loader);
169N/A UnicastRemoteObject.exportObject(obj);
169N/A Thread.currentThread().setContextClassLoader(
169N/A ClassLoader.getSystemClassLoader());
169N/A System.err.println(
169N/A "exported remote object with loader as context class loader");
0N/A
169N/A loader = null;
169N/A System.err.println("nulled strong reference to loader");
0N/A
169N/A UnicastRemoteObject.unexportObject(obj, true);
169N/A System.err.println("unexported remote object");
0N/A
169N/A /*
169N/A * HACK: Work around the fact that the sun.misc.GC daemon thread
169N/A * also has this bug-- it will have inherited our loader as its
169N/A * context class loader-- by giving it a chance to pass away.
169N/A */
169N/A Thread.sleep(2000);
169N/A System.gc();
0N/A
169N/A System.err.println(
169N/A "waiting to be notified of loader being weakly reachable...");
169N/A Reference dequeued = refQueue.remove(TIMEOUT);
169N/A if (dequeued == null) {
169N/A System.err.println(
169N/A "TEST FAILED: loader not deteced weakly reachable");
169N/A dumpThreads();
169N/A throw new RuntimeException(
169N/A "TEST FAILED: loader not detected weakly reachable");
169N/A }
0N/A
169N/A System.err.println(
169N/A "TEST PASSED: loader detected weakly reachable");
169N/A dumpThreads();
0N/A
169N/A } catch (RuntimeException e) {
169N/A throw e;
169N/A } catch (Exception e) {
169N/A throw new RuntimeException("TEST FAILED: unexpected exception", e);
169N/A } finally {
169N/A try {
169N/A UnicastRemoteObject.unexportObject(obj, true);
169N/A } catch (RemoteException e) {
169N/A }
169N/A }
0N/A }
0N/A
0N/A /**
0N/A * Dumps information about all live threads to System.err,
0N/A * including their context class loaders.
0N/A **/
0N/A private static void dumpThreads() {
169N/A System.err.println(
169N/A "current live threads and their context class loaders:");
169N/A Map threads = Thread.getAllStackTraces();
169N/A for (Iterator iter = threads.entrySet().iterator(); iter.hasNext();) {
169N/A Map.Entry e = (Map.Entry) iter.next();
169N/A Thread t = (Thread) e.getKey();
169N/A System.err.println(" thread: " + t);
169N/A System.err.println(" context class loader: " +
169N/A t.getContextClassLoader());
169N/A StackTraceElement[] trace = (StackTraceElement[]) e.getValue();
169N/A for (int i = 0; i < trace.length; i++) {
169N/A System.err.println(" " + trace[i]);
169N/A }
169N/A }
0N/A }
0N/A}