1/*
2 * Copyright (c) 2003, 2017, 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 4757273
27 * @summary Test deadlock in MBeanServerDelegate listeners
28 * @author Eamonn McManus
29 *
30 * @run clean NotifDeadlockTest
31 * @run build NotifDeadlockTest
32 * @run main NotifDeadlockTest
33 */
34
35/*
36 * Test deadlock when a listener for an MBeanServerDelegate does a
37 * register or unregister of an MBean.  Since such a listener is
38 * triggered by a register or unregister operation, deadlock scenarios
39 * are possible if there are any locks held while the listener is
40 * being dispatched.
41 *
42 * The flow of control looks rather like this:
43 *
44 * Thread 1:
45 * - MBeanServer.createMBean(..., objectName1);
46 * --- MBeanServerDelegate.sendNotification
47 * ----- XListener.handleNotification
48 * ------- create Thread 2
49 * ------- wait for Thread 2 to complete
50 *
51 * Thread 2:
52 * - MBeanServer.createMBean(..., objectName2);
53 * - end Thread 2
54 *
55 * If any locks are held by Thread 1 within createMBean or
56 * sendNotification, then Thread 2 can block waiting for them.
57 * Since Thread 1 is itself waiting for Thread 2, this is a deadlock.
58 *
59 * We test all four combinations of:
60 * (Thread1-create,Thread1-unregister) x (Thread2-create,Thread2-unregister)
61 *
62 * In the JMX 1.1 RI, all four tests fail.  In the JMX 1.2 RI, all four
63 * tests should pass.
64 */
65import javax.management.*;
66
67public class NotifDeadlockTest {
68    static ObjectName on1, on2, delName;
69    static {
70        try {
71            on1 = new ObjectName("thing:a=b");
72            on2 = new ObjectName("thing:c=d");
73            delName =
74                new ObjectName("JMImplementation:type=MBeanServerDelegate");
75        } catch (MalformedObjectNameException e) {
76            throw new Error();
77        }
78    }
79    static MBeanServer mbs;
80
81    /* This listener registers or unregisters the MBean called on2
82       when triggered.  */
83    private static class XListener implements NotificationListener {
84        private boolean firstTime = true;
85        private final boolean register;
86
87        XListener(boolean register) {
88            this.register = register;
89        }
90
91        public void handleNotification(Notification not, Object handback) {
92            if (firstTime) {
93                firstTime = false;
94                Thread t = new Thread() {
95                    public void run() {
96                        try {
97                            if (register) {
98                                mbs.createMBean("javax.management.timer.Timer",
99                                                on2);
100                                System.out.println("Listener created " + on2);
101                            } else {
102                                mbs.unregisterMBean(on2);
103                                System.out.println("Listener removed " + on2);
104                            }
105                        } catch (Exception e) {
106                            e.printStackTrace();
107                        }
108                    }
109                };
110                t.start();
111                try {
112                    t.join();
113                } catch (InterruptedException e) {
114                    e.printStackTrace(); // should not happen
115                }
116            }
117        }
118    }
119
120    public static void main(String[] args) throws Exception {
121
122        System.out.println("Test 1: in register notif, unregister an MBean");
123        mbs = MBeanServerFactory.createMBeanServer();
124        mbs.createMBean("javax.management.timer.Timer", on2);
125        mbs.addNotificationListener(delName, new XListener(false), null, null);
126        mbs.createMBean("javax.management.timer.Timer", on1);
127        MBeanServerFactory.releaseMBeanServer(mbs);
128        System.out.println("Test 1 completed");
129
130        System.out.println("Test 2: in unregister notif, unregister an MBean");
131        mbs = MBeanServerFactory.createMBeanServer();
132        mbs.createMBean("javax.management.timer.Timer", on1);
133        mbs.createMBean("javax.management.timer.Timer", on2);
134        mbs.addNotificationListener(delName, new XListener(false), null, null);
135        mbs.unregisterMBean(on1);
136        MBeanServerFactory.releaseMBeanServer(mbs);
137        System.out.println("Test 2 completed");
138
139        System.out.println("Test 3: in register notif, register an MBean");
140        mbs = MBeanServerFactory.createMBeanServer();
141        mbs.addNotificationListener(delName, new XListener(true), null, null);
142        mbs.createMBean("javax.management.timer.Timer", on1);
143        MBeanServerFactory.releaseMBeanServer(mbs);
144        System.out.println("Test 3 completed");
145
146        System.out.println("Test 4: in unregister notif, register an MBean");
147        mbs = MBeanServerFactory.createMBeanServer();
148        mbs.createMBean("javax.management.timer.Timer", on1);
149        mbs.addNotificationListener(delName, new XListener(true), null, null);
150        mbs.unregisterMBean(on1);
151        MBeanServerFactory.releaseMBeanServer(mbs);
152        System.out.println("Test 4 completed");
153
154        System.out.println("Test passed");
155    }
156}
157