Package org.bzdev.net

Class CloseWaitService

java.lang.Object
org.bzdev.net.CloseWaitService

public class CloseWaitService extends Object
Service to remove sockets in a TCP CLOSE_WAIT state. Tests with Java 11 (this may change eventually) show that HttpsServer (or some class that it uses) does not always shut down TCP connections properly, leaving connections in the TCP CLOSE_WAIT state indefinitely. This also leads to an increasing number of HttpsServer threads that are deadlocked. This class provides a work-around for this issue by fordable turning connections that have been sitting in a CLOSED_WAIT state. This will cause a thread to fail, but in the HttpsServer case, among others, and ExecutorService is used to manage threads and can create a new thread if an existing one fails.

This class is trivial to use: one first calls the contracts to set it up, providing the socket used by a server as an argument, plus a timeout and a polling interval. Then the service can be started using the start() method and stopped using the stop() method.

The method start() must be called with root privileges, and the program ss must be installed (making this class Linux-specific). To use this class on systems other than Linux one should use a container or virtual machine to provide a Linux environment, which can be easily done with various "platform as a service" tools such as Docker.

  • Constructor Details

    • CloseWaitService

      public CloseWaitService(int timeout, int interval, InetSocketAddress... saddr)
      Constructor. The socket address must specify a TCP port number larger than 0, but the IP address may be a wildcard address. Multiple wildcard addresses can be provided, each for a different port (duplicates will be ignored).
      Parameters:
      timeout - the timeout in seconds for how long a socket can be in a CLOSE-WAIT state
      interval - the interval at which the network state is polled, expressed in units of seconds
      saddr - the socket address to monitor
  • Method Details

    • start

      public void start()
      Start the service.
    • stop

      public void stop()
      Stop the service.