Skip to content

Commit

Permalink
fix: check for null value returned from pthread_mach_thread_np
Browse files Browse the repository at this point in the history
  • Loading branch information
armcknight committed Nov 23, 2023
1 parent cb6ab62 commit 73bd871
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 15 deletions.
9 changes: 5 additions & 4 deletions Sources/Sentry/PrivateSentrySDKOnly.mm
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,11 @@ + (uint64_t)startProfilerForTrace:(SentryId *)traceId;

if (payload != nil) {
payload[@"platform"] = SentryPlatformName;
payload[@"transaction"] = @{
@"active_thread_id" :
[NSNumber numberWithLongLong:sentry::profiling::ThreadHandle::current()->tid()]
};
const auto thread = sentry::profiling::ThreadHandle::current();
if (thread != nullptr) {
payload[@"transaction"] =
@{ @"active_thread_id" : [NSNumber numberWithLongLong:thread->tid()] };
}
}

return payload;
Expand Down
6 changes: 5 additions & 1 deletion Sources/Sentry/SentryProfiler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -428,12 +428,16 @@ + (void)updateProfilePayload:(NSMutableDictionary<NSString *, id> *)payload
forTransaction:(SentryTransaction *)transaction;
{
payload[@"platform"] = transaction.platform;

const auto activeThreadID =
[transaction.trace.transactionContext sentry_threadInfo].threadId ?: @(-1);
payload[@"transaction"] = @{
@"id" : transaction.eventId.sentryIdString,
@"trace_id" : transaction.trace.traceId.sentryIdString,
@"name" : transaction.transaction,
@"active_thread_id" : [transaction.trace.transactionContext sentry_threadInfo].threadId
@"active_thread_id" : activeThreadID
};

const auto timestamp = transaction.trace.originalStartTimestamp;
if (UNLIKELY(timestamp == nil)) {
SENTRY_LOG_WARN(@"There was no start timestamp on the provided transaction. Falling back "
Expand Down
3 changes: 3 additions & 0 deletions Sources/Sentry/SentryThreadHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ namespace profiling {
ThreadHandle::current() noexcept
{
const auto thread = pthread_mach_thread_np(pthread_self());
if (thread == (mach_port_t)NULL) {
return nullptr;
}
return std::make_unique<ThreadHandle>(thread);
}

Expand Down
18 changes: 13 additions & 5 deletions Sources/Sentry/SentryTransactionContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,31 @@ - (instancetype)initWithName:(NSString *)name
_name = [NSString stringWithString:name];
_nameSource = source;
self.parentSampled = parentSampled;
#if SENTRY_TARGET_PROFILING_SUPPORTED
[self getThreadInfo];
#endif // SENTRY_TARGET_PROFILING_SUPPORTED
}
return self;
}

#if SENTRY_TARGET_PROFILING_SUPPORTED
- (void)getThreadInfo
{
#if SENTRY_TARGET_PROFILING_SUPPORTED
const auto threadID = sentry::profiling::ThreadHandle::current()->tid();
const auto thread = sentry::profiling::ThreadHandle::current();
if (thread == nullptr) {
return;
}
const auto threadID = thread->tid();
self.threadInfo = [[SentryThread alloc] initWithThreadId:@(threadID)];
#endif
}
#endif // SENTRY_TARGET_PROFILING_SUPPORTED

#if SENTRY_TARGET_PROFILING_SUPPORTED
- (SentryThread *)sentry_threadInfo
- (nullable SentryThread *)sentry_threadInfo
{
return self.threadInfo;
}
#endif
#endif // SENTRY_TARGET_PROFILING_SUPPORTED

- (void)commonInitWithName:(NSString *)name
source:(SentryTransactionNameSource)source
Expand All @@ -143,7 +149,9 @@ - (void)commonInitWithName:(NSString *)name
_name = [NSString stringWithString:name];
_nameSource = source;
self.parentSampled = parentSampled;
#if SENTRY_TARGET_PROFILING_SUPPORTED
[self getThreadInfo];
#endif // SENTRY_TARGET_PROFILING_SUPPORTED
SENTRY_LOG_DEBUG(@"Created transaction context with name %@", name);
}

Expand Down
11 changes: 7 additions & 4 deletions Sources/Sentry/include/SentryTransactionContext+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ SentryTransactionContext ()
parentSampled:(SentrySampleDecision)parentSampled;

#if SENTRY_TARGET_PROFILING_SUPPORTED

// This is currently only exposed for testing purposes, see -[SentryProfilerTests
// testProfilerMutationDuringSerialization]
@property (nonatomic, strong) SentryThread *threadInfo;
// testProfilerMutationDuringSerialization]. Can be null if there was a problem
// getting the current thread info from the underlying API.
@property (nonatomic, strong, nullable) SentryThread *threadInfo;

- (nullable SentryThread *)sentry_threadInfo;

- (SentryThread *)sentry_threadInfo;
#endif
#endif // SENTRY_TARGET_PROFILING_SUPPORTED

@end

Expand Down
2 changes: 1 addition & 1 deletion Tests/SentryProfilerTests/SentryProfilerSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ private extension SentryProfilerSwiftTests {
XCTAssertNotEqual(SentryId.empty, linkedTransactionTraceId)

let activeThreadId = try XCTUnwrap(linkedTransactionInfo["active_thread_id"] as? NSNumber)
XCTAssertEqual(activeThreadId, latestTransaction.trace.transactionContext.sentry_threadInfo().threadId)
XCTAssertEqual(activeThreadId, try XCTUnwrap(latestTransaction.trace.transactionContext.sentry_threadInfo()).threadId)

for sample in samples {
let timestamp = try XCTUnwrap(sample["elapsed_since_start_ns"] as? NSString)
Expand Down

0 comments on commit 73bd871

Please sign in to comment.