--- old/src/hotspot/share/runtime/thread.cpp Sat Nov 18 16:53:57 2017 +++ new/src/hotspot/share/runtime/thread.cpp Sat Nov 18 16:53:56 2017 @@ -2023,11 +2023,6 @@ // Remove from list of active threads list, and notify VM thread if we are the last non-daemon thread Threads::remove(this); - // If someone set a handshake on us just as we entered exit path, we simple cancel it. - if (ThreadLocalHandshakes) { - cancel_handshake(); - } - if (log_is_enabled(Debug, os, thread, timer)) { _timer_exit_phase4.stop(); ResourceMark rm(this); @@ -4830,6 +4825,11 @@ // Retry the whole scenario. } + // If someone set a handshake on us just as we entered exit path, we simple cancel it. + if (ThreadLocalHandshakes) { + thread->cancel_handshake(); + } + delete thread; if (EnableThreadSMRStatistics) { timer.stop(); --- old/test/hotspot/jtreg/runtime/handshake/HandshakeWalkExitTest.java Sat Nov 18 16:54:00 2017 +++ new/test/hotspot/jtreg/runtime/handshake/HandshakeWalkExitTest.java Sat Nov 18 16:53:59 2017 @@ -42,22 +42,18 @@ } static volatile boolean exit_now = false; - static Thread[] threads; public static void main(String... args) throws Exception { - int testRuns = 100; - int testThreads = 500; + int testRuns = 20; + int testThreads = 128; HandshakeWalkExitTest test = new HandshakeWalkExitTest(); - threads = new Thread[64]; - Runnable hser = new Runnable(){ public void run(){ WhiteBox wb = WhiteBox.getWhiteBox(); while(!exit_now) { wb.handshakeWalkStack(null, true); - try { Thread.sleep(1); } catch(Exception e) {} } } }; --- /dev/null Sat Nov 18 16:54:02 2017 +++ new/test/hotspot/jtreg/runtime/handshake/HandshakeWalkOneExitTest.java Sat Nov 18 16:54:01 2017 @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2017, 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. + * + */ + +/* + * @test HandshakeWalkOneExitTest + * @summary This test tries to stress the handshakes with new and exiting threads + * @library /testlibrary /test/lib + * @build HandshakeWalkOneExitTest + * @run main ClassFileInstaller sun.hotspot.WhiteBox + * sun.hotspot.WhiteBox$WhiteBoxPermission + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI HandshakeWalkOneExitTest + */ + +import jdk.test.lib.Asserts; +import sun.hotspot.WhiteBox; + +public class HandshakeWalkOneExitTest implements Runnable { + + @Override + public void run() { + } + + static volatile boolean exit_now = false; + static Thread[] threads; + + public static void main(String... args) throws Exception { + int testRuns = 20; + int testThreads = 128; + + HandshakeWalkOneExitTest test = new HandshakeWalkOneExitTest(); + + Runnable hser = new Runnable(){ + public void run(){ + WhiteBox wb = WhiteBox.getWhiteBox(); + while(!exit_now) { + Thread[] t = threads; + for (int i = 0; i