diff --git a/src/Sentry.Profiling/SampleProfilerSession.cs b/src/Sentry.Profiling/SampleProfilerSession.cs index 3bb606a960..e54cf62020 100644 --- a/src/Sentry.Profiling/SampleProfilerSession.cs +++ b/src/Sentry.Profiling/SampleProfilerSession.cs @@ -17,14 +17,16 @@ internal class SampleProfilerSession : IDisposable private readonly IDiagnosticLogger? _logger; private readonly SentryStopwatch _stopwatch; private bool _stopped = false; + private Task _processing; - private SampleProfilerSession(SentryStopwatch stopwatch, EventPipeSession session, TraceLogEventSource eventSource, IDiagnosticLogger? logger) + private SampleProfilerSession(SentryStopwatch stopwatch, EventPipeSession session, TraceLogEventSource eventSource, Task processing, IDiagnosticLogger? logger) { _session = session; _logger = logger; _eventSource = eventSource; _sampleEventParser = new SampleProfilerTraceEventParser(_eventSource); _stopwatch = stopwatch; + _processing = processing; } // Exposed only for benchmarks. @@ -86,7 +88,7 @@ public static SampleProfilerSession StartNew(IDiagnosticLogger? logger = null) var eventSource = TraceLog.CreateFromEventPipeSession(session, TraceLog.EventPipeRundownConfiguration.Enable(client)); // Process() blocks until the session is stopped so we need to run it on a separate thread. - Task.Factory.StartNew(eventSource.Process, TaskCreationOptions.LongRunning) + var processing = Task.Factory.StartNew(eventSource.Process, TaskCreationOptions.LongRunning) .ContinueWith(_ => { if (_.Exception?.InnerException is { } e) @@ -95,7 +97,7 @@ public static SampleProfilerSession StartNew(IDiagnosticLogger? logger = null) } }, TaskContinuationOptions.OnlyOnFaulted); - return new SampleProfilerSession(stopWatch, session, eventSource, logger); + return new SampleProfilerSession(stopWatch, session, eventSource, processing, logger); } catch (Exception ex) { @@ -128,6 +130,7 @@ public void Stop() { _stopped = true; _session.Stop(); + _processing.Wait(); _session.Dispose(); _eventSource.Dispose(); }