@@ -17,14 +17,16 @@ internal class SampleProfilerSession : IDisposable
17
17
private readonly IDiagnosticLogger ? _logger ;
18
18
private readonly SentryStopwatch _stopwatch ;
19
19
private bool _stopped = false ;
20
+ private Task _processing ;
20
21
21
- private SampleProfilerSession ( SentryStopwatch stopwatch , EventPipeSession session , TraceLogEventSource eventSource , IDiagnosticLogger ? logger )
22
+ private SampleProfilerSession ( SentryStopwatch stopwatch , EventPipeSession session , TraceLogEventSource eventSource , Task processing , IDiagnosticLogger ? logger )
22
23
{
23
24
_session = session ;
24
25
_logger = logger ;
25
26
_eventSource = eventSource ;
26
27
_sampleEventParser = new SampleProfilerTraceEventParser ( _eventSource ) ;
27
28
_stopwatch = stopwatch ;
29
+ _processing = processing ;
28
30
}
29
31
30
32
// Exposed only for benchmarks.
@@ -86,7 +88,7 @@ public static SampleProfilerSession StartNew(IDiagnosticLogger? logger = null)
86
88
var eventSource = TraceLog . CreateFromEventPipeSession ( session , TraceLog . EventPipeRundownConfiguration . Enable ( client ) ) ;
87
89
88
90
// 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 )
90
92
. ContinueWith ( _ =>
91
93
{
92
94
if ( _ . Exception ? . InnerException is { } e )
@@ -95,7 +97,7 @@ public static SampleProfilerSession StartNew(IDiagnosticLogger? logger = null)
95
97
}
96
98
} , TaskContinuationOptions . OnlyOnFaulted ) ;
97
99
98
- return new SampleProfilerSession ( stopWatch , session , eventSource , logger ) ;
100
+ return new SampleProfilerSession ( stopWatch , session , eventSource , processing , logger ) ;
99
101
}
100
102
catch ( Exception ex )
101
103
{
@@ -128,6 +130,7 @@ public void Stop()
128
130
{
129
131
_stopped = true ;
130
132
_session . Stop ( ) ;
133
+ _processing . Wait ( ) ;
131
134
_session . Dispose ( ) ;
132
135
_eventSource . Dispose ( ) ;
133
136
}
0 commit comments