fix: Heap dump files are now deleted from disk once they have been sent to Sentry - #5481
Conversation
jamescrosswell
left a comment
There was a problem hiding this comment.
Thanks for the PR @XAN9xXx . I think it might not have been clear from the issue what we were trying to achieve though.
Your current PR would only delete heap dumps if they exceeded the maximum size and we were unable to send these to Sentry. Other heap dumps that are successfully sent to Sentry would stay on disk forever.
We originally wanted to remove any heap dumps that had been successfully sent to Sentry - these are no longer required as they're now available in the Sentry dashboard.
Bruno's comment was that we should also remove heap dumps that we failed to send, since (and especially since these are large) these could easily exhaust available disk space on some servers.
BackgroundWorker.DoWorkAsync does using var _ = envelope around the send, so the envelope — and the FileStream — is disposed once the background worker has finished with it... using FileOptions.DeleteOnClose (only for heap dumps) might be a good way to deal with this.
Using FileOptions.DeleteOnClose also has the advantage that it would allow cleaning up gcdump files even when CacheDirectoryPath is set:
- Worker awaits _transport.SendEnvelopeAsync(envelope) → CachingTransport.StoreToCacheAsync
- That awaits envelope.SerializeAsync(stream, …) (CachingTransport.cs:486), which copies the .gcdump bytes into the cache file
- Returns → worker's using disposes the envelope → FileStream closes → dump deleted
So the original is only removed once a copy exists in the cache directory. The CachingTransport send reads from that copy, and InnerProcessCacheAsync deletes the cache file afterwards.
|
Thanks for the explanation @jamescrosswell , that clarifies things a lot — makes sense to use My laptop is currently being repaired, so I likely won't get back to this for a few days. Will follow up once I'm back! |
|
@jamescrosswell Reworked this using |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 5d51697. Configure here.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #5481 +/- ##
==========================================
+ Coverage 74.73% 74.76% +0.02%
==========================================
Files 513 513
Lines 18744 18759 +15
Branches 3666 3669 +3
==========================================
+ Hits 14009 14025 +16
Misses 3863 3863
+ Partials 872 871 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
jamescrosswell
left a comment
There was a problem hiding this comment.
I added a few tests and made a couple of tweaks to align with the coding styles in the repo (hope you don't mind).
Thank you very much for the contribution and integrating all the feedback @XAN9xXx !
|
Thanks for the review and for helping improve the PR! Glad to contribute. |

Description
Heap dump (
.gcdump) files created by the SDK were left on disk after being processed. This affected both successfully sent heap dumps and oversized heap dumps that were dropped before sending, potentially causing these files to accumulate over time.This PR introduces a dedicated
AttachmentType.HeapDumpand opens the corresponding file streams withFileOptions.DeleteOnClose. Cleanup is therefore tied to the attachment stream and envelope lifecycle:Changes
AttachmentType.HeapDump.CaptureHeapDumpwith the new attachment type.deleteOnClosesupport toFileAttachmentContent.SentryHintto enableFileOptions.DeleteOnCloseonly for heap dump attachments.event.heapdumpenvelope attachment-type mapping.Testing
Added tests verifying that:
The separate Spotlight envelope-item stream-sharing issue discovered during this work is tracked in #5499 .
Fixes #4009