Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix iOS tracer concurrency #535

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/core/ios/Sources/DdTraceImplementation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ public class DdTraceImplementation: NSObject {
public func finishSpan(spanId: NSString, context: NSDictionary, timestampMs: Double, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
objc_sync_enter(self)
let optionalSpan = spanDictionary.removeValue(forKey: spanId)
objc_sync_exit(self)

if let span = optionalSpan {
set(tags: castAttributesToSwift(context).mergeWithGlobalAttributes(), to: span)
let timeIntervalSince1970: TimeInterval = timestampMs / 1_000
span.finish(at: Date(timeIntervalSince1970: timeIntervalSince1970))
}

objc_sync_exit(self)

resolve(nil)
}

Expand Down
6 changes: 5 additions & 1 deletion packages/core/ios/Tests/DdTraceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ internal class DdTraceTests: XCTestCase {
}

func testTracingConcurrently() {
// It is possible and acceptable that a concurrent finishSpan resolve is called on a different thread before we get the lastResolveValue.
// This would override lastResolveValue to be `nil`. To avoid this, we use a different resolve.
func mockFinishResolve(args: Any?) {}

let iterationCount = 30
DispatchQueue.concurrentPerform(iterations: iterationCount) { iteration in
tracer.startSpan(
Expand All @@ -150,7 +154,7 @@ internal class DdTraceTests: XCTestCase {
reject: mockReject
)
let spanID = lastResolveValue as! NSString
tracer.finishSpan(spanId: spanID, context: testTags, timestampMs: 100, resolve: mockResolve, reject: mockReject)
tracer.finishSpan(spanId: spanID, context: testTags, timestampMs: 100, resolve: mockFinishResolve, reject: mockReject)
}

XCTAssertEqual(mockNativeTracer.startedSpans.count, iterationCount, "\(mockNativeTracer.startedSpans)")
Expand Down