Skip to content

Commit

Permalink
ref: move SentryCrash singleton to SentryDependencyContainer (#3247)
Browse files Browse the repository at this point in the history
  • Loading branch information
armcknight authored Nov 7, 2023
1 parent e5dcbd5 commit c471221
Show file tree
Hide file tree
Showing 19 changed files with 73 additions and 62 deletions.
2 changes: 2 additions & 0 deletions SentryTestUtils/ClearTestState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ class TestCleanup: NSObject {
PrivateSentrySDKOnly.onAppStartMeasurementAvailable = nil
SentrySDK.setAppStartMeasurement(nil)
#endif // os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)

sentrycrash_scopesync_reset()
}
}
1 change: 1 addition & 0 deletions SentryTestUtils/SentryTestUtils-ObjC-BridgingHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#import "SentryReachability.h"
#import "SentrySDK+Private.h"
#import "SentrySDK+Tests.h"
#import "SentryScopeSyncC.h"
#import "SentrySession.h"
#import "SentrySwizzleWrapper.h"
#import "SentrySystemWrapper.h"
Expand Down
6 changes: 4 additions & 2 deletions Sources/Sentry/SentryCrashExceptionApplication.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import "SentryCrashExceptionApplication.h"
#import "SentryCrash.h"
#import "SentryDependencyContainer.h"
#import "SentrySDK.h"

@implementation SentryCrashExceptionApplication
Expand All @@ -10,8 +11,9 @@ - (void)reportException:(NSException *)exception
{
[[NSUserDefaults standardUserDefaults]
registerDefaults:@{ @"NSApplicationCrashOnExceptions" : @YES }];
if (nil != SentryCrash.sharedInstance.uncaughtExceptionHandler && nil != exception) {
SentryCrash.sharedInstance.uncaughtExceptionHandler(exception);
SentryCrash *crash = SentryDependencyContainer.sharedInstance.crashReporter;
if (nil != crash.uncaughtExceptionHandler && nil != exception) {
crash.uncaughtExceptionHandler(exception);
}
[super reportException:exception];
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Sentry/SentryCrashIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ - (void)configureScope
userInfo[@"release"] = self.options.releaseName;
userInfo[@"dist"] = self.options.dist;

[SentryCrash.sharedInstance setUserInfo:userInfo];
[SentryDependencyContainer.sharedInstance.crashReporter setUserInfo:userInfo];

[outerScope addObserver:self.scopeObserver];
}];
Expand Down
8 changes: 5 additions & 3 deletions Sources/Sentry/SentryCrashWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#import <SentryCrashCachedData.h>
#import <SentryCrashDebug.h>
#import <SentryCrashMonitor_System.h>
#import <SentryDependencyContainer.h>
#include <mach/mach.h>

NS_ASSUME_NONNULL_BEGIN
Expand All @@ -23,7 +24,7 @@ + (instancetype)sharedInstance

- (BOOL)crashedLastLaunch
{
return SentryCrash.sharedInstance.crashedLastLaunch;
return SentryDependencyContainer.sharedInstance.crashReporter.crashedLastLaunch;
}

- (NSTimeInterval)durationFromCrashStateInitToLastCrash
Expand All @@ -33,7 +34,7 @@ - (NSTimeInterval)durationFromCrashStateInitToLastCrash

- (NSTimeInterval)activeDurationSinceLastCrash
{
return SentryCrash.sharedInstance.activeDurationSinceLastCrash;
return SentryDependencyContainer.sharedInstance.crashReporter.activeDurationSinceLastCrash;
}

- (BOOL)isBeingTraced
Expand All @@ -55,7 +56,8 @@ - (NSDictionary *)systemInfo
{
static NSDictionary *sharedInfo = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ sharedInfo = SentryCrash.sharedInstance.systemInfo; });
dispatch_once(&onceToken,
^{ sharedInfo = SentryDependencyContainer.sharedInstance.crashReporter.systemInfo; });
return sharedInfo;
}

Expand Down
13 changes: 13 additions & 0 deletions Sources/Sentry/SentryDependencyContainer.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#import "SentryUIDeviceWrapper.h"
#import <SentryAppStateManager.h>
#import <SentryClient+Private.h>
#import <SentryCrash.h>
#import <SentryCrashWrapper.h>
#import <SentryDebugImageProvider.h>
#import <SentryDependencyContainer.h>
Expand Down Expand Up @@ -121,6 +122,18 @@ - (SentryCrashWrapper *)crashWrapper
return _crashWrapper;
}

- (SentryCrash *)crashReporter
{
if (_crashReporter == nil) {
@synchronized(sentryDependencyContainerLock) {
if (_crashReporter == nil) {
_crashReporter = [[SentryCrash alloc] init];
}
}
}
return _crashReporter;
}

