From 13a4fb3976105a816703086190f7d0bbd5711863 Mon Sep 17 00:00:00 2001 From: Gabriel Scatolin <17441745+CypherPotato@users.noreply.github.com> Date: Thu, 28 May 2026 21:06:19 -0300 Subject: [PATCH] Fix LogStream sync writes to honor bounded channel backpressure --- src/Http/LogStream.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Http/LogStream.cs b/src/Http/LogStream.cs index 1265f8b..1a8d07e 100644 --- a/src/Http/LogStream.cs +++ b/src/Http/LogStream.cs @@ -560,8 +560,18 @@ protected virtual void WriteLineInternal ( string line ) { string lineText = NormalizeEntries ? line.Normalize ().Trim ().ReplaceLineEndings () : line; - if (!_channel.Writer.TryWrite ( lineText )) { - throw new InvalidOperationException ( SR.LogStream_FailedWrite ); + try { + _channel.Writer.WriteAsync ( lineText, _cancellationTokenSource.Token ) + .AsTask () + .ConfigureAwait ( false ) + .GetAwaiter () + .GetResult (); + } + catch (ChannelClosedException) { + // Channel was closed, which is expected during shutdown. Ignore. + } + catch (OperationCanceledException) when (_cancellationTokenSource.IsCancellationRequested) { + // Cancellation requested during shutdown. Ignore. } } @@ -738,4 +748,4 @@ private sealed class FlushSignal { public TaskCompletionSource Completion { get; } = new TaskCompletionSource ( TaskCreationOptions.RunContinuationsAsynchronously ); } } -} \ No newline at end of file +}