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

Hang in PreClose method during dup2 system call #605

Open
wants to merge 1 commit into
base: openj9
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Comment on lines -110 to +116
Copy link
Member

Choose a reason for hiding this comment

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

I think we need a much more detailed description of why the original order is wrong and why it should apply to all "unix" platforms. Further, the same order (preClose then signal) also exists in each of these files

src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java
src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java
src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java
src/java.base/unix/classes/sun/nio/ch/SinkChannelImpl.java
src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpChannelImpl.java
src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpMultiChannelImpl.java
src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpServerChannelImpl.java

Please either correct each of those instances or explain why they don't need similar treatment.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, I think you've convinced me that reordering those calls makes sense. Other threads that might retry their read or write operation will fail because the channel has been closed (notice assert !isOpen(); on line 823.

However, you haven't explained why the same pattern that exists in the classes I listed above doesn't need correcting.

Copy link
Contributor Author

@shruacha1234 shruacha1234 Jan 25, 2023

Choose a reason for hiding this comment

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

@keithc-ca
The changes are applicable to the files you have listed. I have made the changes to those files


// 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