Fixes a race condition leading to a thread stuck in receive() (#135)

* fix: Fixes a race condition leading to a thread

being stuck in AbstractUdpListener.MySocket#receive().

* minor: Braces, cleanup.
This commit is contained in:
bgrozev 2017-11-14 14:40:17 -06:00 committed by bbaldino
parent 610a438dac
commit a2e5ca1d4c
2 changed files with 11 additions and 8 deletions

View File

@ -535,10 +535,10 @@ public abstract class AbstractUdpListener
@Override @Override
public void close() public void close()
{ {
closed = true;
synchronized (queue) synchronized (queue)
{ {
closed = true;
// Wake up any threads still in receive() // Wake up any threads still in receive()
queue.notifyAll(); queue.notifyAll();
} }
@ -546,7 +546,9 @@ public abstract class AbstractUdpListener
// We could be called by the super-class constructor, in which // We could be called by the super-class constructor, in which
// case this.removeAddress is not initialized yet. // case this.removeAddress is not initialized yet.
if (remoteAddress != null) if (remoteAddress != null)
{
AbstractUdpListener.this.sockets.remove(remoteAddress); AbstractUdpListener.this.sockets.remove(remoteAddress);
}
super.close(); super.close();
} }
@ -565,11 +567,13 @@ public abstract class AbstractUdpListener
while (buf == null) while (buf == null)
{ {
if (closed)
throw new SocketException("Socket closed");
synchronized (queue) synchronized (queue)
{ {
if (closed)
{
throw new SocketException("Socket closed");
}
if (queue.isEmpty()) if (queue.isEmpty())
{ {
try try
@ -592,7 +596,9 @@ public abstract class AbstractUdpListener
// XXX Should we use p.setData() here with a buffer of our own? // XXX Should we use p.setData() here with a buffer of our own?
if (pData == null || pData.length < buf.len) if (pData == null || pData.length < buf.len)
{
throw new IOException("packet buffer not available"); throw new IOException("packet buffer not available");
}
System.arraycopy(buf.buffer, 0, pData, 0, buf.len); System.arraycopy(buf.buffer, 0, pData, 0, buf.len);
p.setLength(buf.len); p.setLength(buf.len);

View File

@ -143,9 +143,6 @@ public class SafeCloseDatagramSocket
{ {
super.close(); super.close();
if(inReceiveSyncRoot == null)
return;
synchronized (inReceiveSyncRoot) synchronized (inReceiveSyncRoot)
{ {
boolean interrupted = false; boolean interrupted = false;