NServiceBus icon indicating copy to clipboard operation
NServiceBus copied to clipboard

Dealing with a message handler that is unresponsive

Open JustinThirkell opened this issue 13 years ago • 16 comments

Creating issue as per skype discussion with @SimonCropp ...

What happens if a dev writes a msg handler that has Thread.Sleep(forever) in it? Or if the msg handler sync calls a badly behaved service with a stupidly long timeout.

Is there any way to terminate runaway/rogue threads in the host?

When using the DTC this is obviously handled by DTC with a timeout. But I would expect this to be a problem common to all non-DTC brokered transports - would be preferable to have this handled at an NSB level rather than within msg handlers.

JustinThirkell avatar Oct 23 '13 22:10 JustinThirkell

@thirkcircus - Tell dev not to have a forever sleep in the handler? :) Monitor CriticalTime for the endpoints and see why messages take too long to process and identify the stupidly long timeouts and fix it?

indualagarsamy avatar Oct 23 '13 22:10 indualagarsamy

Or if the msg handler sync calls a badly behaved service with a stupidly long timeout.

So if you are calling a 3rd party that may take a long time, then you need to specify a timeout for that 3rd party call, exactly the same as when you calling http and you can(should always) specify a timeout.

What happens if a dev writes a msg handler that has Thread.Sleep(forever) in it?

Wouldn't this scenario be considered a bug and hopefully picked up in testing?

Is there any way to terminate runaway/rogue threads in the host?

No there isn't and I'm not sure that is the responsibility of the framework, there can be situations where this scenario is fine. You can also enable SLAs and keep an eye on CriticalTime performance counter.

johnsimons avatar Oct 23 '13 22:10 johnsimons

BTW, even though DTC has a timeout, the timeout exception is only triggered when the code calls Commit/Rollback!

johnsimons avatar Oct 23 '13 22:10 johnsimons

I concede, there may be a usage for this feature, even thought at this stage I have no idea how we would implement it!

johnsimons avatar Oct 24 '13 00:10 johnsimons

Side note: I would use simon's static type replacer for fody and replace every Thread.Sleep call with a replacer which throws an EvilStuffUsedHereException ;)

Am 24.10.2013 um 00:19 schrieb Justin Thirkell [email protected]:

Creating issue as per skype discussion with @SimonCropp ...

What happens if a dev writes a msg handler that has Thread.Sleep(forever) in it? Or if the msg handler sync calls a badly behaved service with a stupidly long timeout.

Is there any way to terminate runaway/rogue threads in the host?

— Reply to this email directly or view it on GitHub.

danielmarbach avatar Oct 24 '13 06:10 danielmarbach

for some context @danielmarbach is referring to Inoad https://github.com/Fody/Ionad

SimonCropp avatar Oct 24 '13 07:10 SimonCropp

What are the use cases for this? Other than bad developers? In addition to the Critical time, we also do have the EndpointSLAViolationCountdown perf counter as well to indicate to Ops that this handler is taking too long. What advantages do we see in having this feature? So, in the cases where the handler takes too long, rather than get notified, we want to kill that handler? Just trying to understand?

indualagarsamy avatar Oct 24 '13 07:10 indualagarsamy

Doesn't critical time & the perf counter only report back once the message has completed?

chrisbednarski avatar Oct 24 '13 20:10 chrisbednarski

@chrisbednarski - yes, but aren't we talking about long thread sleeps here, or long timeouts on sync calls. eventually they end? I am trying to understand the scenarios where this feature will be useful.

indualagarsamy avatar Oct 24 '13 21:10 indualagarsamy

@indualagarsamy Thread.Sleep(forever) will never end. If there's no timeout that kills the worker thread, the worker thread will be blocked forever. I agree that scenarios are unclear!

chrisbednarski avatar Oct 24 '13 22:10 chrisbednarski

a more likely case is someone has done a while(true) loop and there is a bug in the exit clause. this will never end and may not be picked up in a core review or testing.

SimonCropp avatar Oct 24 '13 22:10 SimonCropp

So to close off my part of the issue - I was asked the question by co-workers and just wasn't sure of the answer. I now have a good-enough answer thanks (discovery -> use the msg-level monitoring provided. fix -> recode and redeploy the service). Whether there is still an actionable issue to come out of this I'm happy to let you guys thrash it out...

JustinThirkell avatar Oct 24 '13 23:10 JustinThirkell

@distantcam applying your knowledge of async/threading is this something we should attempt to solve in NSB?

SimonCropp avatar Oct 18 '14 09:10 SimonCropp

Well, we could wrap calling the handler in a task, and use Task.Wait to timeout the call. All that's needed is a default timeout that could be configurable.

If the handler times out, treat it as an error?

distantcam avatar Oct 20 '14 10:10 distantcam

I would favor going for an async pipeline and async handler

Am 20.10.2014 um 12:10 schrieb Cameron MacFarland [email protected]:

Well, we could wrap calling the handler in a task, and use Task.Wait to timeout the call. All that's needed is a default timeout that could be configurable.

If the handler times out, treat it as an error?

— Reply to this email directly or view it on GitHub.

danielmarbach avatar Oct 21 '14 04:10 danielmarbach

With v6 implementors of handlers have to possibility to use cooperative cancellation provided by the .NET framework for their custom code. In the future, we might even expose the cancellation token source and token provided by the transport for each message received. From the perspective of rogue message handler detection there is not much we can do since each transport has its own way of dealing with long running operations (peek lock, no restriction, transaction timeouts...). Closing this.

danielmarbach avatar Aug 03 '16 11:08 danielmarbach