Use a line of code to crash the w3wp process. How can I find the cause of the w3wp process crash?

Source: Internet
Author: User

The w3wp process crashes, and an error is left in the system log, but few error messages are left. How can we quickly find out the cause of w3wp crash?

First, write a row.CodeCrash w3wp:

View Source

Print?

1 Protected Void Page_load (Object Sender, eventargs E)
2 {
3 Threadpool. queueuserworkitem (Delegate(Object Nouse ){Throw New Exception ("Go to hell w3wp! ");});
4 }

This line of code will cause the w3wp process to crash, because after Asp.net 2.0, the default method for Microsoft to handle multithreaded operation exceptions has changed. If the thread, threadpool, or system. threading. if an unhandled exception occurs in the timer execution method, the current execution process stops and crashes.

 

Of course, in actual situations, we will not intentionally write such a line of code to cause w3wp to crash. Maybe you will say that I generally do notProgramEven if thread or threadpool is used, a crash may occur, because thread or system is also used in system methods such as database connection pool and httpruntime management request queue. threading. timer class, or threadpool. I have personally encountered a system crash because the SQL Server database connection pool is exhausted and a new database connection is unavailable, this is the same principle as the crash caused by the above code line.

 

It can be said that this crash is a situation that Microsoft designed to crash. Microsoft believes that the process should crash if an exception occurs during multi-thread execution. However, this kind of crash often leaves few system logs. We can only see the crash errors, but not the specific points of the crash or the call stack of the crash. As the saying goes: "There is always more way than suffering". Although the system has no records, we can use code to record the cause of the crash. We have two ways to get the cause of the crash.

 

The first method is to use reflection to record the exit stack and reason of httpruntime in the global application_end event. If you have a decompilation tool, you can decompile the system. Web. httpruntime class. You can see that this class has two private fields:

View Source

Print?

1 Private String _ Shutdownmessage;
2 Private String _ Shutdownstack;

They record the Exit message and exit stack of the w3wp process. We usually record the values of these two variables in the application_end event to see the collapsed stack. See the code for details.

View Source

Print?

01 Protected Void Application_end (Object Sender, eventargs E)
02 {
03 Httpruntime runtime = (httpruntime)Typeof(System. Web. httpruntime). invokemember ("_ Theruntime",
04 Bindingflags. nonpublic | bindingflags. Static | bindingflags. getfield,
05 Null,
06 Null,
07 Null);
08   
09 If (Runtime =Null)
10 Return;
11   
12 String Shutdownmessage = (String) Runtime. GetType (). invokemember ("_ Shutdownmessage",
13 Bindingflags. nonpublic | bindingflags. instance | bindingflags. getfield,
14 Null,
15 Runtime,
16 Null);
17   
18 String Shutdownstack = (String) Runtime. GetType (). invokemember (
19 "_ Shutdownstack",
20 Bindingflags. nonpublic | bindingflags. instance | bindingflags. getfield,
21 Null,
22 Runtime,
23 Null);
24   
25 If (! EventLog. sourceexists (". Netruntime"))
26 {
27   
28 EventLog. createeventsource (". Netruntime","Application");
29   
30 }
31   
32 EventLog =New EventLog ();
33   
34 Log. Source =". Net RunTime";
35   
36 Log. writeentry (string. Format ("\ R \ n \ r \ n_shutdownmessage = {0} \ r \ n \ r \ n_shutdownstack = {1 }", Shutdownmessage, shutdownstack), eventlogentrytype. Error );
37 }

The above code mainly uses reflection, which is not described in detail in this article.

The above method usually gets the call stack of w3wp crash, and we have another method to get it. The appdomain. currentdomain. unhandledexception event is used to record the cause of the unhandled exception. The Code is also simple:

View Source

Print?

01 Protected Void Application_start (Object Sender, eventargs E)
02 {
03 Appdomain. currentdomain. unhandledexception + =New Unhandledexceptioneventhandler (currentdomain_unhandledexception );
04 }
05   
06 Void Currentdomain_unhandledexception (Object Sender, unhandledexceptioneventargs E)
07 {
08 Exception EX = E. exceptionobjectAs Exception;
09 String MSG = ex. message;
10 String Stack = ex. stacktrace;
11 }

The above Code adds a processing method to the appdomain's unhandleexception time during application start to record the cause of the unprocessed exception.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.