public synchronized void close()

in nailgun-server/src/main/java/com/facebook/nailgun/NGUnixDomainServerSocket.java [156:178]


  public synchronized void close() throws IOException {
    if (isClosed) {
      throw new IllegalStateException("Socket is already closed");
    }
    try {
      // Close listening socket to unblock a thread calling 'accept()'
      int socketFd = fd.getAndSet(-1);

      // Mac and Linux have different behavior. On Mac, calling 'close()' on socket descriptor
      // will cause `accept()` to throw and unblock (allowing us to finish gracefully), while on
      // Linux it remains blocked. To unblock listening socket on Linux we have to call `shutdown()`
      // first, but on Mac it fails with 'Socket not connected' exception. So we only call shutdown
      // on Linux.
      if (Platform.isLinux()) {
        NGUnixDomainSocketLibrary.shutdown(socketFd, NGUnixDomainSocketLibrary.SHUT_RDWR);
      }

      NGUnixDomainSocketLibrary.close(socketFd);
      isClosed = true;
    } catch (LastErrorException e) {
      throw new IOException(e);
    }
  }