# HG changeset patch # User clanger # Date 1608668929 0 # Tue Dec 22 20:28:49 2020 +0000 # Node ID f8a82b54f31557899c7ae0a2599f33a293a30f30 # Parent cf0f3b64a18f20738345e4a45bae2d797b58331b 8256818: SSLSocket that is never bound or connected leaks socket resources Reviewed-by: xuelei diff --git a/src/share/classes/sun/security/ssl/SSLSocketImpl.java b/src/share/classes/sun/security/ssl/SSLSocketImpl.java --- a/src/share/classes/sun/security/ssl/SSLSocketImpl.java +++ b/src/share/classes/sun/security/ssl/SSLSocketImpl.java @@ -457,7 +457,7 @@ // locks may be deadlocked. @Override public void close() throws IOException { - if (tlsIsClosed) { + if (isClosed()) { return; } @@ -466,19 +466,16 @@ } try { - // shutdown output bound, which may have been closed previously. - if (!isOutputShutdown()) { - duplexCloseOutput(); - } + if (isConnected()) { + // shutdown output bound, which may have been closed previously. + if (!isOutputShutdown()) { + duplexCloseOutput(); + } - // shutdown input bound, which may have been closed previously. - if (!isInputShutdown()) { - duplexCloseInput(); - } - - if (!isClosed()) { - // close the connection directly - closeSocket(false); + // shutdown input bound, which may have been closed previously. + if (!isInputShutdown()) { + duplexCloseInput(); + } } } catch (IOException ioe) { // ignore the exception @@ -486,7 +483,19 @@ SSLLogger.warning("SSLSocket duplex close failed", ioe); } } finally { - tlsIsClosed = true; + if (!isClosed()) { + // close the connection directly + try { + closeSocket(false); + } catch (IOException ioe) { + // ignore the exception + if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { + SSLLogger.warning("SSLSocket close failed", ioe); + } + } finally { + tlsIsClosed = true; + } + } } } diff --git a/test/sun/security/ssl/SSLSocketImpl/SSLSocketLeak.java b/test/sun/security/ssl/SSLSocketImpl/SSLSocketLeak.java new file mode 100644 --- /dev/null +++ b/test/sun/security/ssl/SSLSocketImpl/SSLSocketLeak.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2020 SAP SE. 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. + */ + +import java.io.IOException; +import java.lang.management.ManagementFactory; + +import javax.net.SocketFactory; +import javax.net.ssl.SSLSocketFactory; + +import com.sun.management.UnixOperatingSystemMXBean; + +/* + * @test + * @bug 8256818 + * @summary Test that creating and closing SSL Sockets without bind/connect + * will not leave leaking socket file descriptors + * @run main/native/manual/othervm SSLSocketLeak + * @comment native library is required only on Windows, use the following commands: + * "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat" + * cl ^ + * /LD ^ + * \jdk\test\sun\security\ssl\SSLSocketImpl\libFileUtils.c ^ + * /I\include ^ + * /I\include\win32 ^ + * /FeFileUtils.dll + * jtreg <...> -nativepath:. \jdk\test\sun\security\ssl\SSLSocketImpl\SSLSocketLeak.java + */ +public class SSLSocketLeak { + + private static final int NUM_TEST_SOCK = 500; + private static final boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase().startsWith("windows"); + private static volatile boolean nativeLibLoaded; + + public static void main(String[] args) throws IOException { + long fds_start = getProcessHandleCount(); + System.out.println("FDs at the beginning: " + fds_start); + + SocketFactory f = SSLSocketFactory.getDefault(); + for (int i = 0; i < NUM_TEST_SOCK; i++) { + f.createSocket().close(); + } + + long fds_end = getProcessHandleCount(); + System.out.println("FDs in the end: " + fds_end); + + if ((fds_end - fds_start) > (NUM_TEST_SOCK / 10)) { + throw new RuntimeException("Too many open file descriptors. Looks leaky."); + } + } + + // Return the current process handle count + private static long getProcessHandleCount() { + if (IS_WINDOWS) { + if (!nativeLibLoaded) { + System.loadLibrary("FileUtils"); + nativeLibLoaded = true; + } + return getWinProcessHandleCount(); + } else { + return ((UnixOperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean()).getOpenFileDescriptorCount(); + } + } + + private static native long getWinProcessHandleCount(); +} diff --git a/test/sun/security/ssl/SSLSocketImpl/libFileUtils.c b/test/sun/security/ssl/SSLSocketImpl/libFileUtils.c new file mode 100644 --- /dev/null +++ b/test/sun/security/ssl/SSLSocketImpl/libFileUtils.c @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2020, 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. + */ + +#ifdef _WIN32 + +#include "jni.h" +#include + +JNIEXPORT jlong JNICALL Java_SSLSocketLeak_getWinProcessHandleCount(JNIEnv *env) +{ + DWORD handleCount; + HANDLE handle = GetCurrentProcess(); + if (GetProcessHandleCount(handle, &handleCount)) { + return (jlong)handleCount; + } else { + return -1L; + } +} + +#endif /* _WIN32 */