Skip to content

Logging: fix race in disposal of a passed-in TextWriter #2581

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

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/StackExchange.Redis/ConnectionMultiplexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ private static async Task<ConnectionMultiplexer> ConnectImplAsync(ConfigurationO
{
if (connectHandler != null && muxer != null) muxer.ConnectionFailed -= connectHandler;
if (killMe != null) try { killMe.Dispose(); } catch { }
if (log is TextWriterLogger twLogger) twLogger.Dispose();
if (log is TextWriterLogger twLogger) twLogger.Release();
}
}

Expand Down Expand Up @@ -740,7 +740,7 @@ private static ConnectionMultiplexer ConnectImpl(ConfigurationOptions configurat
{
if (connectHandler != null && muxer != null) muxer.ConnectionFailed -= connectHandler;
if (killMe != null) try { killMe.Dispose(); } catch { }
if (log is TextWriterLogger twLogger) twLogger.Dispose();
if (log is TextWriterLogger twLogger) twLogger.Release();
}
}

Expand Down
25 changes: 18 additions & 7 deletions src/StackExchange.Redis/TextWriterLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace StackExchange.Redis;

internal sealed class TextWriterLogger : ILogger, IDisposable
internal sealed class TextWriterLogger : ILogger
{
private TextWriter? _writer;
private ILogger? _wrapped;
private readonly ILogger? _wrapped;

internal static Action<string> NullWriter = _ => { };

Expand All @@ -26,16 +26,27 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
{
lock (writer)
{
writer.Write($"{DateTime.UtcNow:HH:mm:ss.ffff}: ");
writer.WriteLine(formatter(state, exception));
// We check here again because it's possible we've released below, and never want to write past releasing.
if (_writer is TextWriter innerWriter)
{
innerWriter.Write($"{DateTime.UtcNow:HH:mm:ss.ffff}: ");
innerWriter.WriteLine(formatter(state, exception));
}
}
}
}

public void Dispose()
public void Release()
{
_writer = null;
_wrapped = null;
// We lock here because we may have piled up on a lock above and still be writing.
// We never want a write to go past the Release(), as many TextWriter implementations are not thread safe.
if (_writer is TextWriter writer)
{
lock (writer)
{
_writer = null;
}
}
}
}

Expand Down