Path: blob/master/test/jdk/javax/management/MBeanServer/NotifDeadlockTest.java
41149 views
/*1* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 475727326* @summary Test deadlock in MBeanServerDelegate listeners27* @author Eamonn McManus28*29* @run clean NotifDeadlockTest30* @run build NotifDeadlockTest31* @run main NotifDeadlockTest32*/3334/*35* Test deadlock when a listener for an MBeanServerDelegate does a36* register or unregister of an MBean. Since such a listener is37* triggered by a register or unregister operation, deadlock scenarios38* are possible if there are any locks held while the listener is39* being dispatched.40*41* The flow of control looks rather like this:42*43* Thread 1:44* - MBeanServer.createMBean(..., objectName1);45* --- MBeanServerDelegate.sendNotification46* ----- XListener.handleNotification47* ------- create Thread 248* ------- wait for Thread 2 to complete49*50* Thread 2:51* - MBeanServer.createMBean(..., objectName2);52* - end Thread 253*54* If any locks are held by Thread 1 within createMBean or55* sendNotification, then Thread 2 can block waiting for them.56* Since Thread 1 is itself waiting for Thread 2, this is a deadlock.57*58* We test all four combinations of:59* (Thread1-create,Thread1-unregister) x (Thread2-create,Thread2-unregister)60*61* In the JMX 1.1 RI, all four tests fail. In the JMX 1.2 RI, all four62* tests should pass.63*/64import javax.management.*;6566public class NotifDeadlockTest {67static ObjectName on1, on2, delName;68static {69try {70on1 = new ObjectName("thing:a=b");71on2 = new ObjectName("thing:c=d");72delName =73new ObjectName("JMImplementation:type=MBeanServerDelegate");74} catch (MalformedObjectNameException e) {75throw new Error();76}77}78static MBeanServer mbs;7980/* This listener registers or unregisters the MBean called on281when triggered. */82private static class XListener implements NotificationListener {83private boolean firstTime = true;84private final boolean register;8586XListener(boolean register) {87this.register = register;88}8990public void handleNotification(Notification not, Object handback) {91if (firstTime) {92firstTime = false;93Thread t = new Thread() {94public void run() {95try {96if (register) {97mbs.createMBean("javax.management.timer.Timer",98on2);99System.out.println("Listener created " + on2);100} else {101mbs.unregisterMBean(on2);102System.out.println("Listener removed " + on2);103}104} catch (Exception e) {105e.printStackTrace();106}107}108};109t.start();110try {111t.join();112} catch (InterruptedException e) {113e.printStackTrace(); // should not happen114}115}116}117}118119public static void main(String[] args) throws Exception {120121System.out.println("Test 1: in register notif, unregister an MBean");122mbs = MBeanServerFactory.createMBeanServer();123mbs.createMBean("javax.management.timer.Timer", on2);124mbs.addNotificationListener(delName, new XListener(false), null, null);125mbs.createMBean("javax.management.timer.Timer", on1);126MBeanServerFactory.releaseMBeanServer(mbs);127System.out.println("Test 1 completed");128129System.out.println("Test 2: in unregister notif, unregister an MBean");130mbs = MBeanServerFactory.createMBeanServer();131mbs.createMBean("javax.management.timer.Timer", on1);132mbs.createMBean("javax.management.timer.Timer", on2);133mbs.addNotificationListener(delName, new XListener(false), null, null);134mbs.unregisterMBean(on1);135MBeanServerFactory.releaseMBeanServer(mbs);136System.out.println("Test 2 completed");137138System.out.println("Test 3: in register notif, register an MBean");139mbs = MBeanServerFactory.createMBeanServer();140mbs.addNotificationListener(delName, new XListener(true), null, null);141mbs.createMBean("javax.management.timer.Timer", on1);142MBeanServerFactory.releaseMBeanServer(mbs);143System.out.println("Test 3 completed");144145System.out.println("Test 4: in unregister notif, register an MBean");146mbs = MBeanServerFactory.createMBeanServer();147mbs.createMBean("javax.management.timer.Timer", on1);148mbs.addNotificationListener(delName, new XListener(true), null, null);149mbs.unregisterMBean(on1);150MBeanServerFactory.releaseMBeanServer(mbs);151System.out.println("Test 4 completed");152153System.out.println("Test passed");154}155}156157158