Mike Flynn
Mike Flynn

Reputation: 24325

Throwing an Exception In a ASP.NET Thread To Bring Down IIS

I am doing a code review and found this piece of code. Does throwing an exception inside a thread bring down IIS? Read the comments.

         Thread unhandledExceptionThread = new System.Threading.Thread(new System.Threading.ThreadStart(delegate()
         {
              System.Threading.Thread.Sleep(90 * 1000);
              throw new ApplicationException("Thread 1");
         }));
         unhandledExceptionThread.Name = "IntentionalCrasher";
         Thread environmentExitThread = new System.Threading.Thread(new System.Threading.ThreadStart(delegate()
         {
              System.Threading.Thread.Sleep(100 * 1000);
              Environment.Exit(-500);
         }));
         environmentExitThread.Name = "Thread 2";
         unhandledExceptionThread.Start();
         environmentExitThread.Start();

Upvotes: 0

Views: 410

Answers (1)

usr
usr

Reputation: 171246

It won't bring "IIS down" but it will terminate the ASP.NET worker process. It will restart.

Your code snippet first tries to die by throwing an exception. 10sec later, if that didn't help, it takes matters into its own hands and kills the process.

Alternatively, it could call AppDomain.Unload on the current domain. But this is a reasonable way to restart the worker process.

Make sure rapid fail protection is disabled. This feature is the devil because it permanently shuts down your app after 5 failures. Permanently without notification. On any kind of error.

Upvotes: 1

Related Questions