Odd Event Problem - Handlers not running code??

C

c.w.browne

Hi,

I have a weird problem with events in a multithreaded c# windows
service which acts as middleware between a 3rd party and my company's
payment prcoessing system, and was wondering if anyone can help.

My service essentially has a thread to accept connections, which then
spawns threads to take these connections and service their requests.
These threads then do some work and then raise events which are picked
up by the controlling Server object which logs their progress.

The most basic run of events is for a "ping" type request which is sent
by clients to ascertain my server's availability. The normal sequence
of events is as follows:

1 - server thread accepts new connection
2 - server thread raises "Connection received" event
3 - server thread spawns new thread to handle connection
4 - connection thread reads bytes from socket
5 - connection thread raises "Data Read" event
6 - connection thread decodes message, realises it is a "ping" request
7 - connection thread generates and sends reply
8 - connection thread raises "Data Sent" event
9 - connection closed by client
10 - "Connection closed" event raised by connection thread

during testing today, i noticed that the logging for the "Data Sent"
event was not taking place. I put a break point in in at the start of
the event handling code and found that the event was indeed raised.
Stepping over the only line of code in the handler, that called a
method on the logging object, however, did nothing.

a bit perturbed, i tried this a few times to see if it was something
intermittent - it didnt seem to be. However, when i switched the
logging object from using Debug.WriteLine() to writing out to a file,
the code worked fine.

Thinking that this could have something to do with events being fired
very closely to eachother, i again stepped through the code in the
handler, but gave it some time between the break point being hit and my
starting to step through, and this also worked OK, even using
Debug.WriteLine().

the really strange (and even more worrying) thing is that this is an
adaptation of an existing server which is already in the field, and
seems to be working fine. The only major difference between the two
that i can see is that the new server that isnt working is for a client
system that creates and destroys connections ad hoc, whereas the one
already in the field maintains permanenent connections from its clients
(establishing a connection comes with a fair bit of work, as certain
other objects have to be instantiated, etc, in order to handle
different types of message). They deal with different messaging
protocols as well, but the message concerned is common to the two and
so is handled in the same way (ie: with minimal processing).

i am pretty stuck as to what is causing this - does anyone have any
ideas at all?? Im not sure exactly what the writing to a log file being
OK / Debug.WriteLine() thing tells me, maybe that extra I/O overhead
slows things down enough to give the framework/windows/something else
time to catch up?? Is this even event related (I'm guessing so, but
perhaps it has something to do with load and multithreading)???

i am using events throughout the server, and am now slightly worried
that this server in the field is also quietly neglecting to execute
code, possibly during processing transactions, which would obviously be
pretty bad. So any possible solutions would also be greatly
appreciated.

Unfortunately i am at home now, and so cannot post any example code. If
necessary i am happy to tomorrow, though.

sorry for the long post, btw - thanks for reading this far, if youve
made it.

cheers,

Chris Browne

(ps: if this should have gone to a different NG please let me know and
i can repost somewhere else - i am assuming that this is a framework,
rather than a c# thing...)
 
W

Willy Denoyette [MVP]

method on the logging object, however, did nothing.

Are you sure the logging object instance didn't get disposed of? I mean are
you sure the object reference is still valid and the object didn't get GC'd.
Note that the "nullreference" exception thrown when calling a method on a
"nulled" object will be ignored as your thread will die silently.
The intermittent nature of this issue could be explained by the fact that
the GC kicks in un-deterministically.
Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top