From 20217ef4ce4d59133f7605e93312e1bf0c7ee2bd Mon Sep 17 00:00:00 2001 From: "Shruthi.Shruthi1" Date: Thu, 19 Jan 2023 21:00:09 -0800 Subject: [PATCH] Hang in PreClose method during dup2 system call During fcntl, dup2 system calls if read/write happens then PreClose method gets hang. This issue is not seen if we Signal/Kill the thread first and then call the preClose method. Signed-off-by: Shruthi.Shruthi1 --- .../share/classes/sun/nio/ch/DatagramChannelImpl.java | 10 ++++++++-- .../classes/sun/nio/ch/ServerSocketChannelImpl.java | 9 ++++++++- .../share/classes/sun/nio/ch/SocketChannelImpl.java | 11 +++++++++-- .../unix/classes/sun/nio/ch/SinkChannelImpl.java | 8 +++++++- .../unix/classes/sun/nio/ch/SourceChannelImpl.java | 7 ++++++- .../unix/classes/sun/nio/ch/sctp/SctpChannelImpl.java | 10 +++++++++- .../classes/sun/nio/ch/sctp/SctpMultiChannelImpl.java | 11 +++++++++-- .../sun/nio/ch/sctp/SctpServerChannelImpl.java | 9 ++++++++- 8 files changed, 64 insertions(+), 11 deletions(-) diff --git a/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java b/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java index fd594c4b725..f88c774e53a 100644 --- a/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java @@ -23,6 +23,12 @@ * questions. */ +/* + * =========================================================================== + * (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved + * =========================================================================== + */ + package sun.nio.ch; import java.io.FileDescriptor; @@ -1165,13 +1171,13 @@ protected void implCloseSelectableChannel() throws IOException { long reader = readerThread; long writer = writerThread; if (reader != 0 || writer != 0) { - nd.preClose(fd); - if (reader != 0) NativeThread.signal(reader); if (writer != 0) NativeThread.signal(writer); + nd.preClose(fd); + // wait for blocking I/O operations to end while (readerThread != 0 || writerThread != 0) { try { diff --git a/src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java b/src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java index 0d002c7054e..129ec77bb32 100644 --- a/src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java @@ -23,6 +23,12 @@ * questions. */ +/* + * =========================================================================== + * (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved + * =========================================================================== + */ + package sun.nio.ch; import java.io.FileDescriptor; @@ -361,9 +367,10 @@ protected void implCloseSelectableChannel() throws IOException { assert state == ST_CLOSING; long th = thread; if (th != 0) { - nd.preClose(fd); NativeThread.signal(th); + nd.preClose(fd); + // wait for accept operation to end while (thread != 0) { try { diff --git a/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java b/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java index a163d2088c7..b91f527e8e2 100644 --- a/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java @@ -23,6 +23,12 @@ * questions. */ +/* + * =========================================================================== + * (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved + * =========================================================================== + */ + package sun.nio.ch; import java.io.FileDescriptor; @@ -835,14 +841,15 @@ protected void implCloseSelectableChannel() throws IOException { long reader = readerThread; long writer = writerThread; if (reader != 0 || writer != 0) { - nd.preClose(fd); - connected = false; // fd is no longer connected socket if (reader != 0) NativeThread.signal(reader); if (writer != 0) NativeThread.signal(writer); + nd.preClose(fd); + connected = false; // fd is no longer connected socket + // wait for blocking I/O operations to end while (readerThread != 0 || writerThread != 0) { try { diff --git a/src/java.base/unix/classes/sun/nio/ch/SinkChannelImpl.java b/src/java.base/unix/classes/sun/nio/ch/SinkChannelImpl.java index 2a855290ae0..7a0154069d5 100644 --- a/src/java.base/unix/classes/sun/nio/ch/SinkChannelImpl.java +++ b/src/java.base/unix/classes/sun/nio/ch/SinkChannelImpl.java @@ -23,6 +23,12 @@ * questions. */ +/* + * =========================================================================== + * (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved + * =========================================================================== +*/ + package sun.nio.ch; import java.io.FileDescriptor; @@ -107,8 +113,8 @@ protected void implCloseSelectableChannel() throws IOException { assert state == ST_CLOSING; long th = thread; if (th != 0) { - nd.preClose(fd); NativeThread.signal(th); + nd.preClose(fd); // wait for write operation to end while (thread != 0) { diff --git a/src/java.base/unix/classes/sun/nio/ch/SourceChannelImpl.java b/src/java.base/unix/classes/sun/nio/ch/SourceChannelImpl.java index 99e934e3436..61601e55fdf 100644 --- a/src/java.base/unix/classes/sun/nio/ch/SourceChannelImpl.java +++ b/src/java.base/unix/classes/sun/nio/ch/SourceChannelImpl.java @@ -23,6 +23,11 @@ * questions. */ +/* + * =========================================================================== + * (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved + * =========================================================================== + */ package sun.nio.ch; import java.io.FileDescriptor; @@ -107,8 +112,8 @@ protected void implCloseSelectableChannel() throws IOException { assert state == ST_CLOSING; long th = thread; if (th != 0) { - nd.preClose(fd); NativeThread.signal(th); + nd.preClose(fd); // wait for read operation to end while (thread != 0) { diff --git a/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpChannelImpl.java b/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpChannelImpl.java index f33d105dc69..39af304b88b 100644 --- a/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpChannelImpl.java +++ b/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpChannelImpl.java @@ -22,6 +22,13 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ + +/* + * =========================================================================== + * (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved + * =========================================================================== +*/ + package sun.nio.ch.sctp; import java.net.InetAddress; @@ -561,7 +568,6 @@ protected void implConfigureBlocking(boolean block) throws IOException { @Override public void implCloseSelectableChannel() throws IOException { synchronized (stateLock) { - SctpNet.preClose(fdVal); if (receiverThread != 0) NativeThread.signal(receiverThread); @@ -571,6 +577,8 @@ public void implCloseSelectableChannel() throws IOException { if (!isRegistered()) kill(); + + SctpNet.preClose(fdVal); } } diff --git a/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpMultiChannelImpl.java b/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpMultiChannelImpl.java index 084e5e36859..d14125abc6a 100644 --- a/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpMultiChannelImpl.java +++ b/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpMultiChannelImpl.java @@ -22,6 +22,13 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ + +/* + * =========================================================================== + * (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved + * =========================================================================== +*/ + package sun.nio.ch.sctp; import java.net.InetAddress; @@ -288,8 +295,6 @@ protected void implConfigureBlocking(boolean block) throws IOException { @Override public void implCloseSelectableChannel() throws IOException { synchronized (stateLock) { - SctpNet.preClose(fdVal); - if (receiverThread != 0) NativeThread.signal(receiverThread); @@ -298,6 +303,8 @@ public void implCloseSelectableChannel() throws IOException { if (!isRegistered()) kill(); + + SctpNet.preClose(fdVal); } } diff --git a/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpServerChannelImpl.java b/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpServerChannelImpl.java index 385851acfb0..358827224e5 100644 --- a/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpServerChannelImpl.java +++ b/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpServerChannelImpl.java @@ -22,6 +22,13 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ + +/* + * =========================================================================== + * (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved + * =========================================================================== +*/ + package sun.nio.ch.sctp; import java.net.SocketAddress; @@ -265,11 +272,11 @@ protected void implConfigureBlocking(boolean block) throws IOException { @Override public void implCloseSelectableChannel() throws IOException { synchronized (stateLock) { - SctpNet.preClose(fdVal); if (thread != 0) NativeThread.signal(thread); if (!isRegistered()) kill(); + SctpNet.preClose(fdVal); } }