Skip to content

Spotlight transport shares and disposes envelope item streams with the inner transport #5499

Closed

Description

@jamescrosswell

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 using on processedEnvelope — those items belong to the caller.

@XAN9xXx mentioned having local regression tests that reproduce this, which would be a good starting point.

Activity

  1. added
    Bug
    on Aug 18, 2026
  2. linear-code commented on Aug 18, 2026

    @linear-code
  3. added
    .NET
    on Aug 18, 2026
  4. XAN9xXx commented on Aug 19, 2026

    @XAN9xXx
    Contributor

    Hi! I should be free in a day or two. If this is still available then, I'd be happy to take it.

  5. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Aug 19, 2026
  6. jamescrosswell commented on Aug 20, 2026

    @jamescrosswell
    CollaboratorAuthor

    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.

  7. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Aug 20, 2026
  8. XAN9xXx commented on Aug 20, 2026

    @XAN9xXx
    Contributor

    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 all IAttachmentContent implementations the same way. FileAttachmentContent and ByteAttachmentContent can provide independent streams, but StreamAttachmentContent can't — its GetStream() 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 IAttachmentContent it'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:

    • Only turn on replay/buffering for the multi-destination case, so the normal single-destination path doesn't pay for it at all.
    • Or go hybrid inside the payload layer — use independent stream creation where it's cheap (file-backed attachments), and only fall back to buffering when the source is genuinely single-consumer.

    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.

  9. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Aug 20, 2026
  10. jamescrosswell commented on Aug 23, 2026

    @jamescrosswell
    CollaboratorAuthor

    My current inclination is to explore the replayable payload route first

    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).

  11. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Aug 23, 2026
  12. added a commit that references this issue on Aug 31, 2026
    5da3920
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETAttachmentsBug

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions