From a16e54c44623bc37a2e4d73c01f7af5876892d8e Mon Sep 17 00:00:00 2001 From: Takakuri Date: Wed, 24 Jan 2024 21:03:29 +0900 Subject: [PATCH] Backport d91e227abb94953129adc297fbd456c55bb2ae10 --- .../sun/nio/ch/sctp/SctpChannelImpl.java | 3 +- .../sun/nio/ch/sctp/SctpMultiChannelImpl.java | 3 +- .../nio/ch/sctp/SctpServerChannelImpl.java | 3 +- .../sctp/SctpChannel/CloseDescriptors.java | 211 ++++++++++++++++++ 4 files changed, 217 insertions(+), 3 deletions(-) create mode 100644 jdk/test/com/sun/nio/sctp/SctpChannel/CloseDescriptors.java diff --git a/jdk/src/solaris/classes/sun/nio/ch/sctp/SctpChannelImpl.java b/jdk/src/solaris/classes/sun/nio/ch/sctp/SctpChannelImpl.java index 246d9dea3ba..4961e5a29a1 100644 --- a/jdk/src/solaris/classes/sun/nio/ch/sctp/SctpChannelImpl.java +++ b/jdk/src/solaris/classes/sun/nio/ch/sctp/SctpChannelImpl.java @@ -562,7 +562,8 @@ protected void implConfigureBlocking(boolean block) throws IOException { @Override public void implCloseSelectableChannel() throws IOException { synchronized (stateLock) { - SctpNet.preClose(fdVal); + if (state != ChannelState.KILLED) + SctpNet.preClose(fdVal); if (receiverThread != 0) NativeThread.signal(receiverThread); diff --git a/jdk/src/solaris/classes/sun/nio/ch/sctp/SctpMultiChannelImpl.java b/jdk/src/solaris/classes/sun/nio/ch/sctp/SctpMultiChannelImpl.java index db08f342aa1..629c1ca1e5e 100644 --- a/jdk/src/solaris/classes/sun/nio/ch/sctp/SctpMultiChannelImpl.java +++ b/jdk/src/solaris/classes/sun/nio/ch/sctp/SctpMultiChannelImpl.java @@ -289,7 +289,8 @@ protected void implConfigureBlocking(boolean block) throws IOException { @Override public void implCloseSelectableChannel() throws IOException { synchronized (stateLock) { - SctpNet.preClose(fdVal); + if (state != ChannelState.KILLED) + SctpNet.preClose(fdVal); if (receiverThread != 0) NativeThread.signal(receiverThread); diff --git a/jdk/src/solaris/classes/sun/nio/ch/sctp/SctpServerChannelImpl.java b/jdk/src/solaris/classes/sun/nio/ch/sctp/SctpServerChannelImpl.java index ee9d7f85ea1..ba7051c117c 100644 --- a/jdk/src/solaris/classes/sun/nio/ch/sctp/SctpServerChannelImpl.java +++ b/jdk/src/solaris/classes/sun/nio/ch/sctp/SctpServerChannelImpl.java @@ -266,7 +266,8 @@ protected void implConfigureBlocking(boolean block) throws IOException { @Override public void implCloseSelectableChannel() throws IOException { synchronized (stateLock) { - SctpNet.preClose(fdVal); + if (state != ChannelState.KILLED) + SctpNet.preClose(fdVal); if (thread != 0) NativeThread.signal(thread); if (!isRegistered()) diff --git a/jdk/test/com/sun/nio/sctp/SctpChannel/CloseDescriptors.java b/jdk/test/com/sun/nio/sctp/SctpChannel/CloseDescriptors.java new file mode 100644 index 00000000000..d5b9c460e6d --- /dev/null +++ b/jdk/test/com/sun/nio/sctp/SctpChannel/CloseDescriptors.java @@ -0,0 +1,211 @@ +/* + * 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 8238274 + * @summary Potential leak file descriptor for SCTP + * @requires (os.family == "linux") + * @run main/othervm/timeout=250 CloseDescriptors + */ + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.lang.management.ManagementFactory; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import com.sun.nio.sctp.SctpChannel; +import com.sun.nio.sctp.SctpServerChannel; + +public class CloseDescriptors { + private static Selector selector; + private static final int LOOP = 10; + private static final int LIMIT_LINES = 3; + private static SelectorThread selThread; + private static boolean finished = false; + + public static void main(String[] args) throws Exception { + if (!Util.isSCTPSupported()) { + System.out.println("SCTP protocol is not supported"); + System.out.println("Test cannot be run"); + return; + } + + List lsofDirs = Arrays.asList("/usr/bin", "/usr/sbin"); + + Optional lsof = lsofDirs.stream() + .map(s -> Paths.get(s, "lsof")) + .filter(f -> Files.isExecutable(f)) + .findFirst(); + if (!lsof.isPresent()) { + System.out.println("Cannot locate lsof in " + lsofDirs); + System.out.println("Test cannot be run"); + return; + } + + try (ServerSocket ss = new ServerSocket(0)) { + int port = ss.getLocalPort(); + + Server server = new Server(port); + server.start(); + + selector = Selector.open(); + + selThread = new SelectorThread(); + selThread.start(); + + // give time for the server and selector to start + Thread.sleep(100); + for (int i = 0 ; i < 100 ; ++i) { + System.out.println(i); + doIt(port); + Thread.sleep(100); + } + System.out.println("end"); + if (!check()) { + cleanup(port); + throw new RuntimeException("Failed: detected unclosed FD."); + } + cleanup(port); + server.join(); + selThread.join(); + } + } + + private static void doIt(int port) throws Exception { + InetSocketAddress sa = new InetSocketAddress("localhost", port); + + for (int i = 0 ; i < LOOP ; ++i) { + System.out.println(" " + i); + try (SctpChannel channel = SctpChannel.open(sa, 1, 1)) { + channel.configureBlocking(false); + + SelectionKey key = selThread.regChannel(channel); + + key.cancel(); + selector.wakeup(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + Thread.sleep(200); + } + } + + private static boolean check() throws Exception { + String vmName = ManagementFactory.getRuntimeMXBean().getName(); + long myPid = Long.parseLong(vmName.split("@")[0]); + ProcessBuilder pb = new ProcessBuilder( + "lsof", "-U", "-a", "-p", Long.toString(myPid)); + pb.redirectErrorStream(true); + Process p = pb.start(); + p.waitFor(); + if (p.exitValue() != 0) { + return false; + } + + boolean result = true; + try (BufferedReader br = new BufferedReader(new InputStreamReader( + p.getInputStream()))) { + int count = 0; + String line = br.readLine(); + while (line != null) { + System.out.println(line); + count++; + if (count > LIMIT_LINES) { + result = false; + } + line = br.readLine(); + } + } + return result; + } + + private static void cleanup(int port) throws IOException { + finished = true; + InetSocketAddress sa = new InetSocketAddress("localhost", port); + SctpChannel channel = SctpChannel.open(sa, 1, 1); + channel.close(); + } + + private static class SelectorThread extends Thread { + private Object lock = new Object(); + private SctpChannel channel; + private SelectionKey key; + + public SelectionKey regChannel(SctpChannel ch) throws Exception { + synchronized (lock) { + channel = ch; + selector.wakeup(); + lock.wait(); + } + return key; + } + + public void run() { + try { + while (!finished) { + selector.select(1000); + synchronized (lock) { + if (channel != null) { + key = channel.register(selector, SelectionKey.OP_READ); + channel = null; + lock.notify(); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + private static class Server extends Thread { + private int port; + + public Server(int port) { this.port = port; } + + public void run() { + try { + SctpServerChannel ss = SctpServerChannel.open(); + InetSocketAddress sa = new InetSocketAddress("localhost", port); + ss.bind(sa); + while (!finished) { + SctpChannel soc = ss.accept(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } +}