Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/MBeanServer/NotifDeadlockTest.java
38839 views
/*1* Copyright (c) 2003, 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* @run clean NotifDeadlockTest29* @run build NotifDeadlockTest30* @run main NotifDeadlockTest31*/3233/*34* Test deadlock when a listener for an MBeanServerDelegate does a35* register or unregister of an MBean. Since such a listener is36* triggered by a register or unregister operation, deadlock scenarios37* are possible if there are any locks held while the listener is38* being dispatched.39*40* The flow of control looks rather like this:41*42* Thread 1:43* - MBeanServer.createMBean(..., objectName1);44* --- MBeanServerDelegate.sendNotification45* ----- XListener.handleNotification46* ------- create Thread 247* ------- wait for Thread 2 to complete48*49* Thread 2:50* - MBeanServer.createMBean(..., objectName2);51* - end Thread 252*53* If any locks are held by Thread 1 within createMBean or54* sendNotification, then Thread 2 can block waiting for them.55* Since Thread 1 is itself waiting for Thread 2, this is a deadlock.56*57* We test all four combinations of:58* (Thread1-create,Thread1-unregister) x (Thread2-create,Thread2-unregister)59*60* In the JMX 1.1 RI, all four tests fail. In the JMX 1.2 RI, all four61* tests should pass.62*/63import javax.management.*;6465public class NotifDeadlockTest {66static ObjectName on1, on2, delName;67static {68try {69on1 = new ObjectName("thing:a=b");70on2 = new ObjectName("thing:c=d");71delName =72new ObjectName("JMImplementation:type=MBeanServerDelegate");73} catch (MalformedObjectNameException e) {74throw new Error();75}76}77static MBeanServer mbs;78static boolean timedOut;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(2000);112} catch (InterruptedException e) {113e.printStackTrace(); // should not happen114}115if (t.isAlive()) {116System.out.println("FAILURE: Wait timed out: " +117"probable deadlock");118timedOut = true;119}120}121}122}123124public static void main(String[] args) throws Exception {125boolean success = true;126127System.out.println("Test 1: in register notif, unregister an MBean");128timedOut = false;129mbs = MBeanServerFactory.createMBeanServer();130mbs.createMBean("javax.management.timer.Timer", on2);131mbs.addNotificationListener(delName, new XListener(false), null, null);132mbs.createMBean("javax.management.timer.Timer", on1);133MBeanServerFactory.releaseMBeanServer(mbs);134if (timedOut) {135success = false;136Thread.sleep(500);137// wait for the spawned thread to complete its work, probably138}139System.out.println("Test 1 completed");140141System.out.println("Test 2: in unregister notif, unregister an MBean");142timedOut = false;143mbs = MBeanServerFactory.createMBeanServer();144mbs.createMBean("javax.management.timer.Timer", on1);145mbs.createMBean("javax.management.timer.Timer", on2);146mbs.addNotificationListener(delName, new XListener(false), null, null);147mbs.unregisterMBean(on1);148MBeanServerFactory.releaseMBeanServer(mbs);149if (timedOut) {150success = false;151Thread.sleep(500);152// wait for the spawned thread to complete its work, probably153}154System.out.println("Test 2 completed");155156System.out.println("Test 3: in register notif, register an MBean");157timedOut = false;158mbs = MBeanServerFactory.createMBeanServer();159mbs.addNotificationListener(delName, new XListener(true), null, null);160mbs.createMBean("javax.management.timer.Timer", on1);161MBeanServerFactory.releaseMBeanServer(mbs);162if (timedOut) {163success = false;164Thread.sleep(500);165// wait for the spawned thread to complete its work, probably166}167System.out.println("Test 3 completed");168169System.out.println("Test 4: in unregister notif, register an MBean");170timedOut = false;171mbs = MBeanServerFactory.createMBeanServer();172mbs.createMBean("javax.management.timer.Timer", on1);173mbs.addNotificationListener(delName, new XListener(true), null, null);174mbs.unregisterMBean(on1);175MBeanServerFactory.releaseMBeanServer(mbs);176if (timedOut) {177success = false;178Thread.sleep(500);179// wait for the spawned thread to complete its work, probably180}181System.out.println("Test 4 completed");182183if (success)184System.out.println("Test passed");185else {186System.out.println("TEST FAILED: at least one subcase failed");187System.exit(1);188}189}190}191192193