Skip to content

Commit c6a5d4a

Browse files
authored
fix: wait for profiling session shutdown (#3966)
1 parent 3974e41 commit c6a5d4a

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/Sentry.Profiling/SampleProfilerSession.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ internal class SampleProfilerSession : IDisposable
1717
private readonly IDiagnosticLogger? _logger;
1818
private readonly SentryStopwatch _stopwatch;
1919
private bool _stopped = false;
20+
private Task _processing;
2021

21-
private SampleProfilerSession(SentryStopwatch stopwatch, EventPipeSession session, TraceLogEventSource eventSource, IDiagnosticLogger? logger)
22+
private SampleProfilerSession(SentryStopwatch stopwatch, EventPipeSession session, TraceLogEventSource eventSource, Task processing, IDiagnosticLogger? logger)
2223
{
2324
_session = session;
2425
_logger = logger;
2526
_eventSource = eventSource;
2627
_sampleEventParser = new SampleProfilerTraceEventParser(_eventSource);
2728
_stopwatch = stopwatch;
29+
_processing = processing;
2830
}
2931

3032
// Exposed only for benchmarks.
@@ -86,7 +88,7 @@ public static SampleProfilerSession StartNew(IDiagnosticLogger? logger = null)
8688
var eventSource = TraceLog.CreateFromEventPipeSession(session, TraceLog.EventPipeRundownConfiguration.Enable(client));
8789

8890
// Process() blocks until the session is stopped so we need to run it on a separate thread.
89-
Task.Factory.StartNew(eventSource.Process, TaskCreationOptions.LongRunning)
91+
var processing = Task.Factory.StartNew(eventSource.Process, TaskCreationOptions.LongRunning)
9092
.ContinueWith(_ =>
9193
{
9294
if (_.Exception?.InnerException is { } e)
@@ -95,7 +97,7 @@ public static SampleProfilerSession StartNew(IDiagnosticLogger? logger = null)
9597
}
9698
}, TaskContinuationOptions.OnlyOnFaulted);
9799

98-
return new SampleProfilerSession(stopWatch, session, eventSource, logger);
100+
return new SampleProfilerSession(stopWatch, session, eventSource, processing, logger);
99101
}
100102
catch (Exception ex)
101103
{
@@ -128,6 +130,7 @@ public void Stop()
128130
{
129131
_stopped = true;
130132
_session.Stop();
133+
_processing.Wait();
131134
_session.Dispose();
132135
_eventSource.Dispose();
133136
}

0 commit comments

Comments
 (0)