Monday, October 22, 2007

Thread was being aborted exception - possible solutions and unsolved problems

Update February 2016: This post discusses a problem in .Net 2.0 that is more than 8 years old. Perhaps you should look elsewhere. That being said, there are several situations here:
  1. The default execution timeout is 90 in ASP.Net, increase it to whatever you like (in seconds) with <system.web>
       <httpRuntime executionTimeout="VALUE" />
    Also read this: What exactly is Appdomain recycling, since it is likely this applies in a lot more situations where the server decides the app needs to be recycled.
  2. You use a Response.Redirect, Server.Transfer or Response.End directly, which is the method that ultimately throws this exception. Response.Redirect has an optional bool parameter that, when set to false, does not execute Response.End. Response.End itself is obsolete, recommended is the HttpApplication.CompleteRequest method. Read the remarks for Response.End, it is a method exclusively left there as for compatibility with ASP and it is pretty damn awful. See here how to replace Response.End.
  3. The original problem that made me write this post, which was a long running method inside a web service in .Net 2.0, to which I have not found the solution back then. The solution is probably related to AppDomain/Application Pool recycling from the first point and at that time I did not look hard enough at all application pool settings.
  4. Some issues encountered by other readers that were caused by the ASP.Net application writing in its bin folder, causing the automatic recompiling of the app.

Read more about the ThreadAbortException, which is raised when aborting a thread, which in itself is a pretty bad idea. Ultimately, I would say that getting this exception nowadays is a sure sign of bad design.

Now for the original post:

Update: the problem I was facing is still there and with no solution. I must be doing something wrong, but I can't figure out what. It involves a web service in NET 2.0, added with Web Reference in another app. The service has a method that takes a lot of time. If I do it synchronously it works, depending on the Timeout property of the web service proxy and the executionTimeout setting in the web.config. If I do it asynchronously, though, it doesn't work! If it takes too long it just kills the thread. The Timeout property of the web service doesn't even count in async calls. My solution was to call the method synchronously and be done with it. If anyone has an answer for this, please let me know!

(Oh, but if you mean to tell me that an asynchronous operation in a web service should not take long and that it is bad design and so on and so on, don't let me know!)

The single most related article to my problem that I found on the net is this: timeout problem with webservice and suggests a client problem, but no solution other than asynchronously calling the synchronous call to the web service, and no explanation to the underlying problem.

As for the regular solution for Thread being aborted, it actually it's not my solution, it was given in a forum by a guy with the nickname dstefanov, but I stole it with impunity :)

Here it is:
ThreadAbortException happens when the thread serving the request doesn't finish for a long time. The ASP.NET hosting process aborts this thread after certain time. You can adjust that time with:

<httpRuntime executionTimeout="600" />

By default the executionTimeout="90" seconds.

This applies to ASP.Net web apps, including web services, so also to console or windows forms applications that use web services. My own personal favourite is 86400, the number of seconds in a day.

Thanks dstefanov!


Anonymous said...

Did you ever find a solution to this? I'm running into the exact same issue. I make a request to a web service and the web service threads out several long running processes. After about 5 or 10 minutes though, it errors out regardless of the execution timeout I set.

Siderite said...

Actually, I am building a windows app that connects to a web service. I just create a new thread (with a Mutex object to make sure there is no other thread doing the same thine) and inside I access the web service synchronously. And it works! no matter how long it takes.

Also a good info I found is here, although I don't know if it applies: You should look up the ProtocolVersion and the KeepAlive properties of a web request.

brendan richards said...

I had a very similar issue. In my case, the process launched as a background thread under a web service was writing a file to somewhere under the bin folder - causing the app pool to be restarted.
Yes, I did slap myself once I realised what I was doing!

Anonymous said...

You are a life saver!

milind mane said...

it works

Carlos said...

Also saved my life! My app was being recompiled! Thanks!

Pradeep Motar said... works!!!!!!!

Anonymous said...

Goog job ! It works fine !

TheRunningNobody said...

Alleluia man ! ... that's an old thread, but it still saves lives !

Anonymous said...

You are awesome! Thanks a ton. I was in too much stress, you saved my life