Skip to content

Commit

Permalink
feat: Disable SIGTERM reporting by default
Browse files Browse the repository at this point in the history
We added support for SIGTERM reporting in the last release and enabled
it by default. For some users, SIGTERM events were verbose and not
actionable. Therefore, we disable it per default.

Fixes GH-4013
  • Loading branch information
philipphofmann committed May 31, 2024
1 parent 7d72ee1 commit cec63fc
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
- Add start time to network request breadcrumbs (#4008)
- Add C++ exception support for `__cxa_rethrow` (#3996)
- Add beforeCaptureScreenshot callback (#4016)
- Add option to enable SIGTERM reporting (#4025). We added support
for SIGTERM reporting in the last release and enabled it by default.
For some users, SIGTERM events were verbose and not actionable.
Therefore, we disable it per default in this release. If you'd like
to receive SIGTERM events, set the option `enableSigtermReporting = true`.

### Improvements

Expand Down
1 change: 1 addition & 0 deletions Samples/iOS-Swift/iOS-Swift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
options.beforeSend = { event in
return event
}
options.enableSigtermReporting = true
options.beforeCaptureScreenshot = { _ in
return true
}
Expand Down
13 changes: 13 additions & 0 deletions Sources/Sentry/Public/SentryOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ NS_SWIFT_NAME(Options)
*/
@property (nonatomic, assign) BOOL enableCrashHandler;

/**
* When enabled, the SDK reports SIGTERM signals to Sentry.
*
* It's crucial for developers to understand that the OS sends a SIGTERM to their app as a prelude
* to a graceful shutdown, before resorting to a SIGKILL. This SIGKILL, which your app can't catch
* or ignore, is a direct order to terminate your app's process immediately. Developers should be
* aware that their app can receive a SIGTERM in various scenarios, such as CPU or disk overuse,
* watchdog terminations, or when the OS updates your app.
*
* @note The default value is @c NO.
*/
@property (nonatomic, assign) BOOL enableSigtermReporting;

/**
* How many breadcrumbs do you want to keep in memory?
* @note Default is @c 100 .
Expand Down
6 changes: 5 additions & 1 deletion Sources/Sentry/SentryCrashIntegration.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import "SentryCrashIntegration.h"
#import "SentryCrashInstallationReporter.h"
#import "SentryCrashMonitor_Signal.h"
#import "SentryCrashWrapper.h"
#import "SentryDispatchQueueWrapper.h"
#import "SentryEvent.h"
Expand Down Expand Up @@ -87,7 +88,8 @@ - (BOOL)installWithOptions:(nonnull SentryOptions *)options
self.scopeObserver =
[[SentryCrashScopeObserver alloc] initWithMaxBreadcrumbs:options.maxBreadcrumbs];

[self startCrashHandler:options.cacheDirectoryPath];
[self startCrashHandler:options.cacheDirectoryPath
enableSigtermReporting:options.enableSigtermReporting];

[self configureScope];

Expand All @@ -100,6 +102,7 @@ - (SentryIntegrationOption)integrationOptions
}

- (void)startCrashHandler:(NSString *)cacheDirectory
enableSigtermReporting:(BOOL)enableSigtermReporting
{
void (^block)(void) = ^{
BOOL canSendReports = NO;
Expand All @@ -116,6 +119,7 @@ - (void)startCrashHandler:(NSString *)cacheDirectory
canSendReports = YES;
}

setEnableSigtermReporting(enableSigtermReporting);
[installation install:cacheDirectory];

// We need to send the crashed event together with the crashed session in the same envelope
Expand Down
4 changes: 4 additions & 0 deletions Sources/Sentry/SentryOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ - (instancetype)init
self.enabled = YES;
self.shutdownTimeInterval = 2.0;
self.enableCrashHandler = YES;
self.enableSigtermReporting = NO;
self.diagnosticLevel = kSentryLevelDebug;
self.debug = NO;
self.maxBreadcrumbs = defaultMaxBreadcrumbs;
Expand Down Expand Up @@ -316,6 +317,9 @@ - (BOOL)validateOptions:(NSDictionary<NSString *, id> *)options
[self setBool:options[@"enableCrashHandler"]
block:^(BOOL value) { self->_enableCrashHandler = value; }];

[self setBool:options[@"enableSigtermReporting"]
block:^(BOOL value) { self->_enableSigtermReporting = value; }];

if ([options[@"maxBreadcrumbs"] isKindOfClass:[NSNumber class]]) {
self.maxBreadcrumbs = [options[@"maxBreadcrumbs"] unsignedIntValue];
}
Expand Down
12 changes: 12 additions & 0 deletions Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_Signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@
// ============================================================================

static volatile bool g_isEnabled = false;
static volatile bool g_isSigtermReportingEnabled = false;

void
setEnableSigtermReporting(bool enabled)
{
g_isSigtermReportingEnabled = enabled;
}

static SentryCrash_MonitorContext g_monitorContext;
static SentryCrashStackCursor g_stackCursor;
Expand Down Expand Up @@ -163,6 +170,11 @@ installSignalHandler(void)
action.sa_sigaction = &handleSignal;

for (int i = 0; i < fatalSignalsCount; i++) {
if (fatalSignals[i] == SIGTERM && !g_isSigtermReportingEnabled) {
SentryCrashLOG_DEBUG("SIGTERM handling disabled. Skipping assigning handler.");
continue;
}

SentryCrashLOG_DEBUG("Assigning handler for signal %d", fatalSignals[i]);
if (sigaction(fatalSignals[i], &action, &g_previousSignalHandlers[i]) != 0) {
char sigNameBuff[30];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ extern "C" {

#include "SentryCrashMonitor.h"

/** Wether to assign the signal handler for SIGTERM or not.
*/
void setEnableSigtermReporting(bool enabled);

/** Access the Monitor API.
*/
SentryCrashMonitorAPI *sentrycrashcm_signal_getAPI(void);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ static const int g_fatalSignals[] = {
// SIGTERM can be caught and is usually sent by iOS and variants
// when Apple wants to try and gracefully shutdown the app
// before sending a SIGKILL (which can't be caught).
// Some areas I've seen this happen are:
// Some areas this might happen are:
// - When the OS updates an app.
// - In some circumstances for Watchdog Events.
// - Resource overuse (CPU, Disk, ...).
Expand Down
5 changes: 5 additions & 0 deletions Tests/SentryTests/SentryOptionsTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ - (void)testDiagnosticlevelWith:(NSObject *)level expected:(SentryLevel)expected
XCTAssertEqual(expected, options.diagnosticLevel);
}

- (void)testEnableSigtermReporting
{
[self testBooleanField:@"enableSigtermReporting" defaultValue:NO];
}

- (void)testValidEnabled
{
[self testEnabledWith:@YES expected:YES];
Expand Down

0 comments on commit cec63fc

Please sign in to comment.