Skip to content

Clean up profile and replay artifacts on pre-envelope drops #6153

Description

@adinauer

Problem

SentryClient currently relies on lazy envelope-item serialization to clean up file-backed transaction profiles and replay recordings. If capture terminates after those sidecars exist but before envelope construction, serialization never takes ownership and the files are not promptly deleted.

This affects both EventProcessor and beforeSend* drop paths. PR #6142 adds new fail-closed exits when an EventProcessor throws, but the same ownership gap already exists for intentional processor drops and beforeSend* drops.

Affected paths

Profiled transactions

A profiled transaction can return before buildEnvelope(..., profilingTraceData) when:

  • an EventProcessor returns null;
  • an EventProcessor throws and the SDK fails closed;
  • beforeSendTransaction returns null; or
  • beforeSendTransaction throws.

The profile trace is normally deleted by the lazy serializer created in SentryEnvelopeItem.fromProfilingTrace. These early returns prevent that cleanup, allowing sensitive profile data and disk usage to accumulate in long-running Android apps or JVM services.

Session replays

A replay can return before replay envelope construction when:

  • an EventProcessor returns null;
  • an EventProcessor throws and the SDK fails closed;
  • beforeSendReplay returns null; or
  • beforeSendReplay throws.

Replay cleanup currently occurs in the serializer created by SentryEnvelopeItem.fromReplay. Bypassing it can retain the replay video or, for a backfilled replay, the entire replay folder.

Expected behavior

Introduce explicit ownership for file-backed sidecars so every terminal pre-envelope drop either:

  1. transfers the sidecar to an envelope item that owns serialization and cleanup; or
  2. disposes of the sidecar immediately at the capture boundary.

The implementation should:

  • delete abandoned profile trace files;
  • delete normal replay videos and the correct backfilled replay folder;
  • record one dropped profile with the same discard reason as its transaction (event_processor, before_send, or callback_error);
  • avoid double deletion and double accounting; and
  • preserve existing replay-folder semantics and transaction/span discard quantities.

Tests

Add real temporary-file regression coverage for:

  • profiled transactions dropped by processor null and processor exception;
  • profiled transactions dropped by beforeSendTransaction null and exception;
  • normal replay videos dropped by processor and beforeSendReplay paths;
  • backfilled replay folders dropped by processor and beforeSendReplay paths; and
  • DataCategory.Profile accounting under each corresponding discard reason.

Context

This was identified while reviewing #6142. The fail-closed processor behavior is correct; the issue is that cleanup is coupled to envelope serialization even though several valid terminal paths return before an envelope is built.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions