Skip to content

Commit

Permalink
Avoid ODE in tests with Server logging (#2742) (#2907)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanConroy authored Sep 4, 2018
1 parent 04c606d commit 753c3da
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions test/Microsoft.AspNetCore.SignalR.Tests.Utils/ServerLogScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class ServerLogScope : IDisposable
private readonly ConcurrentDictionary<string, ILogger> _serverLoggers;
private readonly ILogger _scopeLogger;
private readonly object _lock;
private bool _disposed;

public ServerLogScope(ServerFixture serverFixture, ILoggerFactory loggerFactory, IDisposable wrappedDisposable)
{
Expand Down Expand Up @@ -45,11 +46,15 @@ private void ServerFixtureOnServerLogged(LogRecord logRecord)

ILogger logger;

// There maybe thready safety issues in logging when creating multiple loggers at the same time
// https://github.com/aspnet/Logging/issues/810
lock (_lock)
{
if (_disposed)
{
return;
}

// Create (or get) a logger with the same name as the server logger
// Call in the lock to avoid ODE where LoggerFactory could be disposed by the wrapped disposable
logger = _serverLoggers.GetOrAdd(write.LoggerName, loggerName => _loggerFactory.CreateLogger(loggerName));
}

Expand All @@ -62,7 +67,11 @@ public void Dispose()

_scopeLogger.LogInformation("Server log scope stopped.");

_wrappedDisposable?.Dispose();
lock (_lock)
{
_wrappedDisposable?.Dispose();
_disposed = true;
}
}
}
}

0 comments on commit 753c3da

Please sign in to comment.