Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide fix for #81093 - "Mono does not emit ProcessExit event on SIGTERM" #100056

Merged
merged 5 commits into from
Jul 11, 2024

Conversation

nealef
Copy link
Contributor

@nealef nealef commented Mar 21, 2024

Supersedes #82813
Fixes #81093

  • src/mono/mono/mini/mini-posix.c

    • Add signal handler for SIGTERM
  • src/mono/mono/mini/mini-windows.c

    • Add signal handler for SIGTERM
    • Use the correct signal for handler
  • src/mono/mono/mini/mini-runtime.c

    • Add mono_sigterm_signal_handler to process SIGTERM that will set a global variable to be monitored by the GC finalizer thread
    • Set a default exit code before setting the term_signaled variable that gets checked in gc
  • src/mono/mono/mini/mini-runtime.h

    • Define prototype for mono_sigterm_signal_handler()
  • src/mono/mono/metadata/gc.c

    • Monitor for sigterm and kick off the shutdown process when encountered by calling mono_runtime_try_shutdown().
    • Exit with either the user set exitcode (System.Environment.ExitCode) or SIGTERM + 128.
    • Simplify use of exit code now that a default is being set
    • Rename term_signaled to match mono style
    • Remove volatile attribute
    • Move testing of shutdown until after the sem wait
  • src/libraries/System.Runtime/tests/System/ExitCodeTests.Unix.cs

    • Re-enable ExitCodeTests for mono
  • src/mono/mono/mini/exceptions-amd64.c src/mono/mono/mini/exceptions-x86.c

    • Add control event handling for windows

Note: The comments in the original PR regarding possible memory barriers and acquire/release are still to be addressed.

…on SIGTERM"

* src/mono/mono/mini/mini-posix.c
  - Add signal handler for SIGTERM

* src/mono/mono/mini/mini-windows.c
  - Add signal handler for SIGTERM
  - Use the correct signal for handler

* src/mono/mono/mini/mini-runtime.c
  - Add mono_sigterm_signal_handler to process SIGTERM that will set a global variable
    to be monitored by the GC finalizer thread
  - Set a default exit code before setting the term_signaled variable that gets checked in gc

* src/mono/mono/mini/mini-runtime.h
  - Define prototype for mono_sigterm_signal_handler()

* src/mono/mono/metadata/gc.c
  - Monitor for sigterm and kick off the shutdown process when encountered by calling mono_runtime_try_shutdown().
  - Exit with either the user set exitcode (System.Environment.ExitCode) or SIGTERM + 128.
  - Simplify use of exit code now that a default is being set
  - Rename term_signaled to match mono style
  - Remove volatile attribute
  - Move testing of shutdown until after the sem wait

* src/libraries/System.Runtime/tests/System/ExitCodeTests.Unix.cs
  - Re-enable ExitCodeTests for mono

* src/mono/mono/mini/exceptions-amd64.c
  src/mono/mono/mini/exceptions-x86.c
  - Add control event handling for windows
@lewing
Copy link
Member

lewing commented Jun 13, 2024

I cycled CI just to have up to date checks

@@ -272,6 +298,11 @@ void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
case SIGSEGV:
segv_handler = handler;
break;
case SIGTERM:
term_handler = handler;
if (!SetConsoleCtrlHandler(mono_win_ctrl_handler, TRUE))
Copy link
Member

@kg kg Jun 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds a new handler, so if win32_seh_set_handler was previously called to install a different term_handler, shouldn't we uninstall the old one first?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, i misread, but i think this would add a copy of our handler each time you set a term handler, so we might end up with 3 copies of the same one installed unless the kernel removes duplicates. the docs don't say whether it does

if an application calls AllocConsole or AttachConsole this handler will get uninstalled, but that's probably fine to consider as a 'don't do that' problem

@kg
Copy link
Member

kg commented Jun 13, 2024

lgtm but i'm not an expert on our EH internals or signals

Copy link
Member

@lambdageek lambdageek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@directhex
Copy link
Contributor

/ba-g all dead lettering on some tests, signed off by various managers & engineers who know relevant areas

@directhex directhex merged commit 5bc0f83 into dotnet:main Jul 11, 2024
103 of 117 checks passed
@nealef nealef deleted the sigterm-win branch July 12, 2024 03:14
akoeplinger added a commit to dotnet/sdk that referenced this pull request Jul 17, 2024
The mono issue that caused the different exit code was fixed in dotnet/runtime#100056.

We now have the same exit code as coreclr so remove the mono special case.

Fixes dotnet/source-build#3174
Fixes dotnet/source-build#4514
@github-actions github-actions bot locked and limited conversation to collaborators Aug 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-VM-meta-mono community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mono does not emit ProcessExit event on SIGTERM.
5 participants