Skip to content

Commit

Permalink
Fix connect
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Dec 13, 2024
1 parent 5fcf716 commit 55ff8d2
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/Ultra.Core/DiagnosticPortSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Tracing;
using System.Net.Sockets;
using Microsoft.Diagnostics.NETCore.Client;
using Microsoft.Diagnostics.Tracing.Parsers;
using Ultra.Sampler;
Expand Down Expand Up @@ -38,7 +37,7 @@ public DiagnosticPortSession(int pid, bool sampler, string baseName, Cancellatio
_baseName = baseName;
_cancelConnectSource = new CancellationTokenSource();
_semaphoreSlim = new SemaphoreSlim(0);
_connectTask = ConnectAndStartProfilingImpl(pid, sampler, baseName, token);
_connectTask = ConnectAndStartProfilingImpl(token);
}

public bool TryGetNettraceFilePathIfExists([NotNullWhen(true)] out string? nettraceFilePath)
Expand All @@ -53,20 +52,20 @@ public bool TryGetNettraceFilePathIfExists([NotNullWhen(true)] out string? nettr
return nettraceFilePath is not null;
}

private async Task ConnectAndStartProfilingImpl(int pid, bool sampler, string baseName, CancellationToken token)
private async Task ConnectAndStartProfilingImpl(CancellationToken token)
{
CancellationTokenSource linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token, _cancelConnectSource.Token);
try
{

var connectCancellationToken = linkedCancellationTokenSource.Token;

if (sampler)
if (_sampler)
{
_cancelConnectSource.CancelAfter(500);
}

var connectionAddress = await TryFindConnectionAddress(pid, sampler, connectCancellationToken).ConfigureAwait(false);
var connectionAddress = await TryFindConnectionAddress(_pid, _sampler, connectCancellationToken).ConfigureAwait(false);
if (connectionAddress is null) return;

_diagnosticsClient = (await DiagnosticsClientConnector.FromDiagnosticPort(connectionAddress, connectCancellationToken).ConfigureAwait(false))?.Instance;
Expand All @@ -76,9 +75,9 @@ private async Task ConnectAndStartProfilingImpl(int pid, bool sampler, string ba
}
catch (OperationCanceledException ex)
{
if (sampler && _cancelConnectSource is not null && _cancelConnectSource.IsCancellationRequested)
if (_sampler && _cancelConnectSource is not null && _cancelConnectSource.IsCancellationRequested)
{
throw new InvalidOperationException($"Cannot connect to the diagnostic port socket for pid {pid}", ex);
throw new InvalidOperationException($"Cannot connect to the diagnostic port socket for pid {_pid}", ex);
}
return;
}
Expand All @@ -102,9 +101,7 @@ public async Task StartProfiling(CancellationToken token)

_profilingTask = _connectTask.ContinueWith(async task =>
{


_nettraceFilePath = Path.Combine(Environment.CurrentDirectory, $"{_baseName}_{(_sampler ? "sampler" : "clr")}_{_pid}.nettrace");
_nettraceFilePath = Path.Combine(Environment.CurrentDirectory, $"{_baseName}_{_pid}_{(_sampler ? "sampler" : "clr")}.nettrace");
_nettraceFileStream = new FileStream(_nettraceFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, 65536, FileOptions.Asynchronous);

long keywords = -1;
Expand Down Expand Up @@ -186,7 +183,7 @@ public async Task WaitForConnectAndStartSession()

// Let's increase the delay after each check to lower the overhead
waitForNextCheckDelayInMs += 10;
waitForNextCheckDelayInMs = Math.Max(waitForNextCheckDelayInMs, 100);
waitForNextCheckDelayInMs = Math.Min(waitForNextCheckDelayInMs, 100);
}

return diagnosticPortSocket;
Expand Down

0 comments on commit 55ff8d2

Please sign in to comment.