Skip to content

Commit 20217ef

Browse files
committed
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 <[email protected]>
1 parent a8becd5 commit 20217ef

File tree

8 files changed

+64
-11
lines changed

8 files changed

+64
-11
lines changed

src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
* questions.
2424
*/
2525

26+
/*
27+
* ===========================================================================
28+
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
29+
* ===========================================================================
30+
*/
31+
2632
package sun.nio.ch;
2733

2834
import java.io.FileDescriptor;
@@ -1165,13 +1171,13 @@ protected void implCloseSelectableChannel() throws IOException {
11651171
long reader = readerThread;
11661172
long writer = writerThread;
11671173
if (reader != 0 || writer != 0) {
1168-
nd.preClose(fd);
1169-
11701174
if (reader != 0)
11711175
NativeThread.signal(reader);
11721176
if (writer != 0)
11731177
NativeThread.signal(writer);
11741178

1179+
nd.preClose(fd);
1180+
11751181
// wait for blocking I/O operations to end
11761182
while (readerThread != 0 || writerThread != 0) {
11771183
try {

src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
* questions.
2424
*/
2525

26+
/*
27+
* ===========================================================================
28+
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
29+
* ===========================================================================
30+
*/
31+
2632
package sun.nio.ch;
2733

2834
import java.io.FileDescriptor;
@@ -361,9 +367,10 @@ protected void implCloseSelectableChannel() throws IOException {
361367
assert state == ST_CLOSING;
362368
long th = thread;
363369
if (th != 0) {
364-
nd.preClose(fd);
365370
NativeThread.signal(th);
366371

372+
nd.preClose(fd);
373+
367374
// wait for accept operation to end
368375
while (thread != 0) {
369376
try {

src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
* questions.
2424
*/
2525

26+
/*
27+
* ===========================================================================
28+
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
29+
* ===========================================================================
30+
*/
31+
2632
package sun.nio.ch;
2733

2834
import java.io.FileDescriptor;
@@ -835,14 +841,15 @@ protected void implCloseSelectableChannel() throws IOException {
835841
long reader = readerThread;
836842
long writer = writerThread;
837843
if (reader != 0 || writer != 0) {
838-
nd.preClose(fd);
839-
connected = false; // fd is no longer connected socket
840844

841845
if (reader != 0)
842846
NativeThread.signal(reader);
843847
if (writer != 0)
844848
NativeThread.signal(writer);
845849

850+
nd.preClose(fd);
851+
connected = false; // fd is no longer connected socket
852+
846853
// wait for blocking I/O operations to end
847854
while (readerThread != 0 || writerThread != 0) {
848855
try {

src/java.base/unix/classes/sun/nio/ch/SinkChannelImpl.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
* questions.
2424
*/
2525

26+
/*
27+
* ===========================================================================
28+
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
29+
* ===========================================================================
30+
*/
31+
2632
package sun.nio.ch;
2733

2834
import java.io.FileDescriptor;
@@ -107,8 +113,8 @@ protected void implCloseSelectableChannel() throws IOException {
107113
assert state == ST_CLOSING;
108114
long th = thread;
109115
if (th != 0) {
110-
nd.preClose(fd);
111116
NativeThread.signal(th);
117+
nd.preClose(fd);
112118

113119
// wait for write operation to end
114120
while (thread != 0) {

src/java.base/unix/classes/sun/nio/ch/SourceChannelImpl.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
* questions.
2424
*/
2525

26+
/*
27+
* ===========================================================================
28+
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
29+
* ===========================================================================
30+
*/
2631
package sun.nio.ch;
2732

2833
import java.io.FileDescriptor;
@@ -107,8 +112,8 @@ protected void implCloseSelectableChannel() throws IOException {
107112
assert state == ST_CLOSING;
108113
long th = thread;
109114
if (th != 0) {
110-
nd.preClose(fd);
111115
NativeThread.signal(th);
116+
nd.preClose(fd);
112117

113118
// wait for read operation to end
114119
while (thread != 0) {

src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpChannelImpl.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
* or visit www.oracle.com if you need additional information or have any
2323
* questions.
2424
*/
25+
26+
/*
27+
* ===========================================================================
28+
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
29+
* ===========================================================================
30+
*/
31+
2532
package sun.nio.ch.sctp;
2633

2734
import java.net.InetAddress;
@@ -561,7 +568,6 @@ protected void implConfigureBlocking(boolean block) throws IOException {
561568
@Override
562569
public void implCloseSelectableChannel() throws IOException {
563570
synchronized (stateLock) {
564-
SctpNet.preClose(fdVal);
565571

566572
if (receiverThread != 0)
567573
NativeThread.signal(receiverThread);
@@ -571,6 +577,8 @@ public void implCloseSelectableChannel() throws IOException {
571577

572578
if (!isRegistered())
573579
kill();
580+
581+
SctpNet.preClose(fdVal);
574582
}
575583
}
576584

src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpMultiChannelImpl.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
* or visit www.oracle.com if you need additional information or have any
2323
* questions.
2424
*/
25+
26+
/*
27+
* ===========================================================================
28+
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
29+
* ===========================================================================
30+
*/
31+
2532
package sun.nio.ch.sctp;
2633

2734
import java.net.InetAddress;
@@ -288,8 +295,6 @@ protected void implConfigureBlocking(boolean block) throws IOException {
288295
@Override
289296
public void implCloseSelectableChannel() throws IOException {
290297
synchronized (stateLock) {
291-
SctpNet.preClose(fdVal);
292-
293298
if (receiverThread != 0)
294299
NativeThread.signal(receiverThread);
295300

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

299304
if (!isRegistered())
300305
kill();
306+
307+
SctpNet.preClose(fdVal);
301308
}
302309
}
303310

src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpServerChannelImpl.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
* or visit www.oracle.com if you need additional information or have any
2323
* questions.
2424
*/
25+
26+
/*
27+
* ===========================================================================
28+
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
29+
* ===========================================================================
30+
*/
31+
2532
package sun.nio.ch.sctp;
2633

2734
import java.net.SocketAddress;
@@ -265,11 +272,11 @@ protected void implConfigureBlocking(boolean block) throws IOException {
265272
@Override
266273
public void implCloseSelectableChannel() throws IOException {
267274
synchronized (stateLock) {
268-
SctpNet.preClose(fdVal);
269275
if (thread != 0)
270276
NativeThread.signal(thread);
271277
if (!isRegistered())
272278
kill();
279+
SctpNet.preClose(fdVal);
273280
}
274281
}
275282

0 commit comments

Comments
 (0)