Skip to content

Commit aa0b738

Browse files
authored
Fix crash in file manager (#5535)
* Fix crash in file manager * Fix other nullable case * Fix tests * PR feedback
1 parent 5ec90e0 commit aa0b738

13 files changed

+48
-42
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixes
66

77
- Set handled to false for fatal app hangs (#5514)
8+
- Fix crash when SentryFileManger is nil (#5535)
89

910
### Improvements
1011

SentryTestUtils/ClearTestState.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class TestCleanup: NSObject {
3939
SentryAppStartTracker.load()
4040
SentryDependencyContainer.sharedInstance().uiViewControllerPerformanceTracker.alwaysWaitForFullDisplay = false
4141
SentryDependencyContainer.sharedInstance().swizzleWrapper.removeAllCallbacks()
42-
SentryDependencyContainer.sharedInstance().fileManager.clearDiskState()
42+
SentryDependencyContainer.sharedInstance().fileManager?.clearDiskState()
4343

4444
#endif // os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
4545

Sources/Sentry/SentryDependencyContainer.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ - (instancetype)init
210210
return self;
211211
}
212212

213-
- (SentryFileManager *)fileManager SENTRY_THREAD_SANITIZER_DOUBLE_CHECKED_LOCK
213+
- (nullable SentryFileManager *)fileManager SENTRY_THREAD_SANITIZER_DOUBLE_CHECKED_LOCK
214214
{
215215
SENTRY_LAZY_INIT(_fileManager, ({
216216
NSError *error;

Sources/Sentry/SentrySessionReplayIntegration.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,12 @@ - (void)startWithOptions:(SentryReplayOptions *)replayOptions
424424
options:replayOptions];
425425
}
426426

427-
- (NSURL *)replayDirectory
427+
- (nullable NSURL *)replayDirectory
428428
{
429429
NSString *sentryPath = [SentryDependencyContainer.sharedInstance.fileManager sentryPath];
430+
if (!sentryPath) {
431+
return nil;
432+
}
430433
NSURL *dir = [NSURL fileURLWithPath:sentryPath];
431434
return [dir URLByAppendingPathComponent:SENTRY_REPLAY_FOLDER];
432435
}

Sources/Sentry/include/HybridPublic/SentryDependencyContainer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ SENTRY_NO_INIT
104104

105105
#pragma mark - Lazy Dependencies
106106

107-
@property (nonatomic, strong) SentryFileManager *fileManager;
107+
@property (nonatomic, strong, nullable) SentryFileManager *fileManager;
108108
@property (nonatomic, strong) SentryAppStateManager *appStateManager;
109109
@property (nonatomic, strong) SentryThreadInspector *threadInspector;
110110
@property (nonatomic, strong) SentryFileIOTracker *fileIOTracker;

Sources/Swift/Persistence/SentryScopeContextPersistentStore.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
@_spi(Private) public class SentryScopeContextPersistentStore: NSObject {
1414
private let fileManager: SentryFileManagerProtocol
1515

16-
public init(fileManager: SentryFileManagerProtocol) {
16+
public init?(fileManager: SentryFileManagerProtocol?) {
17+
guard let fileManager else { return nil }
18+
1719
self.fileManager = fileManager
1820
}
1921

Tests/SentryTests/Helper/SentryFileManagerTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -850,9 +850,9 @@ class SentryFileManagerTests: XCTestCase {
850850

851851
func testReadPreviousBreadcrumbs() throws {
852852
let breadcrumbProcessor = SentryWatchdogTerminationBreadcrumbProcessor(maxBreadcrumbs: 2, fileManager: sut)
853-
let contextProcessor = SentryWatchdogTerminationContextProcessor(
853+
let contextProcessor = try SentryWatchdogTerminationContextProcessor(
854854
withDispatchQueueWrapper: SentryDispatchQueueWrapper(),
855-
scopeContextStore: SentryScopeContextPersistentStore(fileManager: sut)
855+
scopeContextStore: XCTUnwrap(SentryScopeContextPersistentStore(fileManager: sut))
856856
)
857857
let observer = SentryWatchdogTerminationScopeObserver(
858858
breadcrumbProcessor: breadcrumbProcessor,
@@ -880,9 +880,9 @@ class SentryFileManagerTests: XCTestCase {
880880

881881
func testReadPreviousBreadcrumbsCorrectOrderWhenFileTwoHasMoreCrumbs() throws {
882882
let breadcrumbProcessor = SentryWatchdogTerminationBreadcrumbProcessor(maxBreadcrumbs: 2, fileManager: sut)
883-
let contextProcessor = SentryWatchdogTerminationContextProcessor(
883+
let contextProcessor = try SentryWatchdogTerminationContextProcessor(
884884
withDispatchQueueWrapper: TestSentryDispatchQueueWrapper(),
885-
scopeContextStore: SentryScopeContextPersistentStore(fileManager: sut)
885+
scopeContextStore: XCTUnwrap(SentryScopeContextPersistentStore(fileManager: sut))
886886
)
887887
let observer = SentryWatchdogTerminationScopeObserver(
888888
breadcrumbProcessor: breadcrumbProcessor,

Tests/SentryTests/Integrations/WatchdogTerminations/SentryWatchdogTerminationScopeObserverTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class SentryWatchdogTerminationScopeObserverTests: XCTestCase {
3030
maxBreadcrumbs: 10,
3131
fileManager: fileManager
3232
)
33-
contextProcessor = TestSentryWatchdogTerminationContextProcessor(
33+
contextProcessor = try TestSentryWatchdogTerminationContextProcessor(
3434
withDispatchQueueWrapper: TestSentryDispatchQueueWrapper(),
35-
scopeContextStore: SentryScopeContextPersistentStore(fileManager: fileManager)
35+
scopeContextStore: XCTUnwrap(SentryScopeContextPersistentStore(fileManager: fileManager))
3636
)
3737
}
3838

Tests/SentryTests/Integrations/WatchdogTerminations/SentryWatchdogTerminationTrackerTests.swift

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase {
2626
let breadcrumbProcessor: SentryWatchdogTerminationBreadcrumbProcessor
2727
let contextProcessor: SentryWatchdogTerminationContextProcessor
2828

29-
init() {
29+
init() throws {
3030
SentryDependencyContainer.sharedInstance().sysctlWrapper = sysctl
3131
options = Options()
3232
options.maxBreadcrumbs = 2
@@ -37,7 +37,7 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase {
3737

3838
breadcrumbProcessor = SentryWatchdogTerminationBreadcrumbProcessor(maxBreadcrumbs: Int(options.maxBreadcrumbs), fileManager: fileManager)
3939
let backgroundQueueWrapper = TestSentryDispatchQueueWrapper()
40-
let scopeContextStore = SentryScopeContextPersistentStore(fileManager: fileManager)
40+
let scopeContextStore = try XCTUnwrap(SentryScopeContextPersistentStore(fileManager: fileManager))
4141
contextProcessor = SentryWatchdogTerminationContextProcessor(
4242
withDispatchQueueWrapper: backgroundQueueWrapper,
4343
scopeContextStore: scopeContextStore
@@ -51,11 +51,11 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase {
5151
SentrySDK.setCurrentHub(hub)
5252
}
5353

54-
func getSut() -> SentryWatchdogTerminationTracker {
55-
return getSut(fileManager: fileManager )
54+
func getSut() throws -> SentryWatchdogTerminationTracker {
55+
return try getSut(fileManager: fileManager )
5656
}
5757

58-
func getSut(fileManager: SentryFileManager) -> SentryWatchdogTerminationTracker {
58+
func getSut(fileManager: SentryFileManager) throws -> SentryWatchdogTerminationTracker {
5959
let appStateManager = SentryAppStateManager(
6060
options: options,
6161
crashWrapper: crashWrapper,
@@ -68,9 +68,9 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase {
6868
crashAdapter: crashWrapper,
6969
appStateManager: appStateManager
7070
)
71-
let scopePersistentStore = SentryScopeContextPersistentStore(
71+
let scopePersistentStore = try XCTUnwrap(SentryScopeContextPersistentStore(
7272
fileManager: fileManager
73-
)
73+
))
7474
return SentryWatchdogTerminationTracker(
7575
options: options,
7676
watchdogTerminationLogic: logic,
@@ -85,11 +85,11 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase {
8585
private var fixture: Fixture!
8686
private var sut: SentryWatchdogTerminationTracker!
8787

88-
override func setUp() {
89-
super.setUp()
88+
override func setUpWithError() throws {
89+
try super.setUpWithError()
9090

91-
fixture = Fixture()
92-
sut = fixture.getSut()
91+
fixture = try Fixture()
92+
sut = try fixture.getSut()
9393
SentrySDK.startInvocations = 1
9494
}
9595

@@ -101,8 +101,8 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase {
101101
clearTestState()
102102
}
103103

104-
func testStart_StoresAppState() {
105-
sut = fixture.getSut()
104+
func testStart_StoresAppState() throws {
105+
sut = try fixture.getSut()
106106

107107
XCTAssertNil(fixture.fileManager.readAppState())
108108

@@ -116,8 +116,8 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase {
116116
XCTAssertEqual(1, fixture.dispatchQueue.dispatchAsyncCalled)
117117
}
118118

119-
func testGoToForeground_SetsIsActive() {
120-
sut = fixture.getSut()
119+
func testGoToForeground_SetsIsActive() throws {
120+
sut = try fixture.getSut()
121121

122122
sut.start()
123123

@@ -248,8 +248,8 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase {
248248
assertNoOOMSent()
249249
}
250250

251-
func testDifferentBootTime_NoOOM() {
252-
sut = fixture.getSut()
251+
func testDifferentBootTime_NoOOM() throws {
252+
sut = try fixture.getSut()
253253
sut.start()
254254
let appState = SentryAppState(releaseName: fixture.options.releaseName ?? "", osVersion: UIDevice.current.systemVersion, vendorId: TestData.someUUID, isDebugging: false, systemBootTimestamp: fixture.sysctl.systemBootTimestamp.addingTimeInterval(1))
255255

@@ -260,7 +260,7 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase {
260260
}
261261

262262
func testAppWasInForeground_OOM() throws {
263-
sut = fixture.getSut()
263+
sut = try fixture.getSut()
264264

265265
sut.start()
266266
goToForeground()
@@ -283,7 +283,7 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase {
283283
}
284284

285285
func testAppOOM_WithBreadcrumbs() throws {
286-
sut = fixture.getSut()
286+
sut = try fixture.getSut()
287287

288288
let breadcrumb = TestData.crumb
289289
let sentryWatchdogTerminationScopeObserver = SentryWatchdogTerminationScopeObserver(
@@ -308,7 +308,7 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase {
308308
}
309309

310310
func testAppOOM_WithOnlyHybridSdkDidBecomeActive() throws {
311-
sut = fixture.getSut()
311+
sut = try fixture.getSut()
312312

313313
sut.start()
314314
hybridSdkDidBecomeActive()
@@ -319,7 +319,7 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase {
319319
}
320320

321321
func testAppOOM_Foreground_And_HybridSdkDidBecomeActive() throws {
322-
sut = fixture.getSut()
322+
sut = try fixture.getSut()
323323

324324
sut.start()
325325
goToForeground()
@@ -331,7 +331,7 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase {
331331
}
332332

333333
func testAppOOM_HybridSdkDidBecomeActive_and_Foreground() throws {
334-
sut = fixture.getSut()
334+
sut = try fixture.getSut()
335335

336336
sut.start()
337337
hybridSdkDidBecomeActive()
@@ -361,9 +361,9 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase {
361361
assertNoOOMSent()
362362
}
363363

364-
func testStop_StopsObserving_NoMoreFileManagerInvocations() {
364+
func testStop_StopsObserving_NoMoreFileManagerInvocations() throws {
365365
let fileManager = try! TestFileManager(options: Options())
366-
sut = fixture.getSut(fileManager: fileManager)
366+
sut = try fixture.getSut(fileManager: fileManager)
367367

368368
sut.start()
369369
sut.stop()

Tests/SentryTests/Integrations/WatchdogTerminations/SentryWatchdogTerminationTrackingIntegrationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class SentryWatchdogTerminationIntegrationTests: XCTestCase {
5454
container.appStateManager = appStateManager
5555
appStateManager.start()
5656

57-
let scopeContextPersistentStore = TestSentryScopeContextPersistentStore(fileManager: fileManager)
57+
let scopeContextPersistentStore = try XCTUnwrap(TestSentryScopeContextPersistentStore(fileManager: fileManager))
5858
watchdogTerminationContextProcessor = TestSentryWatchdogTerminationContextProcessor(
5959
withDispatchQueueWrapper: dispatchQueueWrapper,
6060
scopeContextStore: scopeContextPersistentStore

0 commit comments

Comments
 (0)