Skip to content

AssemblyLoadContext.Unloading is not called when TERM signal sent to application #6733

Closed

Description

@dougkpowers

Based on #2688 and #5205, I would expect the default AssemblyLoadContext's Unloading event to fire when the application receives a SIGTERM. However, the event handler doesn't seem to ever be called.

Code to reproduce:

public class Program
{
private static void unloadTask(AssemblyLoadContext obj)
{
    Console.WriteLine("Unloading");
}

    public static void Main(string[] args)
    {
        Console.WriteLine("Starting App");

        AssemblyLoadContext.Default.Unloading += unloadTask;

        try
        {
        Console.ReadLine();

        }
        catch (System.Exception)
        {
            Console.WriteLine("aborted?");
        }
    }

}

$dotnet run
$kill -TERM pid

output:
Starting App

expected output:
Starting App
Unloading

Same behavior with dotnet 1.0.1 on OSX and Windows 10

Activity

  1. m-jepson commented on Dec 11, 2016

    @m-jepson

    I haven't been able to get this event to fire either. It is possible on Linux though, but only if you run the process in the background and then kill it using "kill <pid>". If you run it and then press CTRL-C, it won't fire either.

  2. janvorli commented on Dec 12, 2016

    @janvorli
    Member

    @kouvel, @adityamandaleeka, @stephentoub - it seems that the intent of #4950, https://github.com/dotnet/corefx/issues/5205 and dotnet/coreclr#4309 was to enable an app to hook to an event that gets invoked when the app receives the SIGTERM, but it doesn't seem to work. I had hard time figuring out from the lengthy discussions in those issues / PRs where we have finally landed. Does one of you know if the current behavior is a bug or if we have just not finished the work as expected?

  3. kouvel commented on Dec 12, 2016

    @kouvel
    Contributor

    Our expectation was that SIGTERM should raise the Unloading event, so this sounds like a bug. I'm not sure about Ctrl+C / SIGINT, it also seems like a graceful shutdown case so I guess it should also raise the event. I'll take a look at both of these.

  4. janvorli commented on Dec 12, 2016

    @janvorli
    Member

    As for the SIGINT, it is handled in corefx in pal_console.cpp. It is supposed to call a callback registered using SystemNative_RegisterForCtrl.

  5. self-assigned this
    on Dec 12, 2016
  6. dougkpowers commented on Dec 12, 2016

    @dougkpowers
    Author

    I would prefer that a SIGINT NOT call Unloading event handler because I would like the two signals (SIGINT and SIGTERM) be distinguishable. SIGINT currently fires the Console.CancelKeyPress, and because the SIGINT is only applicable in a foreground/console application, this behavior seems sufficient.

    If I wanted to take different actions on each event, I could choose to do that by having different event handling code for each event. The Unloading event would give me a way to handle SIGTERM and the Console.CancelKeyPress would give me a way to handle SIGINT. If I wanted to take the same action on both events, I could choose to execute common code from each handler.

  7. janvorli commented on Dec 12, 2016

    @janvorli
    Member

    @dougkpowers I also believe that's the expected behavior.

  8. tugberkugurlu commented on Dec 13, 2016

    @tugberkugurlu
  9. kouvel commented on Dec 13, 2016

    @kouvel
    Contributor

    It looks like SIGTERM is raising the Unloading event on Linux but not on OSX, this is a bug.

    SIGINT is raising the Unloading event on both, but CoreFX only starts listening for the signal when a callback is registered with the CancelKeyPress event, so if nothing is registered, the Unloading event is not raised and the app terminates. It should be raising the Unloading event in any case, something wrong here too. Should handle SIGQUIT in the same manner as well.

  10. kouvel commented on Dec 13, 2016

    @kouvel
    Contributor

    AppDomain.DomainUnloading, which was added recently for ns2.0, is not raised at all, issue dotnet/coreclr#8622

  11. janvorli commented on Dec 13, 2016

    @janvorli
    Member

    @kouvel - can you check the Windows behavior w.r.t. the SIGINT / SIGQUIT handling and the Unloading event? It would be good to keep both Windows and Unix on the same plan.

  12. kouvel commented on Dec 13, 2016

    @kouvel
    Contributor

    On Windows, when a handler is registered for CancelKeyPress, Ctrl+C & Ctrl+Break both raise the Unloading event (treated as graceful shutdown). When no handler is registered, the Unloading event is not raised (treated as abrupt shutdown). Should we be consistent and treat them both as either graceful or abrupt shutdown? I feel that registering for CancelKeyPress shouldn't change the shutdown behavior.

  13. kouvel commented on Dec 13, 2016

    @kouvel
    Contributor

    In .NET desktop:

    • Normal app exit (return from Main or Environment.Exit):
      • AppDomain.DomainUnloading is not raised for the default app domain, I suppose that is the expected behavior and should be the same for Core as well
      • AppDomain.ProcessExit is raised (Core raises this as well)
    • Ctrl+C and Ctrl+Break
      • AppDomain.DomainUnloading is not raised
      • AppDomain.ProcessExit is also not raised
      • So although it allows the app to intercept and cancel these events, it is in a sense treated as abrupt shutdown

    Maybe we should map the Core behavior to the desktop behavior for SIGINT and SIGQUIT. Could be considered a breaking change though. Thoughts?

    SIGTERM should still be treated as graceful shutdown and raise the appropriate events.

  14. kouvel commented on Dec 13, 2016

    @kouvel
    Contributor
  15. added a commit that references this issue on Apr 8, 2017
  16. transferred this issue fromdotnet/coreclron Jan 31, 2020
  17. added this to the 2.0.0 milestone on Jan 31, 2020
  18. ghost locked as resolved and limited conversation to collaborators on Dec 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-PAL-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions