fix(profiling): release the EventPipe session when the SDK shuts down - #5470
fix(profiling): release the EventPipe session when the SDK shuts down#5470jamescrosswell wants to merge 7 commits into
Conversation
SamplingTransactionProfilerFactory.Dispose() disposed the antecedent Task<SampleProfilerSession> rather than the session it wraps. Fixing that alone changes nothing observable, because two further defects sit between "SDK shuts down" and "EventPipe session released": - SampleProfilerSession.Stop() built _processing as an OnlyOnFaulted continuation, which transitions to Canceled when Process() returns normally - so Wait() threw on every clean shutdown and the disposals after it were never reached. - ProfilingIntegration was not IDisposable, so Hub never registered it for cleanup and the factory's Dispose() was only ever called by tests. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #5470 +/- ##
==========================================
+ Coverage 74.71% 74.74% +0.03%
==========================================
Files 513 513
Lines 18729 18813 +84
Branches 3663 3677 +14
==========================================
+ Hits 13993 14062 +69
- Misses 3865 3872 +7
- Partials 871 879 +8 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Co-authored-by: James Crosswell <jamescrosswell@users.noreply.github.com>
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 27932ec. Configure here.
Both review bots flagged the same window from different angles, and both were right: - The startup task read _shutdownCts.Token, which throws ObjectDisposedException once Dispose() has disposed the CTS. The token is now captured up front so the task never touches the source. - StartEventPipeSession() can block for up to 30s and isn't cancellable, so Dispose() could run before _session was ever assigned - disposing nothing and leaving the session that arrived later running forever. A lock now hands the session between the two sides so exactly one of them stops it. Adds test seams to SampleProfilerSession so the race can be exercised deterministically rather than by timing. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…e' into fix/5418-profiler-session-dispose # Conflicts: # src/Sentry.Profiling/SamplingTransactionProfilerFactory.cs
Co-authored-by: James Crosswell <jamescrosswell@users.noreply.github.com>
Documents the convention in AGENTS.md: this repo favours code that needs no comments, with the code and the PR description as the documentation. Minimal comments only where something genuinely isn't obvious. Applies it to this branch - 34 added comment lines down to 12. What survives is non-intuitive framework behaviour (a ContinueWith whose criteria aren't met is Canceled; reading CancellationTokenSource.Token after Dispose throws) and the justification for an empty catch. Test rationale moved into FluentAssertions "because" strings, where it shows up in failure output instead of sitting in a comment. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…e' into fix/5418-profiler-session-dispose

SamplingTransactionProfilerFactory.Dispose()disposed the antecedentTask<SampleProfilerSession>rather than the session it wraps, so theEventPipeSessionwas never stopped.Tracing it through, that line is the last of three defects sitting between "SDK shuts down" and "EventPipe session released" — fixing it alone would not have changed anything observable:
SampleProfilerSession.Stop()skipped its own disposals on the happy path._processingwas built withTaskContinuationOptions.OnlyOnFaulted; wheneventSource.Process()returns normally the continuation criteria aren't met, so the continuation transitions toCanceledand_processing.Wait()throwsAggregateException(TaskCanceledException).EventPipeSession.Dispose()andTraceLogEventSource.Dispose()sat after that call and were never reached — thecatchloggedError during sampler profiler session shutdown.and swallowed it. The continuation is now unconditional, and the disposals moved into afinallywith a bounded drain wait so shutdown can't hang.Nothing ever called
Dispose()on the factory.ProfilingIntegrationwas notIDisposable, soHubnever added it to_integrationsToCleanup, and the factory's only other reference isoptions.TransactionProfilerFactory, which nothing disposes. The integration is nowIDisposableand disposes the factory — but only one it created itself, sinceTransactionProfilerFactorymay have been supplied elsewhere.The reported bug.
Dispose()now waits (bounded) for an in-flight startup and then disposes the session. It also cancels theWaitForFirstEventAsyncwait, which could otherwise block indefinitely, and observes the startup task's exception if it failed.Impact
Low, and narrower than the issue suggests. The session is created once and lives for the SDK's lifetime —
SamplingTransactionProfiler.Stop()only clears_inProgress, it never touches the session — so the only disposal point is SDK shutdown, which is normally process shutdown, where the OS reclaims the handle and theTraceLogregardless. Nothing accumulates while an application runs.Where it does bite is when the process outlives the SDK: repeated
Init/Closecycles strand oneEventPipeSessionplus itsTraceLogper cycle, and hosts that close Sentry but keep running never release the pipe.The issue links this to the Windows Service memory growth in #3375. That link doesn't hold up — a service that initialises Sentry once would never reach any of these paths.
Notes
Error during sampler profiler session shutdown.on every clean shutdown with profiling enabled. Worth asking anyone reporting profiler-related growth whether they see it.Hub.Dispose()now does real work for profiling users where it previously did none. Both new waits are bounded at 2s so a wedged EventPipe session can't hang shutdown.Sentry.Profilingtests are largelySkip.If(TestEnvironment.IsGitHubActions), so the twoProfilingIntegrationtests are plain[Fact]s that do run in CI; only the session-level test follows the existing skip pattern.Fixes #5418