Spawning threads in ASP.NET can be dangerous

In my earlier blog post about the dangers of using Response.TransmitFile, I gave an example of a workaround involving spawning a new thread in the ASP.NET page. While this does solve the issue at hand, it presents us with a new way to kill our application even quicker than last.

Usually when an uncaught exception occurs in an ASP.NET application, we will be presented with a "friendly" error message like the one below:

Image: caughtexception
While there is an overhead of exceptions being thrown, they're not directly dangerous and will at worst affect scalability (ignoring the actual reason of the exception being thrown). The problem is that ASP.NET will only catch exceptions on the processing thread. That means, if you spawn a new thread and an exception is thrown (and is not caught inside the thread itself), it will propagate and eventually crash the w3wp.exe process.

Safe
protected void Page_Load(object sender, EventArgs e)
{
	Response.Write("CAN HAZ W3WP.EXE?");

	throw new Exception("I'll will be caught by ASP.NET :D");
}

Unsafe, will crash w3wp.exe
protected void Page_Load(object sender, EventArgs e)
{
	Response.Write("CAN HAZ W3WP.EXE?");

	new Thread(delegate()
	{
		throw new Exception("I'll not be caught by ASP.NET :(");
	}).Start();
}

There are several repercussions of the w3wp.exe crashing. There's a major overhead in spawning a new w3wp.exe process on the next request, you will loose all session (if you're using inprocess session storage), application and cache state. If you have error reporting turned on, you furthermore also see the "DW20.exe" process running and taking up 100% CPU for a significant amount of time (depending on w3wp.exe memory usage, etc) - if this happens often, you might have a large amount of DW20.exe error reporting processes running, effectively crashing your server.

So how do we avoid this? Simple, make sure all code in spawned threads is handling exceptions:

protected void Page_Load(object sender, EventArgs e)
{
	Response.Write("CAN HAZ W3WP.EXE?");

	new Thread(delegate()
	{
		try
		{
			throw new Exception("I'll be caught by our own exception handler :)");
		}
		catch
		{
			Response.Write("What doesn't kill me will make me stronger!");
		}
	}).Start();
}

If you're experiencing this issue, you will see errors in the System event log like this one:

A process serving application pool 'DefaultAppPool' terminated unexpectedly. The process id was '708'. THe process exit code was '0xe0434f4d'.

And like this one in the Application log:

EventType clr20r3, P1 w3wp.exe, P2 6.0.3790.3959, P3 45d6968e, P4 crashw3wp, P5 1.0.0.0, P6 47f94ca4, P7 3, P8 b, P9 system.exception, P10 NIL.

Tess has a really great post on how to debug an unknown cause of the crash.

This issue is relevant to all flavors of Windows and all versions of IIS & .NET.

Downloads
CrashW3WP.zip - Demo project

kick it on DotNetKicks.com


Comments

Kitchen Utensils | Jun 12th, 2010, 1:23 PM

The message presented here in the article is very informative and it has shown the message step by step to understand the other people to preventing their own application by itself and well easily managed.
I would like give a big thanks to the author for sharing with us a nice info.
Keep up the great work....

washington dc web design | Jun 13th, 2010, 6:05 AM

I would like give a big thanks to the author for sharing with us a nice info.
Keep up the great work....

simulation assurance auto | Jul 5th, 2010, 4:55 AM

I really loved reading your blog. It was very well authored and easy to understand. Unlike additional blogs I have read which are really not good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he enjoyed it as well!

injury lawyer | Jul 10th, 2010, 11:55 AM

Really interesting! I really like this series. Although its a bit confusing & I'd have to read it a few times, I actually feel a bit smarter now.

injury lawyer | Jul 10th, 2010, 11:56 AM

Wow. This is a very nice work of science, a clear and concise explanation. I did not know.

daily interest calculator | Jul 16th, 2010, 4:41 AM

The resource is relevant to all flavors of Windows and all versions of IIS & .NET at here like the one you mentioned here will be very useful to me! I will post a link to this page on my blog.

daily interest calculator | Jul 16th, 2010, 4:51 AM

The resource is relevant to all flavors of Windows and all versions of IIS & .NET at here like the one you mentioned here will be very useful to me! I will post a link to this page on my blog.

personal loans | Jul 16th, 2010, 4:53 AM

