diff --git a/CHANGELOG.md b/CHANGELOG.md index 01be011d55d..8914989b720 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixes + +- Crash in SentryOutOfMemoryScopeObserver (#2557) + ## 7.31.4 ### Fixes diff --git a/Sources/Sentry/SentryOutOfMemoryScopeObserver.m b/Sources/Sentry/SentryOutOfMemoryScopeObserver.m index 713202fc9f5..b260c4bc0f7 100644 --- a/Sources/Sentry/SentryOutOfMemoryScopeObserver.m +++ b/Sources/Sentry/SentryOutOfMemoryScopeObserver.m @@ -75,15 +75,22 @@ - (void)switchFileHandle - (void)store:(NSData *)data { - [self.fileHandle seekToEndOfFile]; - [self.fileHandle writeData:data]; - [self.fileHandle writeData:[@"\n" dataUsingEncoding:NSASCIIStringEncoding]]; - - self.breadcrumbCounter += 1; - - if (self.breadcrumbCounter >= self.maxBreadcrumbs) { - [self switchFileHandle]; - self.breadcrumbCounter = 0; + unsigned long long fileSize; + @try { + fileSize = [self.fileHandle seekToEndOfFile]; + + [self.fileHandle writeData:data]; + [self.fileHandle writeData:[@"\n" dataUsingEncoding:NSASCIIStringEncoding]]; + + self.breadcrumbCounter += 1; + } @catch (NSException *exception) { + SENTRY_LOG_ERROR(@"Error while writing data to end file with size (%llu): %@ ", fileSize, + exception.description); + } @finally { + if (self.breadcrumbCounter >= self.maxBreadcrumbs) { + [self switchFileHandle]; + self.breadcrumbCounter = 0; + } } } diff --git a/Sources/Sentry/include/SentryOutOfMemoryScopeObserver.h b/Sources/Sentry/include/SentryOutOfMemoryScopeObserver.h index da27299c823..eb708ffda0f 100644 --- a/Sources/Sentry/include/SentryOutOfMemoryScopeObserver.h +++ b/Sources/Sentry/include/SentryOutOfMemoryScopeObserver.h @@ -5,8 +5,12 @@ NS_ASSUME_NONNULL_BEGIN -/// This scope observer is used by the Out of Memory integration to write breadcrumbs to disk. -/// The overhead is ~0.015 seconds for 1000 breadcrumbs. +/** + * This scope observer is used by the Out of Memory integration to write breadcrumbs to disk. + * The overhead is ~0.015 seconds for 1000 breadcrumbs. + * This class doesn't need to be thread safe as the scope already calls the scope observers in a + * thread safe manner. + */ @interface SentryOutOfMemoryScopeObserver : NSObject SENTRY_NO_INIT diff --git a/Tests/SentryTests/Integrations/OutOfMemory/SentryOutOfMemoryScopeObserverTests.swift b/Tests/SentryTests/Integrations/OutOfMemory/SentryOutOfMemoryScopeObserverTests.swift index c26e10ad1a4..eb36a5d2fca 100644 --- a/Tests/SentryTests/Integrations/OutOfMemory/SentryOutOfMemoryScopeObserverTests.swift +++ b/Tests/SentryTests/Integrations/OutOfMemory/SentryOutOfMemoryScopeObserverTests.swift @@ -1,17 +1,22 @@ import XCTest class SentryOutOfMemoryScopeObserverTests: XCTestCase { + + private static let dsn = TestConstants.dsnAsString(username: "SentryOutOfMemoryScopeObserverTests") + private class Fixture { let breadcrumb: Breadcrumb let options: Options let fileManager: SentryFileManager let currentDate = TestCurrentDateProvider() + let maxBreadcrumbs = 10 init() { breadcrumb = TestData.crumb breadcrumb.data = nil options = Options() + options.dsn = SentryOutOfMemoryScopeObserverTests.dsn fileManager = try! SentryFileManager(options: options, andCurrentDateProvider: currentDate) } @@ -20,7 +25,7 @@ class SentryOutOfMemoryScopeObserverTests: XCTestCase { } func getSut(fileManager: SentryFileManager) -> SentryOutOfMemoryScopeObserver { - return SentryOutOfMemoryScopeObserver(maxBreadcrumbs: 10, fileManager: fileManager) + return SentryOutOfMemoryScopeObserver(maxBreadcrumbs: maxBreadcrumbs, fileManager: fileManager) } } @@ -85,7 +90,7 @@ class SentryOutOfMemoryScopeObserverTests: XCTestCase { XCTAssertEqual(fileTwoLines.count, 1) // Store 10 more - for _ in 0..<10 { + for _ in 0.. UnsafeMutablePointer?) -> String? { + private func getScopeJson(getField: (SentryCrashScope) -> UnsafeMutablePointer?) -> String? { guard let scopePointer = sentrycrash_scopesync_getScope() else { return nil } diff --git a/Tests/SentryTests/SentryScopeSwiftTests.swift b/Tests/SentryTests/SentryScopeSwiftTests.swift index 263e1615693..a054bef45db 100644 --- a/Tests/SentryTests/SentryScopeSwiftTests.swift +++ b/Tests/SentryTests/SentryScopeSwiftTests.swift @@ -221,7 +221,7 @@ class SentryScopeSwiftTests: XCTestCase { func testUseSpanForClear() { fixture.scope.span = fixture.transaction - fixture.scope.useSpan { (span) in + fixture.scope.useSpan { (_) in self.fixture.scope.span = nil } XCTAssertNil(fixture.scope.span)