Datagram socket leak (original) (raw)

Chris Hegarty chris.hegarty at oracle.com
Mon Aug 29 10:56:12 PDT 2011


[take two!]

Tom,

This specific area of code was changed recently due to CR 7035556 [1], changeset [2], and this issue was discussed during the code review [3].

Essentially, bind() already closes the impl internally before throwing the exception. Does this resolve the issue for you? Or do you still see potential to leak?

-Chris

[1] http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7035556 [2] http://hg.openjdk.java.net/jdk8/tl/jdk/rev/07a12583d4ea [3] http://mail.openjdk.java.net/pipermail/net-dev/2011-July/003318.html

On 08/29/11 03:27 PM, Salter, Thomas A wrote:

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



More information about the net-dev mailing list