Skip to content

Commit

Permalink
Provide fix for dotnet#81093
Browse files Browse the repository at this point in the history
"Mono does not emit ProcessExit event on SIGTERM"

	* src/mono/mono/mini/mini-posix.c
	  - Add signal handler for SIGTERM
	  - SIGTERM handler will set a global variable that may be
	    monitored by the GC finalizer thread

	* 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.
  • Loading branch information
nealef committed Mar 1, 2023
1 parent 2b74314 commit 4907577
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/mono/mono/metadata/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <mono/utils/mono-os-wait.h>
#include <mono/utils/mono-lazy-init.h>
#include <mono/utils/mono-threads-wasm.h>
#include <mono/metadata/runtime.h>
#ifndef HOST_WIN32
#include <pthread.h>
#endif
Expand All @@ -63,6 +64,8 @@ static gboolean gc_disabled;

static gboolean finalizing_root_domain;

extern volatile gboolean term_signaled;

gboolean mono_log_finalizers;
gboolean mono_do_not_finalize;
static volatile gboolean suspend_finalizers;
Expand Down Expand Up @@ -879,6 +882,17 @@ finalizer_thread (gpointer unused)
mono_hazard_pointer_install_free_queue_size_callback (hazard_free_queue_is_too_big);

while (!finished) {

/* Just in case we've received a SIGTERM */
if (term_signaled) {
int ec = mono_environment_exitcode_get();
mono_runtime_try_shutdown();
if (ec == 0)
exit(128+SIGTERM);
else
exit(ec);
}

/* Wait to be notified that there's at least one
* finaliser to run
*/
Expand Down
13 changes: 13 additions & 0 deletions src/mono/mono/mini/mini-posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@
#endif
#include "mono/utils/mono-tls-inline.h"

gboolean term_signaled = FALSE;

#if defined(HOST_WATCHOS)

void
Expand Down Expand Up @@ -291,6 +293,15 @@ MONO_SIG_HANDLER_FUNC (static, sigquit_signal_handler)
mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
}

MONO_SIG_HANDLER_FUNC (static, sigterm_signal_handler)
{
term_signaled = TRUE;

mono_gc_finalize_notify ();

mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
}

MONO_SIG_HANDLER_FUNC (static, sigusr2_signal_handler)
{
gboolean enabled = mono_trace_is_enabled ();
Expand Down Expand Up @@ -390,6 +401,8 @@ mono_runtime_posix_install_handlers (void)
sigaddset (&signal_set, SIGFPE);
add_signal_handler (SIGQUIT, sigquit_signal_handler, SA_RESTART);
sigaddset (&signal_set, SIGQUIT);
add_signal_handler (SIGTERM, sigterm_signal_handler, SA_RESTART);
sigaddset (&signal_set, SIGTERM);
add_signal_handler (SIGILL, mono_crashing_signal_handler, 0);
sigaddset (&signal_set, SIGILL);
add_signal_handler (SIGBUS, mono_sigsegv_signal_handler, 0);
Expand Down

0 comments on commit 4907577

Please sign in to comment.