1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
/*
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.IOException;
import java.io.Serializable;
import java.net.Socket;
import java.rmi.server.RMIClientSocketFactory;
import java.util.HashMap;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
import javax.management.remote.rmi.RMIConnectorServer;
/*
* @test
* @bug 6697180
* @summary test on a client notification deadlock.
* @author Shanliang JIANG
*
* @run clean MultiThreadDeadLockTest
* @run build MultiThreadDeadLockTest
* @run main MultiThreadDeadLockTest
*/
public class MultiThreadDeadLockTest {
private static long serverTimeout = 500L;
public static void main(String[] args) throws Exception {
print("Create the MBean server");
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
print("Initialize environment map");
HashMap env = new HashMap();
print("Specify a client socket factory to control socket creation.");
env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE,
clientFactory);
print("Specify a server idle timeout to make a server close an idle connection.");
env.put("jmx.remote.x.server.connection.timeout", serverTimeout);
print("Disable client heartbeat.");
env.put("jmx.remote.x.client.connection.check.period", 0);
env.put("jmx.remote.x.notification.fetch.timeout", serverTimeout);
print("Create an RMI server");
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer server =
JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
server.start();
url = server.getAddress();
print("Create jmx client on "+url);
StateMachine.setState(CREATE_SOCKET); // allow to create client socket
client = JMXConnectorFactory.connect(url, env);
Thread.sleep(100);
totoName = new ObjectName("default:name=toto");
mbs.registerMBean(toto, totoName);
print("Register the mbean: " + totoName);
print("Add listener to toto MBean");
client.getMBeanServerConnection().addNotificationListener(
totoName, myListener, null, null);
Thread.sleep(10);
print("send notif, listener will block the fetcher");
toto.sendNotif();
Thread.sleep(100);
StateMachine.setState(NO_OP);
print("Sleep 3 times of server idle timeout: "+serverTimeout+
", the sever should close the idle connection.");
Thread.sleep(serverTimeout*3);
print("start the user thread to call mbean method, it will get IOexception" +
" and start the reconnection, the socket factory will block the" +
" socket creation.");
UserThread ut = new UserThread();
ut.start();
Thread.sleep(10);
print("Free the listener, the fetcher will get IO and makes " +
"a deadlock if the bug is not fixed.");
StateMachine.setState(FREE_LISTENER);
Thread.sleep(100);
print("Allow to create new socket for the reconnection");
StateMachine.setState(CREATE_SOCKET);
print("Check whether the user thread gets free to call the mbean.");
if (!ut.waitDone(5000)) {
throw new RuntimeException("Possible deadlock!");
}
print("Remove the listener.");
client.getMBeanServerConnection().removeNotificationListener(
totoName, myListener, null, null);
Thread.sleep(serverTimeout*3);
print("\nWell passed, bye!");
client.close();
Thread.sleep(10);
server.stop();
}
private static ObjectName totoName = null;
private static JMXConnector client;
public static class UserThread extends Thread {
public UserThread() {
setDaemon(true);
}
public void run() {
try {
client.getMBeanServerConnection().invoke(
totoName, "allowReturn", null, null);
} catch (Exception e) {
throw new Error(e);
}
synchronized(UserThread.class) {
done = true;
UserThread.class.notify();
}
}
public boolean waitDone(long timeout) {
synchronized(UserThread.class) {
if(!done) {
try {
UserThread.class.wait(timeout);
} catch (Exception e) {
throw new Error(e);
}
}
}
return done;
}
private boolean done = false;
}
public static interface TotoMBean {
public void allowReturn();
}
public static class Toto extends NotificationBroadcasterSupport
implements TotoMBean {
public void allowReturn() {
enter("allowReturn");
leave("allowReturn");
}
public void sendNotif() {
enter("sendNotif");
sendNotification(new Notification("Toto", totoName, 0));
leave("sendNotif");
}
}
private static Toto toto = new Toto();
public static NotificationListener myListener = new NotificationListener() {
public void handleNotification(Notification notification, Object handback) {
enter("handleNotification");
StateMachine.waitState(FREE_LISTENER);
leave("handleNotification");
}
};
public static class RMIClientFactory
implements RMIClientSocketFactory, Serializable {
public Socket createSocket(String host, int port) throws IOException {
enter("createSocket");
//print("Calling createSocket(" + host + " " + port + ")");
StateMachine.waitState(CREATE_SOCKET);
Socket s = new Socket(host, port);
leave("createSocket");
return s;
}
}
private static RMIClientFactory clientFactory = new RMIClientFactory();
private static int CREATE_SOCKET = 1;
private static int FREE_LISTENER = 3;
private static int NO_OP = 0;
public static class StateMachine {
private static int state = NO_OP;
private static int[] lock = new int[0];
public static void waitState(int s) {
synchronized (lock) {
while (state != s) {
try {
lock.wait();
} catch (InterruptedException ire) {
// should not
throw new Error(ire);
}
}
}
}
public static int getState() {
synchronized (lock) {
return state;
}
}
public static void setState(int s) {
synchronized (lock) {
state = s;
lock.notifyAll();
}
}
}
private static void print(String m) {
System.out.println(m);
}
private static void enter(String m) {
System.out.println("\n---Enter the method " + m);
}
private static void leave(String m) {
System.out.println("===Leave the method: " + m);
}
}
|