Skip to content

Commit

Permalink
Hang in PreClose method during dup2 system call
Browse files Browse the repository at this point in the history
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 <Shruthi.Shruthi1@ibm.com>
  • Loading branch information
shruacha1234 committed Jan 20, 2023
1 parent a8becd5 commit 20217ef
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 11 deletions.
10 changes: 8 additions & 2 deletions src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
* questions.
*/

/*
* ===========================================================================
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
* ===========================================================================
*/

package sun.nio.ch;

import java.io.FileDescriptor;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
* questions.
*/

/*
* ===========================================================================
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
* ===========================================================================
*/

package sun.nio.ch;

import java.io.FileDescriptor;
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 9 additions & 2 deletions src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
* questions.
*/

/*
* ===========================================================================
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
* ===========================================================================
*/

package sun.nio.ch;

import java.io.FileDescriptor;
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion src/java.base/unix/classes/sun/nio/ch/SinkChannelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
* questions.
*/

/*
* ===========================================================================
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
* ===========================================================================
*/

package sun.nio.ch;

import java.io.FileDescriptor;
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion src/java.base/unix/classes/sun/nio/ch/SourceChannelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
* questions.
*/

/*
* ===========================================================================
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
* ===========================================================================
*/
package sun.nio.ch;

import java.io.FileDescriptor;
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 9 additions & 1 deletion src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpChannelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -571,6 +577,8 @@ public void implCloseSelectableChannel() throws IOException {

if (!isRegistered())
kill();

SctpNet.preClose(fdVal);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -298,6 +303,8 @@ public void implCloseSelectableChannel() throws IOException {

if (!isRegistered())
kill();

SctpNet.preClose(fdVal);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 20217ef

Please sign in to comment.