Skip to content

Commit 7dcf986

Browse files
committed
[InMemory] Fix race condition in end() and isRecording observation
1 parent 71aa61e commit 7dcf986

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

Sources/InMemoryTracing/InMemorySpan.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,13 @@ public struct InMemorySpan: Span {
133133
}
134134

135135
public func end(at instant: @autoclosure () -> some TracerInstant) {
136-
guard isRecording else { return }
136+
let shouldRecord = _isRecording.withValue {
137+
let value = $0
138+
$0 = false // from here on after, stop recording
139+
return value
140+
}
141+
guard shouldRecord else { return }
142+
137143
let finishedSpan = FinishedInMemorySpan(
138144
operationName: operationName,
139145
context: context,
@@ -147,7 +153,6 @@ public struct InMemorySpan: Span {
147153
errors: errors,
148154
status: status
149155
)
150-
_isRecording.withValue { $0 = false }
151156
onEnd(finishedSpan)
152157
}
153158

0 commit comments

Comments
 (0)