public async ValueTask DisposeAsync()
        {
            if (!_disposed)
            {
                _cancellation.Cancel();

                if (null != _listenTask)
                {
                    try
                    {
                        await _listenTask.ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        Debug.Fail(ex.Message);
                    }
                }

                if (null != _server)
                {
                    await _server.DisposeAsync().ConfigureAwait(false);
                }

                _endpointInfosSemaphore.Dispose();

                _cancellation.Dispose();

                _disposed = true;
            }
        }
 public async void Dispose()
 {
     ProcessLauncher.Launcher.Cleanup();
     if (_server != null)
     {
         await _server.DisposeAsync();
     }
 }
 public async void Dispose()
 {
     if (!string.IsNullOrEmpty(_port) && File.Exists(_port))
     {
         File.Delete(_port);
     }
     ProcessLauncher.Launcher.Cleanup();
     if (_server != null)
     {
         await _server.DisposeAsync();
     }
 }
        public async Task <DiagnosticsClientHolder> Build(CancellationToken ct, int processId, string diagnosticPort, bool showChildIO, bool printLaunchCommand)
        {
            IpcEndpointConfig portConfig = null;

            if (!string.IsNullOrEmpty(diagnosticPort))
            {
                portConfig = IpcEndpointConfig.Parse(diagnosticPort);
            }

            if (ProcessLauncher.Launcher.HasChildProc)
            {
                // Create and start the reversed server
                string diagnosticTransportName   = GetTransportName(_toolName);
                ReversedDiagnosticsServer server = new ReversedDiagnosticsServer(diagnosticTransportName);
                server.Start();

                // Start the child proc
                if (!ProcessLauncher.Launcher.Start(diagnosticTransportName, ct, showChildIO, printLaunchCommand))
                {
                    throw new InvalidOperationException($"Failed to start '{ProcessLauncher.Launcher.ChildProc.StartInfo.FileName} {ProcessLauncher.Launcher.ChildProc.StartInfo.Arguments}'.");
                }
                IpcEndpointInfo endpointInfo;
                try
                {
                    // Wait for attach
                    endpointInfo = server.Accept(TimeSpan.FromSeconds(_timeoutInSec));

                    // If for some reason a different process attached to us, wait until the expected process attaches.
                    while (endpointInfo.ProcessId != ProcessLauncher.Launcher.ChildProc.Id)
                    {
                        endpointInfo = server.Accept(TimeSpan.FromSeconds(_timeoutInSec));
                    }
                }
                catch (TimeoutException)
                {
                    Console.Error.WriteLine("Unable to start tracing session - the target app failed to connect to the diagnostics port. This may happen if the target application is running .NET Core 3.1 or older versions. Attaching at startup is only available from .NET 5.0 or later.");
                    throw;
                }
                return(new DiagnosticsClientHolder(new DiagnosticsClient(endpointInfo.Endpoint), endpointInfo, server));
            }
            else if (portConfig != null && portConfig.IsListenConfig)
            {
                string fullPort = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? portConfig.Address : Path.GetFullPath(portConfig.Address);
                ReversedDiagnosticsServer server = new ReversedDiagnosticsServer(fullPort);
                server.Start();
                Console.WriteLine($"Waiting for connection on {fullPort}");
                Console.WriteLine($"Start an application with the following environment variable: DOTNET_DiagnosticPorts={fullPort}");

                try
                {
                    IpcEndpointInfo endpointInfo = await server.AcceptAsync(ct);

                    return(new DiagnosticsClientHolder(new DiagnosticsClient(endpointInfo.Endpoint), endpointInfo, fullPort, server));
                }
                catch (TaskCanceledException)
                {
                    //clean up the server
                    await server.DisposeAsync();

                    if (!ct.IsCancellationRequested)
                    {
                        throw;
                    }
                    return(null);
                }
            }
            else if (portConfig != null && portConfig.IsConnectConfig)
            {
                return(new DiagnosticsClientHolder(new DiagnosticsClient(portConfig)));
            }
            else
            {
                return(new DiagnosticsClientHolder(new DiagnosticsClient(processId)));
            }
        }