|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 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 8293562 |
| 27 | + * @library /test/lib |
| 28 | + * @run main/othervm -Dhttp.keepAlive.time.server=1 B8293562 |
| 29 | + * @summary Http keep-alive thread should close sockets without holding a lock |
| 30 | + */ |
| 31 | + |
| 32 | +import com.sun.net.httpserver.HttpServer; |
| 33 | + |
| 34 | +import javax.net.ssl.HandshakeCompletedListener; |
| 35 | +import javax.net.ssl.HttpsURLConnection; |
| 36 | +import javax.net.ssl.SSLSession; |
| 37 | +import javax.net.ssl.SSLSocket; |
| 38 | +import javax.net.ssl.SSLSocketFactory; |
| 39 | +import java.io.FileNotFoundException; |
| 40 | +import java.io.IOException; |
| 41 | +import java.io.InputStream; |
| 42 | +import java.net.InetAddress; |
| 43 | +import java.net.InetSocketAddress; |
| 44 | +import java.net.Proxy; |
| 45 | +import java.net.Socket; |
| 46 | +import java.net.URL; |
| 47 | +import java.net.UnknownHostException; |
| 48 | +import java.util.concurrent.CompletableFuture; |
| 49 | +import java.util.concurrent.CountDownLatch; |
| 50 | +import java.util.concurrent.Executors; |
| 51 | +import java.util.concurrent.TimeUnit; |
| 52 | + |
| 53 | +public class B8293562 { |
| 54 | + static HttpServer server; |
| 55 | + static CountDownLatch closing = new CountDownLatch(1); |
| 56 | + static CountDownLatch secondRequestDone = new CountDownLatch(1); |
| 57 | + static CompletableFuture<Void> result = new CompletableFuture<>(); |
| 58 | + |
| 59 | + public static void main(String[] args) throws Exception { |
| 60 | + startHttpServer(); |
| 61 | + clientHttpCalls(); |
| 62 | + } |
| 63 | + |
| 64 | + public static void startHttpServer() throws Exception { |
| 65 | + server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 10); |
| 66 | + server.setExecutor(Executors.newCachedThreadPool()); |
| 67 | + server.start(); |
| 68 | + } |
| 69 | + |
| 70 | + public static void clientHttpCalls() throws Exception { |
| 71 | + try { |
| 72 | + System.out.println("http server listen on: " + server.getAddress().getPort()); |
| 73 | + String hostAddr = InetAddress.getLoopbackAddress().getHostAddress(); |
| 74 | + if (hostAddr.indexOf(':') > -1) hostAddr = "[" + hostAddr + "]"; |
| 75 | + String baseURLStr = "https://" + hostAddr + ":" + server.getAddress().getPort() + "/"; |
| 76 | + |
| 77 | + URL testUrl = new URL (baseURLStr); |
| 78 | + |
| 79 | + // SlowCloseSocketFactory is not a real SSLSocketFactory; |
| 80 | + // it produces regular non-SSL sockets. Effectively, the request |
| 81 | + // is made over http. |
| 82 | + HttpsURLConnection.setDefaultSSLSocketFactory(new SlowCloseSocketFactory()); |
| 83 | + System.out.println("Performing first request"); |
| 84 | + HttpsURLConnection uc = (HttpsURLConnection)testUrl.openConnection(Proxy.NO_PROXY); |
| 85 | + byte[] buf = new byte[1024]; |
| 86 | + try { |
| 87 | + uc.getInputStream(); |
| 88 | + throw new RuntimeException("Expected 404 here"); |
| 89 | + } catch (FileNotFoundException ignored) { } |
| 90 | + try (InputStream is = uc.getErrorStream()) { |
| 91 | + while (is.read(buf) >= 0) { |
| 92 | + } |
| 93 | + } |
| 94 | + System.out.println("First request completed"); |
| 95 | + closing.await(); |
| 96 | + // KeepAliveThread is closing the connection now |
| 97 | + System.out.println("Performing second request"); |
| 98 | + HttpsURLConnection uc2 = (HttpsURLConnection)testUrl.openConnection(Proxy.NO_PROXY); |
| 99 | + |
| 100 | + try { |
| 101 | + uc2.getInputStream(); |
| 102 | + throw new RuntimeException("Expected 404 here"); |
| 103 | + } catch (FileNotFoundException ignored) { } |
| 104 | + try (InputStream is = uc2.getErrorStream()) { |
| 105 | + while (is.read(buf) >= 0) { |
| 106 | + } |
| 107 | + } |
| 108 | + System.out.println("Second request completed"); |
| 109 | + // let the socket know it can close now |
| 110 | + secondRequestDone.countDown(); |
| 111 | + result.get(); |
| 112 | + System.out.println("Test completed successfully"); |
| 113 | + } finally { |
| 114 | + server.stop(1); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + static class SlowCloseSocket extends SSLSocket { |
| 119 | + @Override |
| 120 | + public synchronized void close() throws IOException { |
| 121 | + String threadName = Thread.currentThread().getName(); |
| 122 | + System.out.println("Connection closing, thread name: " + threadName); |
| 123 | + closing.countDown(); |
| 124 | + super.close(); |
| 125 | + if (threadName.equals("Keep-Alive-Timer")) { |
| 126 | + try { |
| 127 | + if (secondRequestDone.await(5, TimeUnit.SECONDS)) { |
| 128 | + result.complete(null); |
| 129 | + } else { |
| 130 | + result.completeExceptionally(new RuntimeException( |
| 131 | + "Wait for second request timed out")); |
| 132 | + } |
| 133 | + } catch (InterruptedException e) { |
| 134 | + result.completeExceptionally(new RuntimeException( |
| 135 | + "Wait for second request was interrupted")); |
| 136 | + } |
| 137 | + } else { |
| 138 | + result.completeExceptionally(new RuntimeException( |
| 139 | + "Close invoked from unexpected thread")); |
| 140 | + } |
| 141 | + System.out.println("Connection closed"); |
| 142 | + } |
| 143 | + |
| 144 | + // required abstract method overrides |
| 145 | + @Override |
| 146 | + public String[] getSupportedCipherSuites() { |
| 147 | + return new String[0]; |
| 148 | + } |
| 149 | + @Override |
| 150 | + public String[] getEnabledCipherSuites() { |
| 151 | + return new String[0]; |
| 152 | + } |
| 153 | + @Override |
| 154 | + public void setEnabledCipherSuites(String[] suites) { } |
| 155 | + @Override |
| 156 | + public String[] getSupportedProtocols() { |
| 157 | + return new String[0]; |
| 158 | + } |
| 159 | + @Override |
| 160 | + public String[] getEnabledProtocols() { |
| 161 | + return new String[0]; |
| 162 | + } |
| 163 | + @Override |
| 164 | + public void setEnabledProtocols(String[] protocols) { } |
| 165 | + @Override |
| 166 | + public SSLSession getSession() { |
| 167 | + return null; |
| 168 | + } |
| 169 | + @Override |
| 170 | + public void addHandshakeCompletedListener(HandshakeCompletedListener listener) { } |
| 171 | + @Override |
| 172 | + public void removeHandshakeCompletedListener(HandshakeCompletedListener listener) { } |
| 173 | + @Override |
| 174 | + public void startHandshake() throws IOException { } |
| 175 | + @Override |
| 176 | + public void setUseClientMode(boolean mode) { } |
| 177 | + @Override |
| 178 | + public boolean getUseClientMode() { |
| 179 | + return false; |
| 180 | + } |
| 181 | + @Override |
| 182 | + public void setNeedClientAuth(boolean need) { } |
| 183 | + @Override |
| 184 | + public boolean getNeedClientAuth() { |
| 185 | + return false; |
| 186 | + } |
| 187 | + @Override |
| 188 | + public void setWantClientAuth(boolean want) { } |
| 189 | + @Override |
| 190 | + public boolean getWantClientAuth() { |
| 191 | + return false; |
| 192 | + } |
| 193 | + @Override |
| 194 | + public void setEnableSessionCreation(boolean flag) { } |
| 195 | + @Override |
| 196 | + public boolean getEnableSessionCreation() { |
| 197 | + return false; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + static class SlowCloseSocketFactory extends SSLSocketFactory { |
| 202 | + |
| 203 | + @Override |
| 204 | + public Socket createSocket() throws IOException { |
| 205 | + return new SlowCloseSocket(); |
| 206 | + } |
| 207 | + // required abstract method overrides |
| 208 | + @Override |
| 209 | + public Socket createSocket(String host, int port) throws IOException, UnknownHostException { |
| 210 | + throw new UnsupportedOperationException(); |
| 211 | + } |
| 212 | + @Override |
| 213 | + public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException { |
| 214 | + throw new UnsupportedOperationException(); |
| 215 | + } |
| 216 | + @Override |
| 217 | + public Socket createSocket(InetAddress host, int port) throws IOException { |
| 218 | + throw new UnsupportedOperationException(); |
| 219 | + } |
| 220 | + @Override |
| 221 | + public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException { |
| 222 | + throw new UnsupportedOperationException(); |
| 223 | + } |
| 224 | + @Override |
| 225 | + public String[] getDefaultCipherSuites() { |
| 226 | + return new String[0]; |
| 227 | + } |
| 228 | + @Override |
| 229 | + public String[] getSupportedCipherSuites() { |
| 230 | + return new String[0]; |
| 231 | + } |
| 232 | + @Override |
| 233 | + public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException { |
| 234 | + throw new UnsupportedOperationException(); |
| 235 | + } |
| 236 | + } |
| 237 | +} |
| 238 | + |
0 commit comments