|
| 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 SocketReadInterruptTest 2000 3000 |
| 31 | + * @run main/othervm/native SocketReadInterruptTest 2000 3000 |
| 32 | + * @run main/othervm/native -Djdk.net.usePlainSocketImpl=true SocketReadInterruptTest 2000 0 |
| 33 | + * @run main/othervm/native SocketReadInterruptTest 2000 0 |
| 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 | + |
| 41 | +import java.net.*; |
| 42 | +import java.util.concurrent.Callable; |
| 43 | +import java.util.concurrent.ExecutorService; |
| 44 | +import java.util.concurrent.Executors; |
| 45 | +import java.util.concurrent.Future; |
| 46 | + |
| 47 | +public class SocketReadInterruptTest { |
| 48 | + |
| 49 | + public static void main(String[] args) throws Exception { |
| 50 | + System.loadLibrary("NativeThread"); |
| 51 | + ExecutorService executor = Executors.newFixedThreadPool(2); |
| 52 | + InetAddress loopback = InetAddress.getLoopbackAddress(); |
| 53 | + |
| 54 | + try ( ServerSocket ss = new ServerSocket(0, 50, loopback); |
| 55 | + Socket s1 = new Socket(loopback, ss.getLocalPort());) { |
| 56 | + Server server = new Server(ss, Integer.parseInt(args[0])); |
| 57 | + Future<Result> f1 = executor.submit(server); |
| 58 | + |
| 59 | + Client client = new Client(s1, Integer.parseInt(args[1])); |
| 60 | + Future<Result> f2 = executor.submit(client); |
| 61 | + long threadId = client.getID(); |
| 62 | + |
| 63 | + sleep(200); |
| 64 | + System.out.println("Sending SIGPIPE to client thread."); |
| 65 | + if (NativeThread.signal(threadId, NativeThread.SIGPIPE) != 0) { |
| 66 | + throw new RuntimeException("Failed to interrupt the thread."); |
| 67 | + } |
| 68 | + |
| 69 | + Result r1 = f1.get(); |
| 70 | + if (r1.status == Result.FAIL) { |
| 71 | + throw r1.exception; |
| 72 | + } |
| 73 | + |
| 74 | + Result r2 = f2.get(); |
| 75 | + if (r2.status == Result.FAIL) { |
| 76 | + throw r2.exception; |
| 77 | + } |
| 78 | + System.out.println("OK!"); |
| 79 | + } finally { |
| 80 | + executor.shutdown(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private static void sleep(long time) { |
| 85 | + try { |
| 86 | + Thread.sleep(time); |
| 87 | + } catch (InterruptedException e) { |
| 88 | + Thread.currentThread().interrupt(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + static class Client implements Callable<Result> { |
| 93 | + |
| 94 | + private volatile long threadId; |
| 95 | + private final Socket client; |
| 96 | + private final int timeout; |
| 97 | + |
| 98 | + public Client(Socket s, int timeout) { |
| 99 | + client = s; |
| 100 | + this.timeout = timeout; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public Result call() { |
| 105 | + threadId = NativeThread.getID(); |
| 106 | + byte[] arr = new byte[64]; |
| 107 | + try ( InputStream in = client.getInputStream();) { |
| 108 | + client.setSoTimeout(timeout); |
| 109 | + in.read(arr); |
| 110 | + return new Result(Result.SUCCESS, null); |
| 111 | + } catch (IOException ex) { |
| 112 | + close(); |
| 113 | + return new Result(Result.FAIL, ex); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + long getID() { |
| 118 | + while (threadId == 0) { |
| 119 | + sleep(5); |
| 120 | + } |
| 121 | + return threadId; |
| 122 | + } |
| 123 | + |
| 124 | + void close() { |
| 125 | + if (!client.isClosed()) { |
| 126 | + try { |
| 127 | + client.close(); |
| 128 | + } catch (IOException ex) { |
| 129 | + // ignore the exception. |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + static class Server implements Callable<Result> { |
| 136 | + |
| 137 | + private final ServerSocket serverSocket; |
| 138 | + private final int timeout; |
| 139 | + |
| 140 | + public Server(ServerSocket ss, int timeout) { |
| 141 | + serverSocket = ss; |
| 142 | + this.timeout = timeout; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public Result call() { |
| 147 | + try { |
| 148 | + try ( Socket client = serverSocket.accept(); OutputStream outputStream = client.getOutputStream();) { |
| 149 | + sleep(timeout); |
| 150 | + outputStream.write("This is just a test string.".getBytes()); |
| 151 | + return new Result(Result.SUCCESS, null); |
| 152 | + } |
| 153 | + } catch (IOException e) { |
| 154 | + close(); |
| 155 | + return new Result(Result.FAIL, e); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + public void close() { |
| 160 | + if (!serverSocket.isClosed()) { |
| 161 | + try { |
| 162 | + serverSocket.close(); |
| 163 | + } catch (IOException ex) { |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + static class Result { |
| 170 | + |
| 171 | + static final int SUCCESS = 0; |
| 172 | + static final int FAIL = 1; |
| 173 | + final int status; |
| 174 | + final Exception exception; |
| 175 | + |
| 176 | + public Result(int status, Exception ex) { |
| 177 | + this.status = status; |
| 178 | + exception = ex; |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments