Skip to content

Commit 4efb384

Browse files
committed
fix: dispose before processing is done
1 parent d865e36 commit 4efb384

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/Sentry.Profiling/SampleProfilerSession.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ internal class SampleProfilerSession : IDisposable
1616
private readonly IDiagnosticLogger? _logger;
1717
private readonly SentryStopwatch _stopwatch;
1818
private bool _stopped = false;
19+
private Task _processing;
1920

20-
private SampleProfilerSession(SentryStopwatch stopwatch, EventPipeSession session, TraceLogEventSource eventSource, IDiagnosticLogger? logger)
21+
private SampleProfilerSession(SentryStopwatch stopwatch, EventPipeSession session, TraceLogEventSource eventSource, Task processing, IDiagnosticLogger? logger)
2122
{
2223
_session = session;
2324
_logger = logger;
2425
EventSource = eventSource;
2526
_sampleEventParser = new SampleProfilerTraceEventParser(EventSource);
2627
_stopwatch = stopwatch;
28+
_processing = processing;
2729
}
2830

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

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

100-
return new SampleProfilerSession(stopWatch, session, eventSource, logger);
102+
return new SampleProfilerSession(stopWatch, session, eventSource, processing, logger);
101103
}
102104
catch (Exception ex)
103105
{
@@ -130,8 +132,9 @@ public void Stop()
130132
{
131133
_stopped = true;
132134
_session.Stop();
133-
EventSource.Dispose();
135+
_processing.Wait();
134136
_session.Dispose();
137+
EventSource.Dispose();
135138
}
136139
catch (Exception ex)
137140
{

0 commit comments

Comments
 (0)