Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8238274: (sctp) JDK-7118373 is not fixed for SctpChannel #5274

Closed
wants to merge 5 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -543,7 +543,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);
@@ -290,7 +290,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);
@@ -267,7 +267,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())
@@ -0,0 +1,202 @@
/*
* 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 CloseDescriptors
Copy link
Member

@dfuch dfuch Sep 13, 2021

Choose a reason for hiding this comment

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

The test SctpMultiChannel/CloseDescriptors seems to work - at least it doesn't fail all the time - but it's using @run main/othervm. I believe you should make this test run in /othervm mode for more stability. I will test again with your later change and /othervm mode.

Copy link
Author

@masyano masyano Sep 14, 2021

Choose a reason for hiding this comment

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

I pushed a fix to run the test in othervm mode. Please test again.

Copy link
Member

@dfuch dfuch Sep 16, 2021

Choose a reason for hiding this comment

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

Meanwhile I did some experiment and was puzzled by the fact that the new test still failed intermittently - while the corresponding SctpMultiChannel/CloseDescriptors did not. By comparing the two I noticed that the SctpMultiChannel test is introducing some delays at key places. I did the same to the new test - and behold! It stopped failing.

Here are my changes (ignore the /othervm which you already did):

diff --git a/test/jdk/com/sun/nio/sctp/SctpChannel/CloseDescriptors.java b/test/jdk/com/sun/nio/sctp/SctpChannel/CloseDescriptors.java
index 99f9563e016..11e2e52f577 100644
--- a/test/jdk/com/sun/nio/sctp/SctpChannel/CloseDescriptors.java
+++ b/test/jdk/com/sun/nio/sctp/SctpChannel/CloseDescriptors.java
@@ -26,7 +26,7 @@
  * @bug 8238274
  * @summary Potential leak file descriptor for SCTP
  * @requires (os.family == "linux")
- * @run main CloseDescriptors
+ * @run main/othervm CloseDescriptors
  */
 
 import java.io.BufferedReader;
@@ -79,9 +79,12 @@ public class CloseDescriptors {
             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()) {
@@ -110,6 +113,7 @@ public class CloseDescriptors {
             catch (Exception ex) {
                 ex.printStackTrace();
             }
+            Thread.sleep(200);
         }
     }
 
@@ -199,4 +203,3 @@ public class CloseDescriptors {
         }
     }
 }

Copy link
Author

@masyano masyano Sep 22, 2021

Choose a reason for hiding this comment

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

Thank you for your help. I applied your suggested fix and it works fine. But it takes about 4 minutes to be finished. So I added a timeout parameter to 250.

*/

import java.io.BufferedReader;
import java.io.InputStreamReader;
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.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 = 2;
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 = List.of("/usr/bin", "/usr/sbin");
Optional<Path> lsof = lsofDirs.stream()
.map(s -> Path.of(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();

for (int i = 0 ; i < 100 ; ++i) {
System.out.println(i);
doIt(port);
}
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();
}
}
}

private static boolean check() throws Exception {
long myPid = ProcessHandle.current().pid();
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();
}
}
}
}