I have a web site which continuously reads data from another site and plots that information on a map. I have a socket started as
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
ReceiveCallback reads the data and dumps it in a Queue for another thread to analyse and then sets the waithandle to start that thread.
readBytesWaitHandle.Set();
Very rarely I am getting the following error
System.Threading.ThreadAbortException: Thread was being aborted.
at System.Threading.WaitHandle.WaitOneNative(SafeHandle waitableSafeHandle, UInt32 millisecondsTimeout, Boolean hasThreadAffinity, Boolean exitContext)
at System.Threading.WaitHandle.InternalWaitOne(SafeHandle waitableSafeHandle, Int64 millisecondsTimeout, Boolean hasThreadAffinity, Boolean exitContext)
at System.Threading.WaitHandle.WaitOne(TimeSpan timeout, Boolean exitContext)
at WebAPRS.PacketListener.readBytesBuffer() in c:\Users\Alan\Documents\Visual Studio 2013\Projects\WebAPRS\WebAPRS\PacketListener.cs:line 274
The question is - which thread is aborting? The one running ReceiveCallback
or the one waiting on
ReceiveCallback
to set the wait handle.
Thanks
Alan
Edit Here is the code for ReceiveCallback
// run by the client thread (which is implicitly created)
private void ReceiveCallback(IAsyncResult ar)
{
try
{
if (restarting) return;
// Retrieve the state object and the client socket
// from the asynchronous state object.
timeSpanSinceLastBytes = DateTime.Now - timeSinceLastBytes;
timeSinceLastBytes = DateTime.Now;
var state = (StateObject)ar.AsyncState;
if (!state.workSocket.Connected)
{
reStartClient("Error: state.workSocket.Connected =false - Restarting");
return;
}
var client = state.workSocket;
// Read data from the remote device.
var bytesRead = client.EndReceive(ar);
if (bytesRead == 0)
{
reStartClient("ERROR: bytes==0 - Restarting");
return;
}
// quickly store the buffer
storeBytes buff = new storeBytes(state.buffer, bytesRead);
byteQueue.Enqueue(buff);
string data = Encoding.ASCII.GetString(state.buffer, 0, bytesRead);
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
// setoff the readBytes Thread
readBytesWaitHandle.Set();
}
catch(Exception e)
{
reStartClient("ReceiveCallback failed : " + e.ToString());
}
}
You will see I have tried to catch every every eventuality I can think of. It often fails with zero bytes read and is restarted OK. (I check to see that the restart process is not ongoing at the beginning)
The catch block is not catching this error. My site was just dying (maybe once every 12 hours) so i have another thread running that checks the health of the thread reading the output from ReceiveCallback every 30secs and it printed out the error message above. The code for that is :
private void checkRunning(object state)
{
if(! ((packetListener.readBytes.ThreadState == ThreadState.Running)||(packetListener.readBytes.ThreadState == ThreadState.WaitSleepJoin)))
{
packetListener.appendToDebugFile("readBytes thread not running "+packetListener.readBytes.ThreadState.ToString());
}
if(! ((packetListener.readStrings.ThreadState == ThreadState.Running) || (packetListener.readStrings.ThreadState == ThreadState.WaitSleepJoin)))
{
packetListener.appendToDebugFile("readStrings thread not running " + packetListener.readStrings.ThreadState.ToString());
}
if (packetListener.timeSpanSinceLastBytes > TimeSpan.FromSeconds(30))
{
packetListener.appendToDebugFile("bytes not seen for " + packetListener.timeSpanSinceLastBytes.ToString());
}
packetListener.appendToDebugFile("Threads running OK");
}