Tries to prevent a race condition while closing sockets.

This commit is contained in:
emcho 2010-02-03 20:31:29 +00:00
parent 8fda55b91b
commit 808fdc4af2

View File

@ -40,6 +40,12 @@ class Connector
*/ */
private DatagramSocket sock; private DatagramSocket sock;
/**
* The object that we use to lock socket operations (since the socket itself
* is often null)
*/
private final Object sockLock = new Object();
/** /**
* A flag that is set to false to exit the message processor. * A flag that is set to false to exit the message processor.
*/ */
@ -112,7 +118,22 @@ class Connector
byte message[] = new byte[bufsize]; byte message[] = new byte[bufsize];
DatagramPacket packet = new DatagramPacket(message, bufsize); DatagramPacket packet = new DatagramPacket(message, bufsize);
sock.receive(packet); DatagramSocket localSock;
synchronized (sockLock)
{
if (!isRunning)
return;
localSock = this.sock;
}
localSock.receive(packet);
//get lost if we are no longer running.
if(!isRunning)
return;
//get lost if we are no longer running. //get lost if we are no longer running.
if(!isRunning) if(!isRunning)
@ -176,8 +197,11 @@ class Connector
*/ */
protected void stop() protected void stop()
{ {
this.sock = null; synchronized(sockLock)
this.isRunning = false; {
this.isRunning = false;
this.sock = null;
}
} }
/** /**