Skip to content

ignore posix signal and shutdown nethost gracefully #106262

Closed

Description

@changnet

I have a Linux C++ program works like:

Process start
  Module1 run
  Module2 run
  ...
  Receive SIGTERM
  Module1 exit
  Module2 exit
  ...
  Save data
Process exit

Recently, I needed to integrate a third-party C# module. Following the hosting example, I was able to run the C# DLL module.

However, when sending SIGTERM to shut down the process, coreclr.so exits the process directly:

Process start
  Module1 run
  Module2 run
  Module_CSharp run
  ...
  Receive SIGTERM
  CoreCLR exits the whole process

  // The following never runs
  Module1 exit
  Module2 exit
  ...
  Save data
Process exit

After several days of research, I couldn't find an API to ignore POSIX signals in C#. The closest solution I found is:

PosixSignalRegistration.Create(PosixSignal.SIGTERM, (ctx) =>
{
    ctx.Cancel = true;
});

This only prevents coreclr from shutting down, but coreclr still consumes the signal. What I'm looking for is an API to prevent coreclr from receiving POSIX signals and leave the signal handling to the main thread:

Process start
  Module1 run
  Module2 run
  Module_C# run
  Make CoreCLR ignore POSIX signals  <<< looking for something like signal(SIGTERM, SIG_IGN)
  ...
  Receive SIGTERM
  Module1 exit
  Module2 exit
  Module_CSharp exit and shut down CoreCLR <<< looking for api to shutdown CoreCLR but not exit the process
  ...
  Save data
Process exit

Currently, I use PosixSignalRegistration to call a C++ function to tell the main thread to save data and exit, but I cannot shut down coreclr when process exit.

What am I missing?

Activity

  1. ghost added
    needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners
    on Aug 12, 2024
  2. added and removed
    needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners
    on Aug 12, 2024
  3. dotnet-policy-service commented on Aug 12, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @vitek-karas, @agocke, @VSadov
    See info in area-owners.md if you want to be subscribed.

  4. added this to the 10.0.0 milestone on Aug 12, 2024
  5. dotnet-policy-service commented on Aug 12, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/interop-contrib
    See info in area-owners.md if you want to be subscribed.

  6. jkotas commented on May 16, 2025

    @jkotas
    Member

    Fixed by #115494. libcoreclr.so does not consume the SIGTERM signal anymore.

  7. locked and limited conversation to collaborators on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status
      No status

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions