[jdk17u-dev] RFR: 8358764: (sc) SocketChannel.close when thread blocked in read causes connection to be reset (win)

Martin Doerr mdoerr at openjdk.org
Wed Oct 22 13:29:29 UTC 2025


On Tue, 21 Oct 2025 10:12:17 GMT, Goetz Lindenmaier <goetz at openjdk.org> wrote:

> I backport this for parity with 17.0.18-oracle.
> 
> Needed resolve and adaptions:
> 
> Net.java:  resolved static initializer.
> SocketChannelImpl.java: In implCloseBlockingMode(), the code guarded by the new condition is not in 17. Omitted.
> Net.c: Copyright.
> 
> The test exercises platform and virtual threads.
> I simplified this to what is supported in 17.
> See extra commit.

It is true that the affected part of `implCloseBlockingMode()` has been introduced with Loom, but it looks relevant without Loom, too. Note that `implCloseNonBlockingMode()` handles `StandardSocketOptions.SO_LINGER`, too.

Without this change, your PR basically only adds a test because `shouldShutdownWriteBeforeClose()` is unused.


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 a771d998b24..09c9ed4df99 100644
--- a/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java
+++ b/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java
@@ -1019,7 +1019,19 @@ class SocketChannelImpl
     private void implCloseBlockingMode() throws IOException {
         synchronized (stateLock) {
             assert state < ST_CLOSING;
+            boolean connected = (state == ST_CONNECTED);
             state = ST_CLOSING;
+
+            if (connected && Net.shouldShutdownWriteBeforeClose()) {
+                // shutdown output when linger interval not set to 0
+                try {
+                    var SO_LINGER = StandardSocketOptions.SO_LINGER;
+                    if ((int) Net.getSocketOption(fd, SO_LINGER) != 0) {
+                        Net.shutdown(fd, Net.SHUT_WR);
+                    }
+                } catch (IOException ignore) { }
+            }
+
             if (!tryClose()) {
                 long reader = readerThread;
                 long writer = writerThread;

-------------

PR Comment: https://git.openjdk.org/jdk17u-dev/pull/4076#issuecomment-3432362261


More information about the jdk-updates-dev mailing list