Skip to content

Commit

Permalink
Merge pull request #386 from shruacha1234/PreClose_hang
Browse files Browse the repository at this point in the history
Avoid hang in preClose method during dup2 system call
  • Loading branch information
pshipton authored Oct 1, 2024
2 parents 94ec587 + 605ea21 commit d3956a6
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 6 deletions.
31 changes: 31 additions & 0 deletions closed/src/java.base/share/classes/sun/net/util/AIX.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* ===========================================================================
* (c) Copyright IBM Corp. 2024, 2024 All Rights Reserved
* ===========================================================================
*
* 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.
*
* IBM designates this particular file as subject to the "Classpath" exception
* as provided by IBM in the LICENSE file that accompanied this code.
*
* 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, see <http://www.gnu.org/licenses/>.
*
* ===========================================================================
*/
package sun.net.util;

import sun.security.action.GetPropertyAction;

public final class AIX {
// Flag indicating whether the operating system is AIX.
public static final boolean isAIX = "AIX".equals(GetPropertyAction.privilegedGetProperty("os.name"));
}
12 changes: 11 additions & 1 deletion 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. 2024, 2024 All Rights Reserved
* ===========================================================================
*/

package sun.nio.ch;

import java.io.FileDescriptor;
Expand Down Expand Up @@ -72,6 +78,7 @@
import jdk.internal.ref.CleanerFactory;
import sun.net.ResourceManager;
import sun.net.ext.ExtendedSocketOptions;
import sun.net.util.AIX;
import sun.net.util.IPAddressUtil;

/**
Expand Down Expand Up @@ -1732,11 +1739,14 @@ private void implCloseBlockingMode() throws IOException {
long reader = readerThread;
long writer = writerThread;
if (reader != 0 || writer != 0) {
nd.preClose(fd);
if (!AIX.isAIX)
nd.preClose(fd);
if (reader != 0)
NativeThread.signal(reader);
if (writer != 0)
NativeThread.signal(writer);
if (AIX.isAIX)
nd.preClose(fd);
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
* questions.
*/

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

package sun.nio.ch;

import java.io.FileDescriptor;
Expand Down Expand Up @@ -56,6 +62,7 @@
import sun.net.PlatformSocketImpl;
import sun.net.ResourceManager;
import sun.net.ext.ExtendedSocketOptions;
import sun.net.util.AIX;
import sun.net.util.SocketExceptions;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
Expand Down Expand Up @@ -909,13 +916,16 @@ protected void close() throws IOException {
// then the socket is pre-closed and the thread(s) signalled. The
// last thread will close the file descriptor.
if (!tryClose()) {
nd.preClose(fd);
if (!AIX.isAIX)
nd.preClose(fd);
long reader = readerThread;
if (reader != 0)
NativeThread.signal(reader);
long writer = writerThread;
if (writer != 0)
NativeThread.signal(writer);
if (AIX.isAIX)
nd.preClose(fd);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
* questions.
*/

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

package sun.nio.ch;

import java.io.FileDescriptor;
Expand Down Expand Up @@ -57,6 +63,7 @@

import sun.net.NetHooks;
import sun.net.ext.ExtendedSocketOptions;
import sun.net.util.AIX;

/**
* An implementation of ServerSocketChannels
Expand Down Expand Up @@ -583,8 +590,11 @@ private void implCloseBlockingMode() throws IOException {
if (!tryClose()) {
long th = thread;
if (th != 0) {
nd.preClose(fd);
if (!AIX.isAIX)
nd.preClose(fd);
NativeThread.signal(th);
if (AIX.isAIX)
nd.preClose(fd);
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion 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. 2024, 2024 All Rights Reserved
* ===========================================================================
*/

package sun.nio.ch;

import java.io.FileDescriptor;
Expand Down Expand Up @@ -62,6 +68,7 @@
import sun.net.ConnectionResetException;
import sun.net.NetHooks;
import sun.net.ext.ExtendedSocketOptions;
import sun.net.util.AIX;
import sun.net.util.SocketExceptions;

/**
Expand Down Expand Up @@ -1014,11 +1021,14 @@ private void implCloseBlockingMode() throws IOException {
long reader = readerThread;
long writer = writerThread;
if (reader != 0 || writer != 0) {
nd.preClose(fd);
if (!AIX.isAIX)
nd.preClose(fd);
if (reader != 0)
NativeThread.signal(reader);
if (writer != 0)
NativeThread.signal(writer);
if (AIX.isAIX)
nd.preClose(fd);
}
}
}
Expand Down
13 changes: 12 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. 2024, 2024 All Rights Reserved
* ===========================================================================
*/

package sun.nio.ch;

import java.io.FileDescriptor;
Expand All @@ -37,6 +43,8 @@
import java.util.Objects;
import java.util.concurrent.locks.ReentrantLock;

import sun.net.util.AIX;

class SinkChannelImpl
extends Pipe.SinkChannel
implements SelChImpl
Expand Down Expand Up @@ -123,8 +131,11 @@ private void implCloseBlockingMode() throws IOException {
if (!tryClose()) {
long th = thread;
if (th != 0) {
nd.preClose(fd);
if (!AIX.isAIX)
nd.preClose(fd);
NativeThread.signal(th);
if (AIX.isAIX)
nd.preClose(fd);
}
}
}
Expand Down
13 changes: 12 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,12 @@
* questions.
*/

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

package sun.nio.ch;

import java.io.FileDescriptor;
Expand All @@ -37,6 +43,8 @@
import java.util.Objects;
import java.util.concurrent.locks.ReentrantLock;

import sun.net.util.AIX;

class SourceChannelImpl
extends Pipe.SourceChannel
implements SelChImpl
Expand Down Expand Up @@ -123,8 +131,11 @@ private void implCloseBlockingMode() throws IOException {
if (!tryClose()) {
long th = thread;
if (th != 0) {
nd.preClose(fd);
if (!AIX.isAIX)
nd.preClose(fd);
NativeThread.signal(th);
if (AIX.isAIX)
nd.preClose(fd);
}
}
}
Expand Down

0 comments on commit d3956a6

Please sign in to comment.