Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Short circuit WritingTask and await its completion in dispose(async) #44

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions src/Elastic.OpenTelemetry/Diagnostics/LogFileWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ internal sealed class LogFileWriter : IDisposable, IAsyncDisposable
{
public static readonly bool FileLoggingEnabled = IsFileLoggingEnabled();

private bool _disposing;
private readonly ManualResetEventSlim _syncDisposeWaitHandle = new(false);
private readonly StreamWriter _streamWriter;
private readonly Channel<string> _channel = Channel.CreateBounded<string>(new BoundedChannelOptions(1024)
{
Expand Down Expand Up @@ -46,13 +48,11 @@ private LogFileWriter()

WritingTask = Task.Run(async () =>
{
while (await _channel.Reader.WaitToReadAsync().ConfigureAwait(false))
{
while (_channel.Reader.TryRead(out var logLine))
{
await _streamWriter.WriteLineAsync(logLine).ConfigureAwait(false);
}
}
while (await _channel.Reader.WaitToReadAsync().ConfigureAwait(false) && !_disposing)
while (_channel.Reader.TryRead(out var logLine) && !_disposing)
await _streamWriter.WriteLineAsync(logLine).ConfigureAwait(false);

_syncDisposeWaitHandle.Set();
});

var builder = StringBuilderCache.Acquire();
Expand Down Expand Up @@ -258,9 +258,10 @@ public void WriteLogLine(Activity? activity, int managedThreadId, DateTime dateT
}

var spin = new SpinWait();
while (true)
var message = StringBuilderCache.GetStringAndRelease(builder);
while (!_disposing)
{
if (_channel.Writer.TryWrite(StringBuilderCache.GetStringAndRelease(builder)))
if (_channel.Writer.TryWrite(message))
break;
spin.SpinOnce();
}
Expand Down Expand Up @@ -288,23 +289,25 @@ private static bool IsFileLoggingEnabled()

public void Dispose()
{
// We don't wait for the channel to be drained which is probably the correct choice.
// Dispose should be a quick operation with no chance of exceptions.
// We should document methods to wait for the WritingTask, before disposal, if that matters to the
// consumer.

//tag that we are running a dispose this allows running tasks and spin waits to short circuit
_disposing = true;
_channel.Writer.TryComplete();

_syncDisposeWaitHandle.Wait(TimeSpan.FromSeconds(1));

_streamWriter.Dispose();
}

public async ValueTask DisposeAsync()
{
// We don't wait for the channel to be drained which is probably the correct choice.
// Dispose should be a quick operation with no chance of exceptions.
// We should document methods to await the WritingTask, before disposal, if that matters to the
// consumer.
//tag that we are running a dispose this allows running tasks and spin waits to short circuit
_disposing = true;

_channel.Writer.TryComplete();

//Writing task will short circuit once _disposing is flipped to true
await WritingTask.ConfigureAwait(false);

await _streamWriter.DisposeAsync().ConfigureAwait(false);
}
}