Repository navigation
Description
Description
Spotted by @XAN9xXx while working on #5481 (discussion). It predates that PR, so filing separately to keep #5481 focused on heap dump cleanup.
When Spotlight is enabled, SpotlightHttpTransport.SendEnvelopeAsync sends one Envelope to two destinations, and both consume the same EnvelopeItem instances — and therefore the same underlying Stream objects. There are two distinct problems.
1. Two consumers read the same stream concurrently
SpotlightHttpTransport.cs#L41 starts the inner send without awaiting it, then serializes the same items to Spotlight while that request is still in flight:
var sentryTask = _inner.SendEnvelopeAsync(envelope, cancellationToken); // hot task, not awaited
...
using var processedEnvelope = ProcessEnvelope(envelope); // same items
using var request = CreateRequest(processedEnvelope);
using var response = await _httpClient.SendAsync(request, cancellationToken)...Attachment payloads are a StreamSerializable wrapping a single stream (a FileStream, for FileAttachmentContent), and nothing ever rewinds it. Whichever consumer reads first drains it to EOF, so the other sends a zero-length attachment — and if the two reads interleave, both payloads are corrupt.
2. Items are disposed while the inner transport may still be using them
HttpTransportBase.ProcessEnvelope returns new Envelope(envelope.Header, envelopeItems) where envelopeItems holds the same EnvelopeItem references, not copies (HttpTransportBase.cs#L108). So the using on processedEnvelope at SpotlightHttpTransport.cs#L48 disposes items that sentryTask may still be reading: Envelope.Dispose() → Items.DisposeAll() → StreamSerializable.Dispose() → Source.Dispose(). Expect ObjectDisposedException or a truncated body on the Sentry request.
Those items are owned by the caller's envelope in any case — BackgroundWorker.DoWorkAsync already disposes the original via using var _ = envelope; — so the using here is disposing something it doesn't own.
Impact
Only stream-backed items are affected. JsonSerializable items re-serialize from the source object on each pass, so events, transactions and sessions are fine. Attachments are not: heap dumps, minidumps, view hierarchies and any user-supplied file or stream attachment can arrive empty or truncated at Sentry, at Spotlight, or both.
Limited to setups with Spotlight enabled, so this is a local-development-only fault — but it manifests as attachments silently going missing, with no error.
Possible directions
- Don't share item instances across the two sends — build the Spotlight envelope from
IAttachmentContent.GetStream()so each destination gets its own stream. (Note this reopens the file, so it isn't free for large attachments.) - Or buffer stream-backed payloads once and replay the buffer to both destinations.
- Or await the inner send before sending to Spotlight. That fixes the concurrency but not the drained stream, so it still needs one of the above.
- Independently of the above, drop the
usingonprocessedEnvelope— those items belong to the caller.
@XAN9xXx mentioned having local regression tests that reproduce this, which would be a good starting point.
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsNo status
Activity
linear-code commentedon Aug 18, 2026
NET-791
XAN9xXx commentedon Aug 19, 2026
Hi! I should be free in a day or two. If this is still available then, I'd be happy to take it.
jamescrosswell commentedon Aug 20, 2026
Hi @XAN9xXx - that would be much appreciated. What direction were you thinking? No. 1 is probably simpler (and I do like simple). Possibly number 2. could be more elegant - but if it's too complex, probably not worth it.
XAN9xXx commentedon Aug 20, 2026
Hi, thanks for the quick reply! Spent some time digging into the attachment flow.
I'm currently leaning towards No.2, though I get why No.1 is appealing from a simplicity standpoint.
My main worry with No.1 (
GetStream()per destination) is that it doesn't cover allIAttachmentContentimplementations the same way.FileAttachmentContentandByteAttachmentContentcan provide independent streams, butStreamAttachmentContentcan't — itsGetStream()just returns whatever stream the caller passed in. There's nothing to "reopen" there, since we never created that source stream ourselves.So going with No.1 would probably mean bolting on a separate strategy just for that case anyway (likely buffering the stream once), which means the implementation ends up branching on what kind of
IAttachmentContentit's dealing with. That's really what's nudging me toward a replayable payload layer instead — it keeps the transport layer out of the business of caring where the attachment came from, and every payload gets treated the same way.That said, I'm with you on simplicity mattering here, and I'm not thrilled about the idea of just buffering a multi-hundred-MB minidump into memory the moment Spotlight happens to be on. A couple ways we could keep that cost in check:
Either way, I think points 3 and 4 from your list — awaiting the inner send first, and revisiting the disposal ownership of
processedEnvelope— should just get fixed regardless. They're correctness bugs on their own and don't really conflict with whichever direction we pick for the stream problem.My current inclination is to explore the replayable payload route first, but happy to just go with the simpler No.1 if you don't think the extra abstraction is worth it here. Once we land on a direction I can start putting together the regression tests and the fix.
jamescrosswell commentedon Aug 23, 2026
Let's see what that looks like. For the file/byte streams - it is quite nice that they can hand you separate handles to read them without duplicating a bunch of stuff in memory... which I assume is what StreamAttachmentContent is going to have to do in order to provide a replayable stream. It would be either that or a temp file. See how you get on anyway.
We should definitely avoid negatively impacting the more common/runtime path (where Spotlight is disabled).
fix: don't dispose borrowed envelope items in the transports
fix: make attachment payloads replayable (getsentry#5511)