Skip to content

Commit e1909b9

Browse files
committed
Cleanup some code
1 parent 7d49266 commit e1909b9

File tree

2 files changed

+20
-38
lines changed

2 files changed

+20
-38
lines changed

src/Ultra.Core/DiagnosticPortSession.cs

+9-36
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private async Task ConnectAndStartProfilingImpl(int pid, bool sampler, string ba
7777

7878
public async Task StartProfiling(CancellationToken token)
7979
{
80-
// We want to make sure that we are not disposing while we are connecting
80+
// We want to make sure that we are not disposing while we are starting a session
8181
await _semaphoreSlim.WaitAsync(token);
8282

8383
try
@@ -143,6 +143,9 @@ public async Task WaitForConnectAndStartSession()
143143
var pattern = $"dotnet-diagnostic-{pid}-*-socket";
144144
string? diagnosticPortSocket = null;
145145

146+
int waitForNextCheckDelayInMs = 10;
147+
148+
// This loop is blocking until a file is available or the token is cancelled
146149
while (true)
147150
{
148151
if (Directory.Exists(tempFolder))
@@ -166,7 +169,11 @@ public async Task WaitForConnectAndStartSession()
166169
}
167170
}
168171

169-
await Task.Delay(10, token).ConfigureAwait(false);
172+
await Task.Delay(waitForNextCheckDelayInMs, token).ConfigureAwait(false);
173+
174+
// Let's increase the delay after each check to lower the overhead
175+
waitForNextCheckDelayInMs += 10;
176+
waitForNextCheckDelayInMs = Math.Max(waitForNextCheckDelayInMs, 100);
170177
}
171178

172179
return diagnosticPortSocket;
@@ -246,38 +253,4 @@ public async ValueTask StopAndDisposeAsync()
246253
_cancelConnectSource.Dispose();
247254
}
248255
}
249-
250-
private async Task StopAsync(CancellationToken token)
251-
{
252-
if (_eventPipeSession is null) return;
253-
254-
try
255-
{
256-
await _eventPipeSession.StopAsync(token).ConfigureAwait(false);
257-
}
258-
catch (EndOfStreamException)
259-
{
260-
261-
}
262-
catch (TimeoutException)
263-
{
264-
265-
}
266-
catch (OperationCanceledException)
267-
{
268-
269-
}
270-
catch (PlatformNotSupportedException)
271-
{
272-
273-
}
274-
catch (ServerNotAvailableException)
275-
{
276-
277-
}
278-
catch (SocketException)
279-
{
280-
281-
}
282-
}
283256
}

src/Ultra.Core/UltraProfiler.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.IO.Compression;
77
using System.Runtime.InteropServices;
88
using System.Text.Json;
9+
using System.Threading;
910
using ByteSizeLib;
1011

1112
namespace Ultra.Core;
@@ -21,17 +22,18 @@ public abstract class UltraProfiler : IDisposable
2122
private protected readonly Stopwatch ProfilerClock;
2223
private protected TimeSpan LastTimeProgress;
2324
private readonly CancellationTokenSource _cancellationTokenSource;
25+
private bool _disposed;
2426

2527
/// <summary>
2628
/// Initializes a new instance of the <see cref="UltraProfiler"/> class.
2729
/// </summary>
28-
protected UltraProfiler()
30+
private protected UltraProfiler()
2931
{
3032
ProfilerClock = new Stopwatch();
3133
_cancellationTokenSource = new CancellationTokenSource();
3234
}
3335

34-
protected CancellationToken CancellationToken => _cancellationTokenSource.Token;
36+
private protected CancellationToken CancellationToken => _cancellationTokenSource.Token;
3537

3638
/// <summary>
3739
/// Creates a new instance of the <see cref="UltraProfiler"/> class.
@@ -69,6 +71,9 @@ public bool Cancel()
6971
{
7072
StopRequested = true;
7173

74+
// Make sure that we cancel any pending operations
75+
_cancellationTokenSource.Cancel();
76+
7277
// Before really canceling, wait for the clean cancel to be done
7378
WaitForCleanCancel();
7479
return true;
@@ -80,7 +85,11 @@ public bool Cancel()
8085
/// </summary>
8186
public void Dispose()
8287
{
88+
if (_disposed) return;
89+
_disposed = true;
90+
8391
DisposeImpl();
92+
_cancellationTokenSource.Dispose();
8493
CleanCancel?.Dispose();
8594
CleanCancel = null;
8695
}

0 commit comments

Comments
 (0)