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:
- transfers the sidecar to an envelope item that owns serialization and cleanup; or
- 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.
Problem
SentryClientcurrently 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 andbeforeSend*drops.Affected paths
Profiled transactions
A profiled transaction can return before
buildEnvelope(..., profilingTraceData)when:null;beforeSendTransactionreturnsnull; orbeforeSendTransactionthrows.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:
null;beforeSendReplayreturnsnull; orbeforeSendReplaythrows.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:
The implementation should:
event_processor,before_send, orcallback_error);Tests
Add real temporary-file regression coverage for:
beforeSendTransactionnull and exception;beforeSendReplaypaths;beforeSendReplaypaths; andDataCategory.Profileaccounting 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.