Skip to content

fix: drop screenshots for events filtered by before_send#2642

Draft
JoshuaMoelans wants to merge 2 commits intomainfrom
joshua/fix/screenshot_on_beforesend
Draft

fix: drop screenshots for events filtered by before_send#2642
JoshuaMoelans wants to merge 2 commits intomainfrom
joshua/fix/screenshot_on_beforesend

Conversation

@JoshuaMoelans
Copy link
Copy Markdown
Member

@JoshuaMoelans JoshuaMoelans commented Apr 13, 2026

Fixes #2641

Since screenshots await EndOfFrame() , we start a coroutine that ends up sending an envelope with just the screenshot item. However, in the meantime, the related event might have been discarded by the before_send filter. In this case, we don't want to send unnecessary data over to Relay.

As a temporary workaround, setting SetBeforeCaptureScreenshot to the same filter used in SetBeforeSend prevents these screenshots from being sent.

Proposed Fix

  • Add internal bool WasCaptured to SentryEvent (sentry-dotnet/SentryEvent.cs:249) — a flag that tracks whether an event actually made it through the entire send pipeline. Defaults to false, which is the safe default: if the flag is never set, the screenshot is skipped rather than sent as an orphan.

  • Set it to true on the success path in DoSendEvent (sentry-dotnet/SentryClient.cs:416) — placed immediately after CaptureEnvelope returns true, which is the only point where we know the event was actually sent. The flag is set on the original @event parameter (not processedEvent) because event processors and before_send can swap the event object, but the screenshot coroutine's closure holds a reference to the original. All five drop paths (exception filters, event processors, before_send, sampling, transport failure) return early and never reach this line, leaving the flag false.

  • Check the flag in the screenshot coroutine before capturing (ScreenshotEventProcessor.cs:48) — after WaitForEndOfFrame resumes the coroutine (at which point DoSendEvent has already completed synchronously), we read WasCaptured. If false, we yield break and skip the screenshot entirely. This check runs before any screenshot capture or CaptureAttachment call, preventing orphaned attachment envelopes from ever being created.


Related PRs:

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 13, 2026

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


Bug Fixes 🐛

  • Drop screenshots for events filtered by before_send by JoshuaMoelans in #2642

🤖 This preview updates automatically when you update the PR.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 13, 2026

Fails
🚫 Please consider adding a changelog entry for the next release.

Instructions and example for changelog

Please add an entry to CHANGELOG.md to the "Unreleased" section. Make sure the entry includes this PR's number.

Example:

## Unreleased

### Fixes

- drop screenshots for events filtered by `before_send` ([#2642](https://github.com/getsentry/sentry-unity/pull/2642))

If none of the above apply, you can opt out of this check by adding #skip-changelog to the PR description or adding a skip-changelog label.

Generated by 🚫 dangerJS against c772bfb

The previous test called processor.Process() directly, bypassing
DoSendEvent entirely. It passed after the WasCaptured fix only
because the flag defaults to false — not because DoSendEvent
actively left it false after before_send dropped the event.

The new tests register the screenshot processor via
AddEventProcessor and trigger events through SentrySdk.CaptureMessage,
so the event flows through the real pipeline. A positive control
test confirms screenshots are sent when events are captured.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unity SDK sends screenshot when the event is dropped via BeforeSend

1 participant