Skip to content

Commit

Permalink
fix: Crash in AppHangs when no threads (#2725)
Browse files Browse the repository at this point in the history
Fix a crash (index 0 beyond bounds for empty array) in the AppHangs logic
when creating the app hang event, and the SDK can't retrieve threads by
not reporting an AppHang in such a case.

Fixes GH-2713
  • Loading branch information
philipphofmann authored Feb 27, 2023
1 parent efb2222 commit 28333b6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 22 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Crash in AppHangs when no threads (#2725)

## 8.2.0

### Features
Expand Down
12 changes: 9 additions & 3 deletions Sources/Sentry/SentryANRTrackingIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#import "SentryEvent.h"
#import "SentryException.h"
#import "SentryHub+Private.h"
#import "SentryLog.h"
#import "SentryMechanism.h"
#import "SentrySDK+Private.h"
#import "SentryStacktrace.h"
Expand Down Expand Up @@ -58,11 +59,16 @@ - (void)anrDetected
{
SentryThreadInspector *threadInspector = SentrySDK.currentHub.getClient.threadInspector;

NSString *message = [NSString stringWithFormat:@"App hanging for at least %li ms.",
(long)(self.options.appHangTimeoutInterval * 1000)];

NSArray<SentryThread *> *threads = [threadInspector getCurrentThreadsWithStackTrace];

if (threads.count == 0) {
SENTRY_LOG_WARN(@"Getting current thread returned an empty list. Can't create AppHang "
@"event without a stacktrace.");
return;
}

NSString *message = [NSString stringWithFormat:@"App hanging for at least %li ms.",
(long)(self.options.appHangTimeoutInterval * 1000)];
SentryEvent *event = [[SentryEvent alloc] initWithLevel:kSentryLevelError];
SentryException *sentryException = [[SentryException alloc] initWithValue:message
type:@"App Hanging"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ class SentryANRTrackingIntegrationTests: SentrySDKIntegrationTestsBase {
XCTAssertTrue(threadsWithFrames > 1, "Not enough threads with frames")
}
}

func testANRDetected_ButNoThreads_EventNotCaptured() {
givenInitializedTracker()
setUpThreadInspector(addThreads: false)

Dynamic(sut).anrDetected()

assertNoEventCaptured()
}

private func givenInitializedTracker(isBeingTraced: Bool = false) {
givenSdkWithHub()
Expand All @@ -106,27 +115,32 @@ class SentryANRTrackingIntegrationTests: SentrySDKIntegrationTestsBase {
sut.install(with: self.options)
}

private func setUpThreadInspector() {
private func setUpThreadInspector(addThreads: Bool = true) {
let threadInspector = TestThreadInspector.instance

let frame1 = Sentry.Frame()
frame1.function = "Second_frame_function"

let thread1 = SentryThread(threadId: 0)
thread1.stacktrace = SentryStacktrace(frames: [frame1], registers: [:])
thread1.current = true

let frame2 = Sentry.Frame()
frame2.function = "main"

let thread2 = SentryThread(threadId: 1)
thread2.stacktrace = SentryStacktrace(frames: [frame2], registers: [:])
thread2.current = false

threadInspector.allThreads = [
thread2,
thread1
]
if addThreads {

let frame1 = Sentry.Frame()
frame1.function = "Second_frame_function"

let thread1 = SentryThread(threadId: 0)
thread1.stacktrace = SentryStacktrace(frames: [frame1], registers: [:])
thread1.current = true

let frame2 = Sentry.Frame()
frame2.function = "main"

let thread2 = SentryThread(threadId: 1)
thread2.stacktrace = SentryStacktrace(frames: [frame2], registers: [:])
thread2.current = false

threadInspector.allThreads = [
thread2,
thread1
]
} else {
threadInspector.allThreads = []
}

SentrySDK.currentHub().getClient()?.threadInspector = threadInspector
}
Expand Down
8 changes: 8 additions & 0 deletions Tests/SentryTests/SentrySDKIntegrationTestsBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ class SentrySDKIntegrationTestsBase: XCTestCase {
SentrySDK.setCurrentHub(SentryHub(client: nil, andScope: nil))
}

func assertNoEventCaptured() {
guard let client = SentrySDK.currentHub().getClient() as? TestClient else {
XCTFail("Hub Client is not a `TestClient`")
return
}
XCTAssertEqual(0, client.captureEventInvocations.count, "No event should be captured.")
}

func assertEventCaptured(_ callback: (Event?) -> Void) {
guard let client = SentrySDK.currentHub().getClient() as? TestClient else {
XCTFail("Hub Client is not a `TestClient`")
Expand Down

0 comments on commit 28333b6

Please sign in to comment.