I am attempting to run my own blog but I think its too general and I want to focus more on smaller topics. Being all things to all people is not all that its cracked up to be.

Link Building | Jul 22nd, 2010, 10:34 AM

I really like this series. Although its a bit confusing & I'd have to read it a few times

yeast infection treatment | Jul 29th, 2010, 9:34 PM

a big thanks to the author for sharing with us a nice info.
Keep up the great work....

placement argent | Aug 6th, 2010, 5:38 AM

Ceci est un blog sympa. Good Clean d'assurance-chômage et de Nice blog informatif. Je reviendrai bientôt,

sudoku tips | Aug 12th, 2010, 11:12 PM

yes thanks a bunch its appreciated

sudoku tips | Aug 12th, 2010, 11:13 PM

Iye sthanks very much for the work its appreciated

Moroccan furniture | Aug 14th, 2010, 3:46 AM

I have read your post.It's is nice one from others blog.I hope you will continue to keep best post.Thank you.

SEO Los Angeles | Aug 15th, 2010, 6:16 PM

own blog but I think its too general and I want to focus more on smaller topics. Being all things to all people is not all that its cracked up to be.

Apricot | Aug 17th, 2010, 11:48 AM

i read your blog occasionally and i own a similar one and i was just wondering if you get a lot of spam comments? If so how do you prevent it, any plugin or anything you can advise? I get so much lately it's driving me mad so any assistance is very much appreciated.

Insurance | Aug 20th, 2010, 8:42 PM

I think its too general and I want to focus more on smaller topics. Being all things to all people is not all that its cracked up to be.

us itunes gift card | Aug 22nd, 2010, 1:37 PM

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.

world of warcraft account | Aug 26th, 2010, 3:07 PM

It is very good for anyone to make a lot of contribution in knowing about the different types of good things there. This is really a special moment in knowing about the proper things there.

Trading Education | Aug 30th, 2010, 8:39 PM

You guys always deliver useful content. Awesome post. Very interesting and valuable videos. Keep posting more articles. Thanks for sharing useful info.

carpet cleaner Monterey | Sep 1st, 2010, 11:06 PM

I am glad to find your blog as it is giving me exact view at the topic of asp world. It is taking my breathe away in this semester for the new project.

Prepress Service | Sep 2nd, 2010, 8:16 AM

I wanted to thank you for this excellent read!! I definitely loved every little bit of it.Cheers for the info!!!! & This is the perfect blog for anyone who wants to know about this topic. You know so much its almost hard to argue with you .........

Tooth whitening Mexico | Sep 2nd, 2010, 2:59 PM

Great to know the -- in depth from this blog.This will really help for my forward steps to be taken.

phentermine phentermine 37.5 | Sep 3rd, 2010, 9:41 AM

Very informative post, thanks for sharing.

austin root canal | Sep 3rd, 2010, 11:07 PM

Excellent sharing without nay Error in the code. Such codes are life saving pills dude. You just rock with explanation.

bow ties | Sep 5th, 2010, 12:25 PM

Its always good to learn tips like you share for blog posting. As I just started posting comments for blog and facing problem of lots of rejections. I think your suggestion would be helpful for me. I will let you know if its work for me too.

leather motorcycle jackets | Sep 5th, 2010, 9:37 PM

Hi Webmaster, commentators and everybody else !!! The blog was absolutely fantastic! Lots of great information and inspiration, both of which we all need!Keep 'em coming... you all do such a great job at such Concepts... can't tell you how much I, for one appreciate all you do!
Thanks

cheap dave matthews band tickets | Sep 6th, 2010, 9:18 AM

Nice post! A very insightful post with a lot of great points. I do hope to see another great article from you.

E20-001 Dumps | Sep 7th, 2010, 11:31 AM

While this does solve the issue at hand, it presents us with a new way to kill our application even quicker than last.

Testking VCP-410 | Sep 9th, 2010, 12:27 PM

Always good to see, this was a brilliant post. In theory I would like to write like this too. You need time to creat that interesting and additionally real effort to make such a good article.

Add comment

After you have posted a comment, an email will be sent to the provided email address. Before your comment is activated, you will have to click the confirmation link within the email.

Name:

Email (only used for validation):

Website (optional):

Message:

Notify me when new comments are added:

Please type the following letters into the box below:  

Post!