Async events firing out of expected sequence.
Obviously I've got some assumptions wrong here but my thinking was that in these sets of methods would still fire in sequence:
this.service.StopAsync() calls...
And my thinking was that
StopService
would halt on that await
until StopAsync
returned, but it must not be so because the OnMessage
is firing after the dispose methods. So... something isn't right. I can probably turn StopAsync
synchronous but I'd rather figure out WTF is going on here.21 Replies
@maxmahem Does it happen if
this.cancellationTokenSource
is not null too?
Also, are you saying OnMessage
is getting called after, or that the result of OnMessage
is received after? What are you basing that on?cancellationTokenSource
always has a value, the guard is just for completeness.
OnMessage
writes to logFileStream
and I'm getting an exception that the stream is disposed. Although... hmm.
I think I know what the problem was. I've changed the code here for a different reason, let me retest.Often there is some pooling in the logging. So it might run deferred, then crash.
hmm... nope. I was calling OnMessage in a different thread for other reasons previously, thought that might be the issue but I've changed that and the problem persists.
Logging is still pretty basic ATM so nothing should be defered here:
What's logStream?
FileStream
sorry, I lied. StreamWriter
.Try flushing it after every writeline, just for fun (to remove that from the equation)
no joy
I'm clueless then, sorry
Can you put a breakpoint with an "on count" condition to stop if there is a 2nd call to
StopService
?
Also, what is your exception exactly? (just to double check)ObjectDisposedException
(no double entry into StopService
either)
It's getting triggered by this line here:
are you sure StopAsync is not being called twice?
100%
@maxmahem
Because what's disposed is
this.listener
right?
(and it's the .LocalEndpoint
that throws)no, that's the um
TcpListner
different thingBut isn't it what throws the exception?
Change
OnMessage(this.listener.LocalEndpoint, $"{Name} Service Stopped.");
to
If you inspect, you'll see that this.listener
is what's already disposed.no no, sorry for the confusion.
OnMessage
is just where it bubbles out from. The exception originates from there. It's nothing to do with the listner, I am sure. The exception tells me it's because of the Stream being disposed.Oh, ok
Do you know if it's the streamwriter or the underlying stream that's being disposed? (I know you said Stream, but I just want to confirm)
I think its still due to threading shennigangs. I did reformulate this, but looking deeper the call is still being processed on a different thread so... let me poke around at it.
Alright then!
I'll have to go to bed anywyas, it's already 2:21
hmmm... no I thought it might be related to this, but it's not:
(same error if called without the Dispatcher)
can you put breakpoints or Debug.WriteLine entering and exiting both
StopAsync
and OnMessage
to check that the order of the operations is what you expect?