private async Task ReadMessageAsync()

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


    private async Task<string> ReadMessageAsync(TimeSpan timeout, CancellationToken cancellationToken)
    {
        VerifyModeIsSpecified();
        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<string> readlineTask = Task.Run(async () =>
        {
            using (StreamReader reader = new StreamReader(_pipeStream, Encoding.UTF8, detectEncodingFromByteOrderMarks: false, bufferSize: -1, leaveOpen: true))
            {
                return await reader.ReadLineAsync().ConfigureAwait(false);
            }
        });

        await Task.WhenAny(timeoutTask, readlineTask).ConfigureAwait(false);

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

        return readlineTask.Result;
    }