C #~ Asynchronous programming continues ~ W3wp.exe caused by awaitand asynccrashes,

Source: Internet
Author: User

C #~ Asynchronous programming continues ~ W3wp.exe caused by awaitand asynccrashes,

This has recently started to happen again. The application pool of IIS is not suspended and all points to the same finger, async, threadPool, Task, and System. nullReferenceException makes us feel that our asynchronous program has a problem. The same is true. Our asynchronous call references non-null references to the context, finally, the w3wp process died!
I found the cause of the problem through the sharing of other predecessors, and my uncle also summarized it.
1 The async method needs to use await to wait for its results. This ensures that the context of your SynchronizationContext is not empty, that is, there will be no non-null reference errors.

2 when calling the async method, if you do not add the await keyword, you can also use its ConfigureAwait (false) method. Although it does not save the SynchronizationContext context, however, it does not report non-null reference errors.

3. Call the async Asynchronous Method in a new thread. Note the preceding two points.

See article

Http://www.cnblogs.com/cmt/p/configure_await_false.html

Http://www.cnblogs.com/cmt/p/sokcet_memory_leak.html

Technical Points

1 Task. Run () =>{}); add a Task to the thread pool and queue for execution

2 async identifies a method as an asynchronous method, which can be executed in parallel with the main thread to take advantage of the multi-core CPU advantages

3 await can add this modifier before calling an async method. It means that the following code is executed after the current Asynchronous Method is executed.

4. ConfigureAwait (true): When the code is synchronized from Synchronous execution to asynchronous execution, the context information of the current thread will be captured and saved to SynchronizationContext for use in asynchronous execution, and used for Synchronous execution after asynchronous execution is completed

5. Configurewait (flase) does not capture the thread context information. The context information of the thread before await cannot be obtained when the code after await is executed in the async method. In ASP. the most direct impact in NET is HttpConext. the Current value is null, but no non-null reference error occurs.

Why w3wp.exe is suspended when asyncis triggered

For people who are lazy about asynchronous methods, that is, those who use Wait () and Result, they will pay some price because it will cause a thread deadlock and eventually cause w3wp to fail, note that this will not happen in the controller console program.

When an unfinished Task is await, the current context re-obtains and continues to execute the remaining code when the Task is completed. This context is the current SynchronizationContext, unless it is empty. SynchronizationContext of GUI and ASP. NET applications is exclusive and only one thread is allowed to run. When await is finished, it tries to execute its remaining part in its original code context, but there is already a thread in the context of the Code, that is the thread that has been waiting for async to complete synchronization. The two of them are waiting for each other, so they are deadlocked.

The Code is as follows:

Public class tools {public static async Task TestAsync () {await Task. delay (1000) ;}} public class HomeController: Controller {public ActionResult Index () {tools. testAsync (). wait (); // Life and Death locks. w3wp.exe hangs the ViewBag. message = "test"; return View ();}}

 

The knowledge we learned in resolving the w3wp.exe crash!

Thank you for reading this article!

 

Related Article

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.