Repository navigation
fix(tracing): Serialize spans outside the SpanBatcher lock (#8023) - #8170
Closed
ansh-rohilla wants to merge 1 commit into
Closed
ansh-rohilla wants to merge 1 commit into
ansh-rohilla wants to merge 1 commit into
Conversation
…#8023) SpanBatcher._flush() previously held _lock while computing _dynamic_sampling_context() and serializing spans via _to_transport_format(). This blocked concurrent additions to the buffer and posed lock-order inversion hazards during serialization or GC finalizers, similar to what was resolved in getsentry#7776 for Batcher. - Detach pending trace buckets under _lock and serialize / construct envelopes outside the lock in SpanBatcher._flush(). - Move _record_lost_func call outside _lock in SpanBatcher.add(). - Add regression tests verifying the lock is not held during serialization, concurrent additions are unblocked, and dropped spans record losses outside the lock. Fixes getsentry#8023
ansh-rohilla
deleted the
fix/span-batcher-lock-outside-serialization-8023
branch
October 30, 2026 11:44
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In
SpanBatcher._flush(),self._lockwas held while calling_dynamic_sampling_context()and serializing spans via_to_transport_format(). This blocked concurrent additions to the buffer for the entire duration of serialization and posed potential lock-order inversion hazards with GC finalizers (similar to what was addressed in #7776 forBatcher).Additionally, in
SpanBatcher.add(),_record_lost_funcwas invoked while holdingself._lock.Solution
SpanBatcher._flush():self._lockviaself._span_buffer.pop(bucket_id, None)andself._running_size.pop(bucket_id, None).self._lockbefore constructing envelopes and serializing spans with_to_transport_format().SpanBatcher.add():_record_lost_funccall outsideself._lockwhen dropping spans due to queue overflow.tests/tracing/test_span_batcher.py:_to_transport_format()executes without holding_lock._record_lost_funcis invoked outside_lockon queue overflow.Fixes #8023