Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: dispose before processing is done
  • Loading branch information
vaind committed Feb 6, 2025
commit 4efb3840deefd76d51c30d2aa14f09d62a7eddf7
11 changes: 7 additions & 4 deletions src/Sentry.Profiling/SampleProfilerSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,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.
Expand Down Expand Up @@ -88,7 +90,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)
Expand All @@ -97,7 +99,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)
{
Expand Down Expand Up @@ -130,8 +132,9 @@ public void Stop()
{
_stopped = true;
_session.Stop();
EventSource.Dispose();
_processing.Wait();
_session.Dispose();
EventSource.Dispose();
}
catch (Exception ex)
{
Expand Down
Loading