|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 8352088 |
| 27 | + * @summary If ThreadGroupReference.groups() is called very early on from an |
| 28 | + * event handler, it can cause a deadlock because the call can result |
| 29 | + * in ClassPrepareEvents, which the debug agent will block on until |
| 30 | + * the debugger handles them, which it won't because the event handler |
| 31 | + * thread is waiting for a reply to ThreadGroupReference.groups(). |
| 32 | + * |
| 33 | + * @run build TestScaffold VMConnection TargetListener TargetAdapter |
| 34 | + * @run compile -g EarlyThreadGroupChildrenTest.java |
| 35 | + * @run main/othervm/timeout=20 EarlyThreadGroupChildrenTest |
| 36 | + */ |
| 37 | + |
| 38 | +import com.sun.jdi.*; |
| 39 | +import com.sun.jdi.event.*; |
| 40 | +import com.sun.jdi.request.*; |
| 41 | + |
| 42 | +import java.util.*; |
| 43 | + |
| 44 | +class EarlyThreadGroupChildrenTestTarg { |
| 45 | + public static void main(String[] args) throws InterruptedException { |
| 46 | + System.out.println("Start"); |
| 47 | + System.out.println("Finish"); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | + /********** test program **********/ |
| 52 | + |
| 53 | +public class EarlyThreadGroupChildrenTest extends TestScaffold { |
| 54 | + EarlyThreadGroupChildrenTest(String args[]) { |
| 55 | + super(args); |
| 56 | + } |
| 57 | + |
| 58 | + public static void main(String[] args) throws Exception { |
| 59 | + new EarlyThreadGroupChildrenTest(args).startTests(); |
| 60 | + } |
| 61 | + |
| 62 | + /********** event handlers **********/ |
| 63 | + |
| 64 | + ClassPrepareRequest cpRequest; |
| 65 | + ThreadStartRequest tsRequest; |
| 66 | + |
| 67 | + @Override |
| 68 | + public void threadStarted(ThreadStartEvent event) { |
| 69 | + System.out.println("Got ThreadStartEvent: " + event); |
| 70 | + cpRequest = eventRequestManager().createClassPrepareRequest(); |
| 71 | + cpRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL); |
| 72 | + cpRequest.enable(); |
| 73 | + } |
| 74 | + |
| 75 | + static volatile int classPreparedCount = 0; |
| 76 | + |
| 77 | + @Override |
| 78 | + public void classPrepared(ClassPrepareEvent event) { |
| 79 | + try { |
| 80 | + ++classPreparedCount; |
| 81 | + System.out.println("ClassPreparedEvent " + classPreparedCount + |
| 82 | + " on thread " + event.thread() + |
| 83 | + ": " + event.referenceType()); |
| 84 | + List<ThreadGroupReference> groups = vm().topLevelThreadGroups(); |
| 85 | + ThreadGroupReference systemThreadGroup = groups.get(0); |
| 86 | + groups = systemThreadGroup.threadGroups(); |
| 87 | + System.out.println("system child ThreadGroups: " + groups); |
| 88 | + } catch (VMDisconnectedException e) { |
| 89 | + // This usually eventually happens during shutdown. |
| 90 | + System.out.println("ClassPreparedEvent " + classPreparedCount + |
| 91 | + ": Got VMDisconnectedException"); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public void vmDisconnected(VMDisconnectEvent event) { |
| 96 | + System.out.println("Got VMDisconnectEvent"); |
| 97 | + } |
| 98 | + |
| 99 | + /********** test core **********/ |
| 100 | + |
| 101 | + protected void runTests() throws Exception { |
| 102 | + connect(new String[]{"EarlyThreadGroupChildrenTestTarg"}); |
| 103 | + System.out.println("Connected: "); |
| 104 | + addListener(this); |
| 105 | + |
| 106 | + // Create a ThreadStartRequest for the first ThreadStartEvent. When this event is |
| 107 | + // received, we will enable ClassPrepareEvents. |
| 108 | + tsRequest = eventRequestManager().createThreadStartRequest(); |
| 109 | + tsRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL); |
| 110 | + tsRequest.addCountFilter(1); |
| 111 | + tsRequest.enable(); |
| 112 | + |
| 113 | + waitForVMStart(); |
| 114 | + System.out.println("VM Started: "); |
| 115 | + |
| 116 | + resumeToVMDisconnect(); |
| 117 | + |
| 118 | + // Failure mode for this test is deadlocking, so there is no error to check for. |
| 119 | + System.out.println("EarlyThreadGroupChildrenTest: passed"); |
| 120 | + } |
| 121 | +} |
0 commit comments