< prev index next >

src/java.base/share/classes/java/net/ServerSocket.java

Print this page
*** 25,10 ***
--- 25,11 ---
  
  package java.net;
  
  import java.io.FileDescriptor;
  import java.io.IOException;
+ import java.io.InterruptedIOException;
  import java.nio.channels.ServerSocketChannel;
  import java.util.Objects;
  import java.util.Set;
  import java.util.Collections;
  

*** 481,22 ***
       * @see #getLocalPort()
       * @see #bind(SocketAddress)
       * @see SecurityManager#checkConnect
       * @since 1.4
       */
- 
      public SocketAddress getLocalSocketAddress() {
          if (!isBound())
              return null;
          return new InetSocketAddress(getInetAddress(), getLocalPort());
      }
  
      /**
       * Listens for a connection to be made to this socket and accepts
       * it. The method blocks until a connection is made.
       *
!      * <p>A new Socket {@code s} is created and, if there
       * is a security manager,
       * the security manager's {@code checkAccept} method is called
       * with {@code s.getInetAddress().getHostAddress()} and
       * {@code s.getPort()}
       * as its arguments to ensure the operation is allowed.
--- 482,26 ---
       * @see #getLocalPort()
       * @see #bind(SocketAddress)
       * @see SecurityManager#checkConnect
       * @since 1.4
       */
      public SocketAddress getLocalSocketAddress() {
          if (!isBound())
              return null;
          return new InetSocketAddress(getInetAddress(), getLocalPort());
      }
  
      /**
       * Listens for a connection to be made to this socket and accepts
       * it. The method blocks until a connection is made.
       *
!      * <p> For the system-default socket implementation at least, if a
+      * {@linkplain Thread#isVirtual() virtual thread} blocked in {@code accept}
+      * is {@linkplain Thread#interrupt() interrupted} then the socket is closed
+      * and {@link SocketException} is thrown with the interrupt status set.
+      *
+      * <p> A new Socket {@code s} is created and, if there
       * is a security manager,
       * the security manager's {@code checkAccept} method is called
       * with {@code s.getInetAddress().getHostAddress()} and
       * {@code s.getPort()}
       * as its arguments to ensure the operation is allowed.

*** 670,11 ***
       */
      private void implAccept(SocketImpl si) throws IOException {
          assert !(si instanceof DelegatingSocketImpl);
  
          // accept a connection
!         impl.accept(si);
  
          // check permission, close SocketImpl/connection if denied
          @SuppressWarnings("removal")
          SecurityManager sm = System.getSecurityManager();
          if (sm != null) {
--- 675,22 ---
       */
      private void implAccept(SocketImpl si) throws IOException {
          assert !(si instanceof DelegatingSocketImpl);
  
          // accept a connection
!         try {
+             impl.accept(si);
+         } catch (SocketTimeoutException e) {
+             throw e;
+         } catch (InterruptedIOException e) {
+             Thread thread = Thread.currentThread();
+             if (thread.isVirtual() && thread.isInterrupted()) {
+                 close();
+                 throw new SocketException("Closed by interrupt");
+             }
+             throw e;
+         }
  
          // check permission, close SocketImpl/connection if denied
          @SuppressWarnings("removal")
          SecurityManager sm = System.getSecurityManager();
          if (sm != null) {
< prev index next >