- (SentrySysctl *)sysctlWrapper
{
if (_sysctlWrapper == nil) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Sentry/SentrySDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ + (void)setUser:(SentryUser *_Nullable)user

+ (BOOL)crashedLastRun
{
return SentryCrash.sharedInstance.crashedLastLaunch;
return SentryDependencyContainer.sharedInstance.crashReporter.crashedLastLaunch;
}

+ (void)startSession
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
@class SentryANRTracker;
@class SentryAppStateManager;
@class SentryBinaryImageCache;
@class SentryCrash;
@class SentryCrashWrapper;
@class SentryCurrentDateProvider;
@class SentryDebugImageProvider;
Expand Down Expand Up @@ -54,6 +55,7 @@ SENTRY_NO_INIT
@property (nonatomic, strong) SentryFileManager *fileManager;
@property (nonatomic, strong) SentryAppStateManager *appStateManager;
@property (nonatomic, strong) SentryCrashWrapper *crashWrapper;
@property (nonatomic, strong) SentryCrash *crashReporter;
@property (nonatomic, strong) SentryThreadWrapper *threadWrapper;
@property (nonatomic, strong) id<SentryRandom> random;
@property (nonatomic, strong) SentrySwizzleWrapper *swizzleWrapper;
Expand Down
9 changes: 5 additions & 4 deletions Sources/SentryCrash/Installations/SentryCrashInstallation.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#import "SentryCrashJSONCodecObjC.h"
#import "SentryCrashLogger.h"
#import "SentryCrashReportFilterBasic.h"
#import "SentryDependencyContainer.h"
#import <objc/runtime.h>

/** Max number of properties that can be defined for writing to the report */
Expand Down Expand Up @@ -175,7 +176,7 @@ - (id)initWithRequiredProperties:(NSArray *)requiredProperties

- (void)dealloc
{
SentryCrash *handler = [SentryCrash sharedInstance];
SentryCrash *handler = SentryDependencyContainer.sharedInstance.crashReporter;
@synchronized(handler) {
if (g_crashHandlerData == self.crashHandlerData) {
g_crashHandlerData = NULL;
Expand Down Expand Up @@ -260,7 +261,7 @@ - (void)setOnCrash:(SentryCrashReportWriteCallback)onCrash

- (void)install
{
SentryCrash *handler = [SentryCrash sharedInstance];
SentryCrash *handler = SentryDependencyContainer.sharedInstance.crashReporter;
@synchronized(handler) {
g_crashHandlerData = self.crashHandlerData;
handler.onCrash = crashCallback;
Expand All @@ -270,7 +271,7 @@ - (void)install

- (void)uninstall
{
SentryCrash *handler = [SentryCrash sharedInstance];
SentryCrash *handler = SentryDependencyContainer.sharedInstance.crashReporter;
@synchronized(handler) {
if (g_crashHandlerData == self.crashHandlerData) {
g_crashHandlerData = NULL;
Expand Down Expand Up @@ -302,7 +303,7 @@ - (void)sendAllReportsWithCompletion:(SentryCrashReportFilterCompletion)onComple

sink = [SentryCrashReportFilterPipeline filterWithFilters:self.prependedFilters, sink, nil];

SentryCrash *handler = [SentryCrash sharedInstance];
SentryCrash *handler = SentryDependencyContainer.sharedInstance.crashReporter;
handler.sink = sink;
[handler sendAllReportsWithCompletion:onCompletion];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@

# ifdef __LP64__
# define MACH_ERROR_CODE_MASK 0xFFFFFFFFFFFFFFFF
# else
# else // !__LP64__
# define MACH_ERROR_CODE_MASK 0xFFFFFFFF
# endif
# endif // __LP64__

// ============================================================================
# pragma mark - Types -
Expand Down Expand Up @@ -267,7 +267,7 @@ sentrycrashcm_hasReservedThreads(void)
return g_primaryMachThread != 0 && g_secondaryMachThread != 0;
}

#else
#else // !SentryCrashCRASH_HAS_MACH
bool
sentrycrashcm_isReservedThread(thread_t thread)
{
Expand All @@ -280,7 +280,7 @@ sentrycrashcm_hasReservedThreads(void)
return false;
}

#endif
#endif // SentryCrashCRASH_HAS_MACH

#if SentryCrashCRASH_HAS_MACH

Expand Down Expand Up @@ -560,7 +560,7 @@ addContextualInfoToEvent(struct SentryCrash_MonitorContext *eventContext)
}
}

#endif
#endif // SentryCrashCRASH_HAS_MACH

SentryCrashMonitorAPI *
sentrycrashcm_machexception_getAPI(void)
Expand All @@ -570,7 +570,7 @@ sentrycrashcm_machexception_getAPI(void)
.setEnabled = setEnabled,
.isEnabled = isEnabled,
.addContextualInfoToEvent = addContextualInfoToEvent
#endif
#endif // SentryCrashCRASH_HAS_MACH
};
return &api;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "SentryCrashMonitorContext.h"
#import "SentryCrashStackCursor_Backtrace.h"
#include "SentryCrashThread.h"
#import "SentryDependencyContainer.h"

// #define SentryCrashLogger_LocalLevel TRACE
#import "SentryCrashLogger.h"
Expand Down Expand Up @@ -128,7 +129,8 @@

SentryCrashLOG_DEBUG(@"Setting new handler.");
NSSetUncaughtExceptionHandler(&handleUncaughtException);
SentryCrash.sharedInstance.uncaughtExceptionHandler = &handleUncaughtException;
SentryDependencyContainer.sharedInstance.crashReporter.uncaughtExceptionHandler
= &handleUncaughtException;
} else {
SentryCrashLOG_DEBUG(@"Restoring original handler.");
NSSetUncaughtExceptionHandler(g_previousUncaughtExceptionHandler);
Expand Down
4 changes: 0 additions & 4 deletions Sources/SentryCrash/Recording/SentryCrash.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,6 @@ static NSString *const SENTRYCRASH_REPORT_ATTACHMENTS_ITEM = @"attachments";

#pragma mark - API -

/** Get the singleton instance of the crash reporter.
*/
+ (SentryCrash *)sharedInstance;

/** Install the crash reporter.
* The reporter will record crashes, but will not send any crash reports unless
* sink is set.
Expand Down
9 changes: 0 additions & 9 deletions Sources/SentryCrash/Recording/SentryCrash.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,6 @@ @implementation SentryCrash
#pragma mark - Lifecycle -
// ============================================================================

+ (instancetype)sharedInstance
{
static SentryCrash *sharedInstance = nil;
static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{ sharedInstance = [[SentryCrash alloc] init]; });
return sharedInstance;
}

- (id)init
{
return [self initWithBasePath:[self getBasePath]];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class SentryCrashIntegrationTests: NotificationCenterTestCase {
}

// Test for GH-581
func testReleaseNamePassedToSentryCrash() {
func testReleaseNamePassedToSentryCrash() throws {
let releaseName = "1.0.0"
let dist = "14G60"
// The start of the SDK installs all integrations
Expand All @@ -80,20 +80,18 @@ class SentryCrashIntegrationTests: NotificationCenterTestCase {
}

// To test this properly we need SentryCrash and SentryCrashIntegration installed and registered on the current hub of the SDK.

let instance = SentryCrash.sharedInstance()
let userInfo = (instance?.userInfo ?? ["": ""]) as Dictionary

let userInfo = try XCTUnwrap(SentryDependencyContainer.sharedInstance().crashReporter.userInfo)
assertUserInfoField(userInfo: userInfo, key: "release", expected: releaseName)
assertUserInfoField(userInfo: userInfo, key: "dist", expected: dist)
}

func testContext_IsPassedToSentryCrash() {
func testContext_IsPassedToSentryCrash() throws {
SentrySDK.start { options in
options.dsn = SentryCrashIntegrationTests.dsnAsString
}

let instance = SentryCrash.sharedInstance()
let userInfo = (instance?.userInfo ?? ["": ""]) as Dictionary
let userInfo = try XCTUnwrap(SentryDependencyContainer.sharedInstance().crashReporter.userInfo)
let context = userInfo["context"] as? [String: Any]

assertContext(context: context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class SentryCrashReportTests: XCTestCase {
* UserInfo is picked up by the crash report when writing a new report.
*/
private func serializeToCrashReport(scope: Scope) {
SentryCrash.sharedInstance().userInfo = scope.serialize()
SentryDependencyContainer.sharedInstance().crashReporter.userInfo = scope.serialize()
}

private func deleteTestDir() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import SentryTestUtils
import XCTest

class SentryCrashScopeObserverTests: XCTestCase {
Expand All @@ -17,16 +18,9 @@ class SentryCrashScopeObserverTests: XCTestCase {

private let fixture = Fixture()

override func setUp() {
super.setUp()
sentrycrash_scopesync_reset()
SentryCrash.sharedInstance().userInfo = nil
}

override func tearDown() {
super.tearDown()
sentrycrash_scopesync_reset()
SentryCrash.sharedInstance().userInfo = nil
clearTestState()
}

func testUser() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class SentryCrashInstallationReporterTests: XCTestCase {
super.tearDown()
sentrycrash_deleteAllReports()
clearTestState()
sut.uninstall()
}

func testReportIsSentAndDeleted() throws {
Expand Down
Loading

0 comments on commit c471221

Please sign in to comment.