fix: Fixes a bug which prevents the socket to close.

After the delegate is closed, isClosed() would return true,
which would prevent the DatagramSocket instance (super) from
actually closing and releasing its resources.
This commit is contained in:
Boris Grozev 2017-05-10 16:14:29 -05:00
parent e472f6ac1c
commit b6e5d035e5

View File

@ -115,6 +115,11 @@ public class DelegatingDatagramSocket
*/ */
private long nbSentPackets = 0; private long nbSentPackets = 0;
/**
* Whether this socket has been closed.
*/
private boolean closed = false;
/** /**
* Initializes a new <tt>DelegatingDatagramSocket</tt> instance and binds it * Initializes a new <tt>DelegatingDatagramSocket</tt> instance and binds it
* to any available port on the local host machine. The socket will be * to any available port on the local host machine. The socket will be
@ -282,10 +287,14 @@ public class DelegatingDatagramSocket
@Override @Override
public void close() public void close()
{ {
// We want both #delegate and super to actually get closed (and release
// the FDs which they hold). But super will not close unless isClosed()
// returns false. So we update the #closed flag last.
if (delegate != null) if (delegate != null)
delegate.close(); delegate.close();
super.close(); super.close();
closed = true;
} }
/** /**
@ -610,7 +619,7 @@ public class DelegatingDatagramSocket
@Override @Override
public boolean isClosed() public boolean isClosed()
{ {
return (delegate == null) ? super.isClosed() : delegate.isClosed(); return closed;
} }
/** /**