From 5d9a0dc41012a757154ed935bbb46a4a5f953baf Mon Sep 17 00:00:00 2001 From: cypherpotato Date: Wed, 21 Aug 2024 21:19:03 -0300 Subject: [PATCH] fix dispose deadlock #10 --- src/Http/LogStream.cs | 89 +++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 41 deletions(-) diff --git a/src/Http/LogStream.cs b/src/Http/LogStream.cs index 4b4de66..1732718 100644 --- a/src/Http/LogStream.cs +++ b/src/Http/LogStream.cs @@ -188,61 +188,68 @@ public void StopBuffering() private async void ProcessQueue() { var reader = channel.Reader; - while (!isDisposed && await reader.WaitToReadAsync()) + try { - writeEvent.Reset(); - - while (reader.TryRead(out var item)) + while (!isDisposed && await reader.WaitToReadAsync()) { - rotatingPolicyLocker.WaitOne(); + writeEvent.Reset(); - bool gotAnyError = false; - string? dataStr = item?.ToString(); + while (reader.TryRead(out var item)) + { + rotatingPolicyLocker.WaitOne(); - if (dataStr is null) - continue; + bool gotAnyError = false; + string? dataStr = item?.ToString(); - try - { - TextWriter?.WriteLine(dataStr); - } - catch - { - if (!gotAnyError) + if (dataStr is null) + continue; + + try { - await channel.Writer.WriteAsync(item); - gotAnyError = true; + TextWriter?.WriteLine(dataStr); + } + catch + { + if (!gotAnyError) + { + await channel.Writer.WriteAsync(item); + gotAnyError = true; + } } - } - try - { - if (filePath is not null) - File.AppendAllText(filePath, dataStr + Environment.NewLine, Encoding); - } - catch - { - if (!gotAnyError) + try { - await channel.Writer.WriteAsync(item); - gotAnyError = true; + if (filePath is not null) + File.AppendAllText(filePath, dataStr + Environment.NewLine, Encoding); + } + catch + { + if (!gotAnyError) + { + await channel.Writer.WriteAsync(item); + gotAnyError = true; + } } - } - try - { - _bufferingContent?.Add(dataStr); - } - catch - { - if (!gotAnyError) + try + { + _bufferingContent?.Add(dataStr); + } + catch { - await channel.Writer.WriteAsync(item); - gotAnyError = true; + if (!gotAnyError) + { + await channel.Writer.WriteAsync(item); + gotAnyError = true; + } } } - } + writeEvent.Set(); + } + } + finally + { writeEvent.Set(); } } @@ -365,8 +372,8 @@ protected virtual void Dispose(bool disposing) { if (disposing) { - Flush(); channel.Writer.Complete(); + Flush(); TextWriter?.Dispose(); rotatingLogPolicy?.Dispose(); consumerThread.Join();