Path: blob/master/test/jdk/java/net/ServerSocket/ThreadStop.java
41149 views
/*1* Copyright (c) 2002, 2019, 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 468016026* @summary The deprecated Thread.stop exposes un-checked JNI calls27* that result in crashes when NULL is passed into subsequent28* JNI calls.29*/3031import java.net.*;32import java.io.IOException;3334public class ThreadStop {3536static class Server implements Runnable {3738ServerSocket ss;3940Server() throws IOException {41ss = new ServerSocket();42ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));43}4445public int localPort() {46return ss.getLocalPort();47}484950public void run() {51try {52Socket s = ss.accept();53} catch (IOException ioe) {54} catch (ThreadDeath x) {55} finally {56try {57ss.close();58} catch (IOException x) { }59}60}61}6263public static void main(String args[]) throws Exception {6465// start a server66Server svr = new Server();67Thread thr = new Thread(svr);68thr.start();6970// give server time to block in ServerSocket.accept()71Thread.sleep(2000);7273// "stop" the thread74thr.stop();7576// give thread time to stop77Thread.sleep(2000);7879// it's platform specific if Thread.stop interrupts the80// thread - on Linux/Windows most likely that thread is81// still in accept() so we connect to server which causes82// it to unblock and do JNI-stuff with a pending exception8384try (Socket s = new Socket(svr.ss.getInetAddress(), svr.localPort())) {85} catch (IOException ioe) {86}87thr.join();88}8990}919293