     }
 
     public func end(time: Date) {
-        if hasEnded {
+        if endAtomic.exchange(true, ordering: .relaxed) {
             return
         }
+        eventsSyncLock.withLockVoid{
+            attributesSyncLock.withLockVoid{
+                isRecording = false
+            }
+        }
         endTime = time
-        hasEnded = true
         spanProcessor.onEnd(span: self)
-        isRecording = false
         scope?.close()
     }
 

    }

    public func setAttribute(key: String, value: AttributeValue?) {
        if hasEnded {
            return
        }

        attributesSyncLock.withLockVoid {
            if value == nil {
                attributes.removeValueForKey(key: key)
            }

    }

    private func addEvent(event: SpanData.Event) {
        eventsSyncLock.withLockVoid {
            if !isRecording {
                return
            }
            events.append(event)
            totalRecordedEvents += 1
        }
