src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java
branchunixdomainchannels
changeset 58911 2c777f25cfff
parent 58847 692de65ab293
parent 58900 434329f6f456
--- a/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java	Wed Oct 30 12:25:22 2019 +0000
+++ b/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java	Mon Nov 04 11:25:55 2019 +0000
@@ -409,7 +409,7 @@
     }
 
     /**
-     * Adjust the blocking mode while holding the readLock or writeLock.
+     * Adjusts the blocking mode. readLock or writeLock must already be held.
      */
     private void lockedConfigureBlocking(boolean block) throws IOException {
         assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread();
@@ -420,6 +420,25 @@
     }
 
     /**
+     * Adjusts the blocking mode if the channel is open. readLock or writeLock
+     * must already be held.
+     *
+     * @return {@code true} if the blocking mode was adjusted, {@code false} if
+     *         the blocking mode was not adjusted because the channel is closed
+     */
+    private boolean tryLockedConfigureBlocking(boolean block) throws IOException {
+        assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread();
+        synchronized (stateLock) {
+            if (isOpen()) {
+                IOUtil.configureBlocking(fd, block);
+                return true;
+            } else {
+                return false;
+            }
+        }
+    }
+
+    /**
      * Returns the local address, or null if not bound
      */
     SocketAddress localAddress() {
@@ -852,8 +871,8 @@
                             int n = connectImpl(fd, sa);
                             connected = (n > 0) ? true : finishTimedConnect(nanos);
                         } finally {
-                            // restore socket to blocking mode
-                            lockedConfigureBlocking(true);
+                            // restore socket to blocking mode (if channel is open)
+                            tryLockedConfigureBlocking(true);
                         }
                     } finally {
                         endConnect(true, connected);
@@ -945,8 +964,8 @@
                     try {
                         n = timedRead(b, off, len, nanos);
                     } finally {
-                        // restore socket to blocking mode
-                        lockedConfigureBlocking(true);
+                        // restore socket to blocking mode (if channel is open)
+                        tryLockedConfigureBlocking(true);
                     }
                 } else {
                     // read, no timeout