Skip to content

Commit d54675b

Browse files
Alexey BakhtinYuri Nesterenko
authored andcommitted
8274524: SSLSocket.close() hangs if it is called during the ssl handshake
Backport-of: 58dae60da0711c4ae0cb23f8ce2328e051d603b2
1 parent a59e8cb commit d54675b

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ public final class SSLSocketImpl
107107
private static final boolean trustNameService =
108108
Utilities.getBooleanProperty("jdk.tls.trustNameService", false);
109109

110+
/*
111+
* Default timeout to skip bytes from the open socket
112+
*/
113+
private static final int DEFAULT_SKIP_TIMEOUT = 1;
114+
110115
/**
111116
* Package-private constructor used to instantiate an unconnected
112117
* socket.
@@ -1781,9 +1786,21 @@ private void closeSocket(boolean selfInitiated) throws IOException {
17811786
if (conContext.inputRecord instanceof
17821787
SSLSocketInputRecord inputRecord && isConnected) {
17831788
if (appInput.readLock.tryLock()) {
1789+
int soTimeout = getSoTimeout();
17841790
try {
1791+
// deplete could hang on the skip operation
1792+
// in case of infinite socket read timeout.
1793+
// Change read timeout to avoid deadlock.
1794+
// This workaround could be replaced later
1795+
// with the right synchronization
1796+
if (soTimeout == 0)
1797+
setSoTimeout(DEFAULT_SKIP_TIMEOUT);
17851798
inputRecord.deplete(false);
1799+
} catch (java.net.SocketTimeoutException stEx) {
1800+
// skip timeout exception during deplete
17861801
} finally {
1802+
if (soTimeout == 0)
1803+
setSoTimeout(soTimeout);
17871804
appInput.readLock.unlock();
17881805
}
17891806
}
Lines changed: 134 additions & 0 deletions
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)