Skip to content

Commit

Permalink
add headerdoc explaining the test case, and explanation for another a…
Browse files Browse the repository at this point in the history
…ssertion
  • Loading branch information
armcknight committed May 12, 2023
1 parent c92fc91 commit 85209f0
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion Tests/SentryProfilerTests/SentryProfilerTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@ - (void)testProfilerMutationDuringSlicing
[self waitForExpectationsWithTimeout:1 handler:nil];
}

/**
* We received reports of crashes happening during serialization, which turned out to be caused by a
* data race in the collections we use to store profiler information, which are block-enumerated by
* NSJSONSerialization, which is not a thread-safe operation. So when the backtrace profiler
* modified the same collection from another thread while the block enumeration was in progress, a
* crash occurred. The solution is twofold:
* 1. copy the data structures so that serialization works with a new instance that will never be
* modified by the backtrace sampler thread
* 2. force exclusive access to the data structures so that they are never modified during any
* other operation, even the copy
*/
- (void)testProfilerMutationDuringSerialization
{
const auto resolvedThreadMetadata =
Expand Down Expand Up @@ -254,6 +265,7 @@ - (void)testProfilerMutationDuringSerialization
}

// cause the data structures to be modified again: overwrite previous thread metadata
// subdictionary contents
{
ThreadMetadata threadMetadata;
threadMetadata.name = "testThread-1";
Expand All @@ -276,7 +288,9 @@ - (void)testProfilerMutationDuringSerialization

// ensure the serialization's copied data structures don't contain the new addresses
NSArray<NSDictionary<NSString *, id> *> *frames = serialization[@"profile"][@"frames"];
XCTAssertEqual(frames.count, 3UL);
XCTAssertEqual(frames.count, 3UL,
@"New frames appeared in the data structure that should have been copied for serialization "
@"and should no longer be modifiable from the backtrace sampler thread.");

const auto index =
[frames indexOfObjectPassingTest:^BOOL(NSDictionary<NSString *, id> *_Nonnull obj,
Expand Down

0 comments on commit 85209f0

Please sign in to comment.