From 556b15a9b78a7d873238688d93432ed43f997916 Mon Sep 17 00:00:00 2001
From: bones_was_here <bones_was_here@xonotic.au>
Date: Fri, 8 Mar 2024 06:31:33 +1000
Subject: [PATCH] sys: allow the platform to handle crashes after DP does

As suggested by divVerent in chat.
In SDL builds (including when using -dedicated on the cmdline) the OS
handler runs after the user clicks OK on the SDL dialog.

Signed-off-by: bones_was_here <bones_was_here@xonotic.au>
---
 host.c       | 2 +-
 host.h       | 3 ++-
 sys_shared.c | 6 +++++-
 3 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/host.c b/host.c
index 59496a58..fbe40b6a 100644
--- a/host.c
+++ b/host.c
@@ -683,7 +683,7 @@ void Host_Main(void)
 	oldtime = Sys_DirtyTime();
 
 	// Main event loop
-	while(host.state != host_shutdown)
+	while(host.state < host_shutdown) // see Sys_HandleCrash() comments
 	{
 		// Something bad happened, or the server disconnected
 		if (setjmp(host.abortframe))
diff --git a/host.h b/host.h
index 986b3224..779ccc84 100644
--- a/host.h
+++ b/host.h
@@ -20,10 +20,11 @@ struct cmd_state_s;
 
 typedef enum host_state_e
 {
-	host_shutdown,
 	host_init,
 	host_loading,
 	host_active,
+	// states >= host_shutdown cause graceful shutdown, see Sys_HandleCrash() comments
+	host_shutdown,
 	host_failing, ///< crashing
 	host_failed ///< crashed or aborted, SDL dialog open
 } host_state_t;
diff --git a/sys_shared.c b/sys_shared.c
index 4c714cc0..745a839e 100644
--- a/sys_shared.c
+++ b/sys_shared.c
@@ -1011,7 +1011,11 @@ static void Sys_HandleCrash(int sig)
 	Sys_SDL_Dialog("Engine Crash", dialogtext);
 
 	fflush(stderr); // not async-signal-safe :(
-	_Exit(sig);
+
+	// Continue execution with default signal handling.
+	// A real crash will be re-triggered so the platform can handle it,
+	// a fake crash (kill -SEGV) will cause a graceful shutdown.
+	signal(sig, SIG_DFL);
 }
 
 static void Sys_HandleSignal(int sig)
-- 
2.39.5