|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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 8237858 |
| 27 | + * @summary PlainSocketImpl.socketAccept() handles EINTR incorrectly |
| 28 | + * @requires (os.family != "windows") |
| 29 | + * @compile NativeThread.java |
| 30 | + * @run main/othervm/native -Djdk.net.usePlainSocketImpl=true SocketAcceptInterruptTest 0 |
| 31 | + * @run main/othervm/native -Djdk.net.usePlainSocketImpl=true SocketAcceptInterruptTest 5000 |
| 32 | + * @run main/othervm/native SocketAcceptInterruptTest 0 |
| 33 | + * @run main/othervm/native SocketAcceptInterruptTest 5000 |
| 34 | + */ |
| 35 | +import java.io.IOException; |
| 36 | +import java.io.InputStream; |
| 37 | +import java.io.OutputStream; |
| 38 | +import java.net.InetAddress; |
| 39 | +import java.net.ServerSocket; |
| 40 | +import java.net.*; |
| 41 | +import java.util.concurrent.Callable; |
| 42 | +import java.util.concurrent.ExecutorService; |
| 43 | +import java.util.concurrent.Executors; |
| 44 | +import java.util.concurrent.Future; |
| 45 | + |
| 46 | +public class SocketAcceptInterruptTest { |
| 47 | + |
| 48 | + public static void main(String[] args) throws Exception { |
| 49 | + System.loadLibrary("NativeThread"); |
| 50 | + InetAddress loopback = InetAddress.getLoopbackAddress(); |
| 51 | + ExecutorService executor = Executors.newFixedThreadPool(1); |
| 52 | + |
| 53 | + try ( ServerSocket ss = new ServerSocket(0, 50, loopback);) { |
| 54 | + Server server = new Server(ss, Integer.parseInt(args[0])); |
| 55 | + Future<Result> future = executor.submit(server); |
| 56 | + long threadId = server.getID(); |
| 57 | + |
| 58 | + sendSignal(threadId, ss); |
| 59 | + sleep(100); |
| 60 | + // In failing case server socket will be closed, so we do need to check first |
| 61 | + if (!ss.isClosed()) { |
| 62 | + // After sending SIGPIPE, create client socket and connect to server |
| 63 | + try ( Socket s = new Socket(loopback, ss.getLocalPort()); InputStream in = s.getInputStream();) { |
| 64 | + in.read(); // reading one byte is enought for test. |
| 65 | + } |
| 66 | + } |
| 67 | + Result result = future.get(); |
| 68 | + if (result.status == Result.FAIL) { |
| 69 | + throw result.exception; |
| 70 | + } |
| 71 | + } finally { |
| 72 | + executor.shutdown(); |
| 73 | + } |
| 74 | + System.out.println("OK!"); |
| 75 | + } |
| 76 | + |
| 77 | + private static void sendSignal(long threadId, ServerSocket ss) { |
| 78 | + System.out.println("Sending SIGPIPE to ServerSocket thread."); |
| 79 | + int count = 0; |
| 80 | + while (!ss.isClosed() && count++ < 20) { |
| 81 | + sleep(10); |
| 82 | + if (NativeThread.signal(threadId, NativeThread.SIGPIPE) != 0) { |
| 83 | + throw new RuntimeException("Failed to interrupt the server thread."); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private static void sleep(long time) { |
| 89 | + try { |
| 90 | + Thread.sleep(time); |
| 91 | + } catch (InterruptedException e) { |
| 92 | + // ignore the exception. |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + static class Server implements Callable<Result> { |
| 97 | + |
| 98 | + private volatile long threadId; |
| 99 | + private final ServerSocket serverSocket; |
| 100 | + private final int timeout; |
| 101 | + |
| 102 | + public Server(ServerSocket ss, int timeout) { |
| 103 | + serverSocket = ss; |
| 104 | + this.timeout = timeout; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public Result call() { |
| 109 | + try { |
| 110 | + threadId = NativeThread.getID(); |
| 111 | + serverSocket.setSoTimeout(timeout); |
| 112 | + try ( Socket socket = serverSocket.accept(); |
| 113 | + OutputStream outputStream = socket.getOutputStream();) { |
| 114 | + outputStream.write("Hello!".getBytes()); |
| 115 | + return new Result(Result.SUCCESS, null); |
| 116 | + } |
| 117 | + } catch (IOException e) { |
| 118 | + close(); |
| 119 | + return new Result(Result.FAIL, e); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + long getID() { |
| 124 | + while (threadId == 0) { |
| 125 | + sleep(5); |
| 126 | + } |
| 127 | + return threadId; |
| 128 | + } |
| 129 | + |
| 130 | + private void close() { |
| 131 | + if (!serverSocket.isClosed()) { |
| 132 | + try { |
| 133 | + serverSocket.close(); |
| 134 | + } catch (IOException ex) { |
| 135 | + // ignore the exception |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + static class Result { |
| 142 | + |
| 143 | + static final int SUCCESS = 0; |
| 144 | + static final int FAIL = 1; |
| 145 | + final int status; |
| 146 | + final Exception exception; |
| 147 | + |
| 148 | + public Result(int status, Exception ex) { |
| 149 | + this.status = status; |
| 150 | + exception = ex; |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments