From 0b468b80615392cc51d46a505d758977ca260266 Mon Sep 17 00:00:00 2001
From: Aloke Desai <aloke.desai@gmail.com>
Date: Thu, 2 Dec 2021 06:08:59 -0500
Subject: [PATCH] feat: Support for ignored signals with SIGN_IGN (#1489)

Currently, if a signal is ignored via SIG_IGN (which is especially common for signals like SIGPIPE), Sentry will still report a fatal, unhandled crash even though the application never received the signal and continued to run just fine.

This PR fixes this behavior by only setting a signal handler to report a crash from signal if the original handler is NOT SIG_IGN.

Fixes GH-1472

Co-authored-by: Philipp Hofmann <philipp.hofmann@sentry.io>
---
 CHANGELOG.md                                               | 1 +
 .../Recording/Monitors/SentryCrashMonitor_Signal.c         | 7 +++++++
 2 files changed, 8 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 524602ec331..48848197d77 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,7 @@
 ## 7.5.4
 
 - fix: Sending OOM when SDK is closed (#1487)
+- feat: Don't mark a signal as a crash if the signal was ignored via `SIG_IGN` (#1489)
 
 ## 7.5.3
 
diff --git a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_Signal.c b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_Signal.c
index 8b4e70395fa..5e2b5cc2ff4 100644
--- a/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_Signal.c
+++ b/Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_Signal.c
@@ -175,6 +175,13 @@ installSignalHandler()
                 sigaction(fatalSignals[i], &g_previousSignalHandlers[i], NULL);
             }
             goto failed;
+        } else {
+            // The previous handler was `SIG_IGN` -- restore the original handler so
+            // we don't override the `SIG_IGN` and report a crash when the application
+            // would have ignored the signal otherwise.
+            if (g_previousSignalHandlers[i].sa_handler == SIG_IGN) {
+                sigaction(fatalSignals[i], &g_previousSignalHandlers[i], NULL);
+            }
         }
     }
     SentryCrashLOG_DEBUG("Signal handlers installed.");