fix(tracing): Fix memory leak in SGP tracing processors#302
Merged
Conversation
SGPSyncTracingProcessor and SGPAsyncTracingProcessor accumulated spans in self._spans dict on every request but never removed them, since on_span_end() used dict.get() (read-only) instead of dict.pop() (read-and-remove). The only cleanup was in shutdown() which is never called. After this fix, spans are removed from the dict when they complete, preventing unbounded memory growth.
0604794 to
97bcaf4
Compare
tests/lib/core/tracing/processors/test_sgp_tracing_processor.py
Outdated
Show resolved
Hide resolved
danielmillerp
approved these changes
Apr 3, 2026
@patch decorators on _make_processor expired before test bodies ran, so on_span_start/on_span_end hit the real create_span and flush. Refactored to @staticmethod with 'with patch(...)' context managers matching the async test class pattern.
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.
Summary
SGPSyncTracingProcessorandSGPAsyncTracingProcessorwhereself._spansdict grew linearly with every request and never shrankon_span_end()useddict.get()(read-only) instead ofdict.pop()(read-and-remove), so completed spans were never cleaned upshutdown()was the only cleanup path but is never called anywhere in the codebaseChanges
self._spans.get(span.id)→self._spans.pop(span.id, None)in bothSGPSyncTracingProcessor.on_span_end()andSGPAsyncTracingProcessor.on_span_end()Test plan
Greptile Summary
This PR fixes a genuine memory leak in
SGPSyncTracingProcessorandSGPAsyncTracingProcessorby changingself._spans.get(span.id)toself._spans.pop(span.id, None)in each processor'son_span_end()method. The fix is correct, minimal, and well-targeted — completed spans are now removed fromself._spansas soon as they finish rather than accumulating indefinitely.Key changes:
sgp_tracing_processor.py:.get()→.pop()in bothSGPSyncTracingProcessor.on_span_endandSGPAsyncTracingProcessor.on_span_end__init__.pyfiles to make test directories proper packagesNotes:
asyncio_mode = "auto"is set inpyproject.toml, so the async test methods run correctly without needing per-test@pytest.mark.asynciomarkers_make_processorhelpers now correctly usewith patch(...)context managers (not decorator-style), and test bodies re-patchcreate_span— the pattern is consistent between sync and async classesConfidence Score: 5/5
Safe to merge — the fix is a minimal, correct one-method change in each processor with no API surface impact.
The core change is a single well-understood substitution (.get() → .pop()) that directly and correctly addresses the described leak. asyncio_mode = auto is confirmed in pyproject.toml so async tests are actually exercised. All remaining observations are pre-existing or already flagged in prior review threads, leaving no new P0/P1 findings in this revision.
No files require special attention.
Important Files Changed
Sequence Diagram
sequenceDiagram participant C as Caller participant P as SGP[Sync|Async]TracingProcessor participant S as self._spans (dict) participant SGP as SGPClient C->>P: on_span_start(span) P->>SGP: create_span(...) SGP-->>P: sgp_span P->>SGP: flush / upsert_batch P->>S: self._spans[span.id] = sgp_span Note over S: span lives in dict while active C->>P: on_span_end(span) P->>S: pop(span.id, None) ← FIX (was .get()) S-->>P: sgp_span (removed from dict) P->>SGP: flush / upsert_batch (with end_time) Note over S: dict entry gone — no leakReviews (2): Last reviewed commit: "fix(tests): Use context manager patches ..." | Re-trigger Greptile