private async Task SendMessageAsync()

in src/ServiceProfiler.EventPipe.Otel/Microsoft.ApplicationInsights.Profiler.Shared/Services/IPC/DuplexNamedPipeService.cs [194:218]


    private async Task SendMessageAsync(string message, TimeSpan timeout, CancellationToken cancellationToken)
    {
        VerifyModeIsSpecified();
        VerifyMessageIsTransmitable(message);

        timeout = ConfigureReadWriteTimeout(timeout);
        using CancellationTokenSource timeoutSource = new CancellationTokenSource(timeout);
        using CancellationTokenSource linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(timeoutSource.Token, cancellationToken);
        cancellationToken = linkedCancellationTokenSource.Token;

        Task timeoutTask = Task.Delay(timeout, cancellationToken);
        Task writelineTask = Task.Run(async () =>
        {
            using (StreamWriter writer = new StreamWriter(_pipeStream, encoding: Encoding.UTF8, bufferSize: -1, leaveOpen: true))
            {
                await writer.WriteLineAsync(message).ConfigureAwait(false);
            }
        }, cancellationToken);
        await Task.WhenAny(timeoutTask, writelineTask).ConfigureAwait(false);

        if (!writelineTask.IsCompleted)
        {
            throw new TimeoutException($"Can't finish writing message within given timeout: {timeout.TotalMilliseconds}ms");
        }
    }