Skip to content

Commit

Permalink
fix dispose deadlock #10
Browse files Browse the repository at this point in the history
  • Loading branch information
CypherPotato committed Aug 22, 2024
1 parent fccb140 commit 5d9a0dc
Showing 1 changed file with 48 additions and 41 deletions.
89 changes: 48 additions & 41 deletions src/Http/LogStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -365,8 +372,8 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Flush();
channel.Writer.Complete();
Flush();
TextWriter?.Dispose();
rotatingLogPolicy?.Dispose();
consumerThread.Join();
Expand Down

0 comments on commit 5d9a0dc

Please sign in to comment.