1 /*
   2  * Copyright (c) 2006, 2015, 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 /*
  25  * @test
  26  * @bug 6475157
  27  * @summary Tests deadlock in simultaneous connection and connector-server close
  28  * @author Eamonn McManus
  29  * @modules java.management.rmi
  30  */
  31 
  32 /* This test is somewhat dependent on implementation details.  If it suddenly
  33  * starts failing after a rewrite of the RMIConnectorServer code, you should
  34  * consider whether it is still relevant.
  35  */
  36 
  37 import java.io.IOException;
  38 import java.lang.management.ManagementFactory;
  39 import java.lang.management.ThreadInfo;
  40 import java.lang.management.ThreadMXBean;
  41 import java.util.concurrent.Exchanger;
  42 import javax.management.MBeanServer;
  43 import javax.management.remote.JMXServiceURL;
  44 import javax.management.remote.rmi.RMIConnection;
  45 import javax.management.remote.rmi.RMIConnectorServer;
  46 import javax.management.remote.rmi.RMIJRMPServerImpl;
  47 
  48 public class ConnectorStopDeadlockTest {
  49     private static String failure;
  50     private static RMIConnectorServer connectorServer;
  51 
  52     public static void main(String[] args) throws Exception {
  53         JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
  54         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  55         RMIJRMPServerImplSub impl = new RMIJRMPServerImplSub();
  56 
  57         System.out.println("Creating connectorServer");
  58         connectorServer = new RMIConnectorServer(url, null, impl, mbs);
  59         System.out.println("Starting connectorServer");
  60         connectorServer.start();
  61         System.out.println("Making client");
  62         RMIConnection cc = impl.newClient(null);
  63         System.out.println("Closing client");
  64         cc.close();
  65         if (connectorServer.isActive()) {
  66             System.out.println("Stopping connectorServer");
  67             connectorServer.stop();
  68         }
  69         if (failure == null)
  70             System.out.println("TEST PASSED, no deadlock");
  71         else
  72             System.out.println("TEST FAILED");
  73     }
  74 
  75     static void fail(Throwable e) {
  76         System.out.println("FAILED WITH EXCEPTION: " + e);
  77         e.printStackTrace(System.out);
  78         failure = e.toString();
  79     }
  80 
  81     static void fail(String s) {
  82         System.out.println("FAILED: " + s);
  83         failure = s;
  84     }
  85 
  86 //    static MonitorInfo[] threadLocks(Thread t) {
  87 //        ThreadMXBean tm = ManagementFactory.getThreadMXBean();
  88 //        ThreadInfo[] tis = tm.getThreadInfo(new long[] {t.getId()}, true, true);
  89 //        if (tis[0] == null)
  90 //            return null;
  91 //        else
  92 //            return tis[0].getLockedMonitors();
  93 //    }
  94 //
  95 //    static void showLocks(Thread t) {
  96 //        System.out.println("Locks for " + t.getName() + ":");
  97 //        MonitorInfo[] mis = threadLocks(t);
  98 //        if (mis == null)
  99 //            System.out.println("  (no longer exists)");
 100 //        else if (mis.length == 0)
 101 //            System.out.println("  (none)");
 102 //        else {
 103 //            for (MonitorInfo mi : mis)
 104 //                System.out.println("  " + mi);
 105 //        }
 106 //    }
 107 
 108     // Wait until thread t blocks waiting for a lock held by the calling thread,
 109     // or until it exits.
 110     static void waitForBlock(Thread t) {
 111         Thread currentThread = Thread.currentThread();
 112         System.out.println("waiting for thread " + t.getName() + " to block " +
 113                 "on a lock held by thread " + currentThread.getName());
 114         ThreadMXBean tm = ManagementFactory.getThreadMXBean();
 115         while (true) {
 116             ThreadInfo ti = tm.getThreadInfo(t.getId());
 117             if (ti == null) {
 118                 System.out.println("  thread has exited");
 119                 return;
 120             }
 121             if (ti.getLockOwnerId() == currentThread.getId()) {
 122                 System.out.println("  thread now blocked");
 123                 return;
 124             }
 125             Thread.yield();
 126         }
 127     }
 128 
 129     public static class RMIJRMPServerImplSub extends RMIJRMPServerImpl {
 130         RMIJRMPServerImplSub() throws IOException {
 131             super(0, null, null, null);
 132         }
 133 
 134         public RMIConnection makeClient() throws IOException {
 135             return super.makeClient("connection id", null);
 136         }
 137 
 138         @Override
 139         protected void clientClosed(RMIConnection conn) throws IOException {
 140             System.out.println("clientClosed, will call connectorServer.stop");
 141             final Exchanger<Void> x = new Exchanger<Void>();
 142             Thread t = new Thread() {
 143                 public void run() {
 144                     try {
 145                         connectorServer.stop();
 146                     } catch (Exception e) {
 147                         fail(e);
 148                     }
 149                 }
 150             };
 151             t.setName("connectorServer.stop");
 152             t.start();
 153             waitForBlock(t);
 154             /* If this thread is synchronized on RMIServerImpl, then
 155              * the thread that does connectorServer.stop will acquire
 156              * the clientList lock and then block waiting for the RMIServerImpl
 157              * lock.  Our call to super.clientClosed will then deadlock because
 158              * it needs to acquire the clientList lock.
 159              */
 160             System.out.println("calling super.clientClosed");
 161             System.out.flush();
 162             super.clientClosed(conn);
 163         }
 164     }
 165 }