Skip to content

Commit

Permalink
Backport d91e227abb94953129adc297fbd456c55bb2ae10
Browse files Browse the repository at this point in the history
  • Loading branch information
ktakakuri committed Jan 24, 2024
1 parent 2f81fb4 commit a16e54c
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 3 deletions.
3 changes: 2 additions & 1 deletion jdk/src/solaris/classes/sun/nio/ch/sctp/SctpChannelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
211 changes: 211 additions & 0 deletions jdk/test/com/sun/nio/sctp/SctpChannel/CloseDescriptors.java
Original file line number Diff line number Diff line change
@@ -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<String> lsofDirs = Arrays.asList("/usr/bin", "/usr/sbin");

Optional<Path> 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();
}
}
}
}

0 comments on commit a16e54c

Please sign in to comment.