Skip to content

Commit

Permalink
HttpClientFactory Prevent throwing if ILogger couldn't be created (#9…
Browse files Browse the repository at this point in the history
…0503)

* HttpClientFactory stop cleanup timer on dispose

* Remove finalizer

* Tweak ThrowIfDisposed

* Revert some changes

* Fix
  • Loading branch information
CarnaViire authored Aug 14, 2023
1 parent 0c48209 commit 2470610
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net.Http;
using System.Threading;
Expand Down Expand Up @@ -208,7 +209,7 @@ internal void ExpiryTimer_Tick(object? state)
var expired = new ExpiredHandlerTrackingEntry(active);
_expiredHandlers.Enqueue(expired);

Log.HandlerExpired(_logger.Value, active.Name, active.Lifetime);
Log.HandlerExpired(_logger, active.Name, active.Lifetime);

StartCleanupTimer();
}
Expand Down Expand Up @@ -266,7 +267,7 @@ internal void CleanupTimer_Tick()
try
{
int initialCount = _expiredHandlers.Count;
Log.CleanupCycleStart(_logger.Value, initialCount);
Log.CleanupCycleStart(_logger, initialCount);

var stopwatch = ValueStopwatch.StartNew();

Expand All @@ -287,7 +288,7 @@ internal void CleanupTimer_Tick()
}
catch (Exception ex)
{
Log.CleanupItemFailed(_logger.Value, entry.Name, ex);
Log.CleanupItemFailed(_logger, entry.Name, ex);
}
}
else
Expand All @@ -298,7 +299,7 @@ internal void CleanupTimer_Tick()
}
}

Log.CleanupCycleEnd(_logger.Value, stopwatch.GetElapsedTime(), disposedCount, _expiredHandlers.Count);
Log.CleanupCycleEnd(_logger, stopwatch.GetElapsedTime(), disposedCount, _expiredHandlers.Count);
}
finally
{
Expand Down Expand Up @@ -343,24 +344,48 @@ public static class EventIds
"HttpMessageHandler expired after {HandlerLifetime}ms for client '{ClientName}'");


public static void CleanupCycleStart(ILogger logger, int initialCount)
public static void CleanupCycleStart(Lazy<ILogger> loggerLazy, int initialCount)
{
_cleanupCycleStart(logger, initialCount, null);
if (TryGetLogger(loggerLazy, out ILogger? logger))
{
_cleanupCycleStart(logger, initialCount, null);
}
}

public static void CleanupCycleEnd(Lazy<ILogger> loggerLazy, TimeSpan duration, int disposedCount, int finalCount)
{
if (TryGetLogger(loggerLazy, out ILogger? logger))
{
_cleanupCycleEnd(logger, duration.TotalMilliseconds, disposedCount, finalCount, null);
}
}

public static void CleanupCycleEnd(ILogger logger, TimeSpan duration, int disposedCount, int finalCount)
public static void CleanupItemFailed(Lazy<ILogger> loggerLazy, string clientName, Exception exception)
{
_cleanupCycleEnd(logger, duration.TotalMilliseconds, disposedCount, finalCount, null);
if (TryGetLogger(loggerLazy, out ILogger? logger))
{
_cleanupItemFailed(logger, clientName, exception);
}
}

public static void CleanupItemFailed(ILogger logger, string clientName, Exception exception)
public static void HandlerExpired(Lazy<ILogger> loggerLazy, string clientName, TimeSpan lifetime)
{
_cleanupItemFailed(logger, clientName, exception);
if (TryGetLogger(loggerLazy, out ILogger? logger))
{
_handlerExpired(logger, lifetime.TotalMilliseconds, clientName, null);
}
}

public static void HandlerExpired(ILogger logger, string clientName, TimeSpan lifetime)
private static bool TryGetLogger(Lazy<ILogger> loggerLazy, [NotNullWhen(true)] out ILogger? logger)
{
_handlerExpired(logger, lifetime.TotalMilliseconds, clientName, null);
logger = null;
try
{
logger = loggerLazy.Value;
}
catch { } // not throwing in logs

return logger is not null;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Logging.Testing;
using Microsoft.Extensions.Options;
using Moq;
using Moq.Protected;
Expand Down Expand Up @@ -508,6 +510,29 @@ private async Task<ExpiredHandlerTrackingEntry> SimulateClientUse_Factory_Cleanu
return cleanupEntry;
}

[Fact]
public void ServiceProviderDispose_DebugLoggingDoesNotThrow()
{
var sink = new TestSink();

var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging();
serviceCollection.AddSingleton<ILoggerFactory>(new TestLoggerFactory(sink, enabled: true));
serviceCollection.AddHttpClient("test");

var serviceProvider = serviceCollection.BuildServiceProvider();

var httpClientFactory = (DefaultHttpClientFactory)serviceProvider.GetRequiredService<IHttpClientFactory>();
_ = httpClientFactory.CreateClient("test");

serviceProvider.Dispose();

httpClientFactory.StartCleanupTimer(); // we need to create a timer instance before triggering cleanup; normally it happens after the first expiry
httpClientFactory.CleanupTimer_Tick(); // trigger cleanup to (try to) write debug logs
// but no log is added, because ILogger couldn't be created
Assert.Equal(0, sink.Writes.Count(w => w.LoggerName == typeof(DefaultHttpClientFactory).FullName));
}

private class TestHttpClientFactory : DefaultHttpClientFactory
{
public TestHttpClientFactory(
Expand Down

0 comments on commit 2470610

Please sign in to comment.