Datagram socket leak

Salter, Thomas A Thomas.Salter at unisys.com
Mon Aug 29 07:27:04 PDT 2011


There appears to be a socket leak in both DatagramSocket and MulticastSocket constructors.  Both classes have constructors that create a socket and then attempt to bind.  The bind can fail with a variety of exceptions none of which are caught by the constructor.  Thus, the actual system socket that was allocated by impl.create() is never closed.

My fix was to wrap a try-finally around the bind call and call close() if isBound is false.

    public DatagramSocket(SocketAddress bindaddr) throws SocketException {
        // create a datagram socket.
        createImpl();
        if (bindaddr != null) {
          try {
            bind(bindaddr);
          } finally {
              if( !isBound() )
                  close();
          }
        }
    }


Tom Salter
Unisys Corporation





-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/net-dev/attachments/20110829/0160b157/attachment.html 


More information about the net-dev mailing list