Skip to content

Commit 7f3e86c

Browse files
author
Alexey Bakhtin
committed
8274524: SSLSocket.close() hangs if it is called during the ssl handshake
Reviewed-by: phh, andrew
1 parent 9a129e3 commit 7f3e86c

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

jdk/src/share/classes/sun/security/ssl/SSLSocketImpl.java

+19
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ public final class SSLSocketImpl
9696
private static final boolean trustNameService =
9797
Utilities.getBooleanProperty("jdk.tls.trustNameService", false);
9898

99+
/*
100+
* Default timeout to skip bytes from the open socket
101+
*/
102+
private static final int DEFAULT_SKIP_TIMEOUT = 1;
103+
99104
/**
100105
* Package-private constructor used to instantiate an unconnected
101106
* socket.
@@ -1635,9 +1640,23 @@ private void closeSocket(boolean selfInitiated) throws IOException {
16351640
if (conContext.inputRecord instanceof
16361641
SSLSocketInputRecord && isConnected) {
16371642
if (appInput.readLock.tryLock()) {
1643+
int soTimeout = getSoTimeout();
16381644
try {
1645+
// deplete could hang on the skip operation
1646+
// in case of infinite socket read timeout.
1647+
// Change read timeout to avoid deadlock.
1648+
// This workaround could be replaced later
1649+
// with the right synchronization
1650+
if (soTimeout == 0) {
1651+
setSoTimeout(DEFAULT_SKIP_TIMEOUT);
1652+
}
16391653
((SSLSocketInputRecord) (conContext.inputRecord)).deplete(false);
1654+
} catch (java.net.SocketTimeoutException stEx) {
1655+
// skip timeout exception during deplete
16401656
} finally {
1657+
if (soTimeout == 0) {
1658+
setSoTimeout(soTimeout);
1659+
}
16411660
appInput.readLock.unlock();
16421661
}
16431662
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright (c) 2021, 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 8274524
27+
* @summary 8274524: SSLSocket.close() hangs if it is called during the ssl handshake
28+
* @library /javax/net/ssl/templates
29+
* @run main/othervm ClientSocketCloseHang TLSv1.2
30+
* @run main/othervm ClientSocketCloseHang TLSv1.3
31+
*/
32+
33+
34+
import javax.net.ssl.*;
35+
import java.net.InetAddress;
36+
37+
public class ClientSocketCloseHang implements SSLContextTemplate {
38+
39+
public static void main(String[] args) throws Exception {
40+
System.setProperty("jdk.tls.client.protocols", args[0]);
41+
for (int i = 0; i<= 20; i++) {
42+
System.err.println("===================================");
43+
System.err.println("loop " + i);
44+
System.err.println("===================================");
45+
new ClientSocketCloseHang().test();
46+
}
47+
}
48+
49+
private void test() throws Exception {
50+
SSLServerSocket listenSocket = null;
51+
SSLSocket serverSocket = null;
52+
ClientSocket clientSocket = null;
53+
try {
54+
SSLServerSocketFactory serversocketfactory =
55+
createServerSSLContext().getServerSocketFactory();
56+
listenSocket =
57+
(SSLServerSocket)serversocketfactory.createServerSocket(0);
58+
listenSocket.setNeedClientAuth(false);
59+
listenSocket.setEnableSessionCreation(true);
60+
listenSocket.setUseClientMode(false);
61+
62+
63+
System.err.println("Starting client");
64+
clientSocket = new ClientSocket(listenSocket.getLocalPort());
65+
clientSocket.start();
66+
67+
System.err.println("Accepting client requests");
68+
serverSocket = (SSLSocket) listenSocket.accept();
69+
70+
serverSocket.startHandshake();
71+
} finally {
72+
if (clientSocket != null) {
73+
clientSocket.close();
74+
}
75+
if (listenSocket != null) {
76+
listenSocket.close();
77+
}
78+
79+
if (serverSocket != null) {
80+
serverSocket.close();
81+
}
82+
}
83+
}
84+
85+
private class ClientSocket extends Thread{
86+
int serverPort = 0;
87+
SSLSocket clientSocket = null;
88+
89+
public ClientSocket(int serverPort) {
90+
this.serverPort = serverPort;
91+
}
92+
93+
@Override
94+
public void run() {
95+
try {
96+
System.err.println(
97+
"Connecting to server at port " + serverPort);
98+
SSLSocketFactory sslSocketFactory =
99+
createClientSSLContext().getSocketFactory();
100+
clientSocket = (SSLSocket)sslSocketFactory.createSocket(
101+
InetAddress.getLocalHost(), serverPort);
102+
clientSocket.setSoLinger(true, 3);
103+
clientSocket.startHandshake();
104+
} catch (Exception e) {
105+
}
106+
}
107+
108+
public void close() {
109+
Thread t = new Thread() {
110+
@Override
111+
public void run() {
112+
try {
113+
if (clientSocket != null) {
114+
clientSocket.close();
115+
}
116+
} catch (Exception ex) {
117+
}
118+
}
119+
};
120+
try {
121+
// Close client connection
122+
t.start();
123+
t.join(2000); // 2 sec
124+
} catch (InterruptedException ex) {
125+
return;
126+
}
127+
128+
if (t.isAlive()) {
129+
throw new RuntimeException("SSL Client hangs on close");
130+
}
131+
}
132+
}
133+
}
134+

0 commit comments

Comments
 (0)