Skip to content

Misinformation on multi-threaded behavior #12900

Description

@jinek

Type of issue

Other (describe below)

Description

Information given in the article https://learn.microsoft.com/en-us/dotnet/api/system.runtime.exceptionservices.exceptionhandling.raiseappdomainunhandledexceptionevent?view=net-10.0
is not reflected by actual behavior in dotnet 10.

Consider following application

using System.Runtime.ExceptionServices;

var exception1 = new Exception("Exception 1");

AppDomain.CurrentDomain.UnhandledException += (_, e) =>
{
    Console.WriteLine("Started handler for " + e.ExceptionObject);

    if (e.ExceptionObject == exception1)
        Thread.Sleep(5000);

    Console.WriteLine("Finished handler for " + e.ExceptionObject);
};

ThreadPool.QueueUserWorkItem(_ =>
{
    Thread.Sleep(1000);
    ExceptionHandling.RaiseAppDomainUnhandledExceptionEvent(new Exception("Exception 2 from parallel thread"));
    Console.WriteLine("Finished calling RaiseAppDomainUnhandledExceptionEvent for exception 2 from parallel thread");
});

ExceptionHandling.RaiseAppDomainUnhandledExceptionEvent(exception1);
Console.WriteLine("Finished calling RaiseAppDomainUnhandledExceptionEvent for exception 1");

Which prints

Started handler for System.Exception: Exception 1
Started handler for System.Exception: Exception 2 from parallel thread
Finished handler for System.Exception: Exception 2 from parallel thread
Finished calling RaiseAppDomainUnhandledExceptionEvent for exception 2 from parallel thread
Finished handler for System.Exception: Exception 1
Finished calling RaiseAppDomainUnhandledExceptionEvent for exception 1
Process finished with exit code 0.

So, no threads have to wait indefinitely and moreover threads don't wait for each other at all.

Page URL

https://learn.microsoft.com/en-us/dotnet/api/system.runtime.exceptionservices.exceptionhandling.raiseappdomainunhandledexceptionevent?view=net-10.0

Content source URL

https://github.com/dotnet/dotnet-api-docs-temp/blob/live/xml/System.Runtime.ExceptionServices/ExceptionHandling.xml

Document Version Independent Id

225e6f33-99c0-49dc-e1c5-6fadd0fdfda2

Platform Id

242dbdae-3183-c01f-7b66-70ba70aa6b66

Article author

@dotnet-bot

Metadata

Metadata

Assignees

No one assigned

    Labels

    untriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions