Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/java.base/share/classes/sun/security/ssl/SSLSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public final class SSLSocketImpl
private static final boolean trustNameService =
Utilities.getBooleanProperty("jdk.tls.trustNameService", false);

/*
* Default timeout to skip bytes from the open socket
*/
private static final int DEFAULT_SKIP_TIMEOUT = 1;

/**
* Package-private constructor used to instantiate an unconnected
* socket.
Expand Down Expand Up @@ -1781,9 +1786,21 @@ private void closeSocket(boolean selfInitiated) throws IOException {
if (conContext.inputRecord instanceof
SSLSocketInputRecord inputRecord && isConnected) {
if (appInput.readLock.tryLock()) {
int soTimeout = getSoTimeout();
try {
// deplete could hang on the skip operation
// in case of infinite socket read timeout.
// Change read timeout to avoid deadlock.
// This workaround could be replaced later
// with the right synchronization
if (soTimeout == 0)
setSoTimeout(DEFAULT_SKIP_TIMEOUT);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This set will impact the socket overall behavior unexpectedly, not just the close() method.

Maybe, an input stream level synchronization is missed in the SSLSocketInputRecord method, so that logic of deplete could be interrupted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @XueleiFan,
Not quite sure What could be wrong with setting SO_TIMEOUT during the socketClose() operation. We still close the socket and SO_TIMEOUT will be restored just after skip() operation is completed. These changes could affect the parallel read of the handshake records (we hold appInput.readLock during the skip) but we still close the socket and interrupt connection in this case.
If you still think these changes are incorrect, What do you think about the most first version of the patch: d1c2a4f
This version adds a new lock in the SSLSocketInputRecord and protects the parallel execution of the read and skip operations.

Copy link
Member

@XueleiFan XueleiFan Feb 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand the issue correctly, the problem is about that the skip method cannot get the right length if the input stream could be accessed in another thread.

  1. get the available input stream bytes length;
  2. a third thread read the input stream, and the input stream get changed;
  3. skip up to the bytes length in step 1.
  4. the skip() method hangs on waiting for more bytes.

I think we could focus on on address the synchronization problem, because the problem could result in other weird behaviors we don't know yet.

For the SO_TIMEOUT, I think it is a good workaround. But I'm not sure if it will impact other behaviors or not. Besides, I know you are trying to use a small timeout, but it is still blocked and it not easy to find a number fit all situation that does not impact the performance.

As I commented in the 1st version, I'm not sure of the locks logic in the patch, which changes the behavior of SSLSocket.

I may suggest to an input stream level synchronization. I know the update could take a while, and may not be able to integrate in time. If you want to fix the issue as soon as possible, I'm OK to move ahead with your current direction, but please set the SO_TIMEOUT to as minimal as possible, for example 1 ms; and have a comment that this is a temporary/workaround solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you again for your detailed response and comments.
Your assumption about this issue is right and I think SO_TIMEOUT should be an acceptable solution.
I've changed DEFAULT_SKIP_TIMEOUT to 1 ms and added comments about a temporary workaround.
If you don't mind, I'd like to commit it asap because this patch should be backported to the early versions.

No regressions were found on the sun/security/ssl tests.

inputRecord.deplete(false);
} catch (java.net.SocketTimeoutException stEx) {
// skip timeout exception during deplete
} finally {
if (soTimeout == 0)
setSoTimeout(soTimeout);
appInput.readLock.unlock();
}
}
Expand Down
134 changes: 134 additions & 0 deletions test/jdk/sun/security/ssl/SSLSocketImpl/ClientSocketCloseHang.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8274524
* @summary 8274524: SSLSocket.close() hangs if it is called during the ssl handshake
* @library /javax/net/ssl/templates
* @run main/othervm ClientSocketCloseHang TLSv1.2
* @run main/othervm ClientSocketCloseHang TLSv1.3
*/


import javax.net.ssl.*;
import java.net.InetAddress;

public class ClientSocketCloseHang implements SSLContextTemplate {

public static void main(String[] args) throws Exception {
System.setProperty("jdk.tls.client.protocols", args[0]);
for (int i = 0; i<= 20; i++) {
System.err.println("===================================");
System.err.println("loop " + i);
System.err.println("===================================");
new ClientSocketCloseHang().test();
}
}

private void test() throws Exception {
SSLServerSocket listenSocket = null;
SSLSocket serverSocket = null;
ClientSocket clientSocket = null;
try {
SSLServerSocketFactory serversocketfactory =
createServerSSLContext().getServerSocketFactory();
listenSocket =
(SSLServerSocket)serversocketfactory.createServerSocket(0);
listenSocket.setNeedClientAuth(false);
listenSocket.setEnableSessionCreation(true);
listenSocket.setUseClientMode(false);


System.err.println("Starting client");
clientSocket = new ClientSocket(listenSocket.getLocalPort());
clientSocket.start();

System.err.println("Accepting client requests");
serverSocket = (SSLSocket) listenSocket.accept();

serverSocket.startHandshake();
} finally {
if (clientSocket != null) {
clientSocket.close();
}
if (listenSocket != null) {
listenSocket.close();
}

if (serverSocket != null) {
serverSocket.close();
}
}
}

private class ClientSocket extends Thread{
int serverPort = 0;
SSLSocket clientSocket = null;

public ClientSocket(int serverPort) {
this.serverPort = serverPort;
}

@Override
public void run() {
try {
System.err.println(
"Connecting to server at port " + serverPort);
SSLSocketFactory sslSocketFactory =
createClientSSLContext().getSocketFactory();
clientSocket = (SSLSocket)sslSocketFactory.createSocket(
InetAddress.getLocalHost(), serverPort);
clientSocket.setSoLinger(true, 3);
clientSocket.startHandshake();
} catch (Exception e) {
}
}

public void close() {
Thread t = new Thread() {
@Override
public void run() {
try {
if (clientSocket != null) {
clientSocket.close();
}
} catch (Exception ex) {
}
}
};
try {
// Close client connection
t.start();
t.join(2000); // 2 sec
} catch (InterruptedException ex) {
return;
}

if (t.isAlive()) {
throw new RuntimeException("SSL Client hangs on close");
}
}
}
}