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

MemoryCache - Remove unhandled exception handler #105853

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
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ public class MemoryCache : ObjectCache, IEnumerable, IDisposable
private readonly bool _configLess;
private bool _useMemoryCacheManager = true;
private bool _throwOnDisposed;
private EventHandler _onAppDomainUnload;
private UnhandledExceptionEventHandler _onUnhandledException;
private int _inUnhandledExceptionHandler;
#if NET
[UnsupportedOSPlatformGuard("browser")]
private static bool _countersSupported => !OperatingSystem.IsBrowser();
Expand Down Expand Up @@ -216,13 +213,6 @@ private void InitDisposableMembers(NameValueCollection config)
_storeRefs[i] = new GCHandleRef<MemoryCacheStore>(new MemoryCacheStore(this, _perfCounters));
}
_stats = new MemoryCacheStatistics(this, config);
AppDomain appDomain = Thread.GetDomain();
EventHandler onAppDomainUnload = new EventHandler(OnAppDomainUnload);
appDomain.DomainUnload += onAppDomainUnload;
_onAppDomainUnload = onAppDomainUnload;
UnhandledExceptionEventHandler onUnhandledException = new UnhandledExceptionEventHandler(OnUnhandledException);
appDomain.UnhandledException += onUnhandledException;
_onUnhandledException = onUnhandledException;
dispose = false;
}
finally
Expand All @@ -234,26 +224,6 @@ private void InitDisposableMembers(NameValueCollection config)
}
}

private void OnAppDomainUnload(object unusedObject, EventArgs unusedEventArgs)
{
Dispose();
}

internal bool InUnhandledExceptionHandler => _inUnhandledExceptionHandler > 0;
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs eventArgs)
{
Interlocked.Increment(ref _inUnhandledExceptionHandler);

// if the CLR is terminating, dispose the cache.
// This will dispose the perf counters
if (eventArgs.IsTerminating)
{
Dispose();
}

Interlocked.Decrement(ref _inUnhandledExceptionHandler);
}

private static void ValidatePolicy(CacheItemPolicy policy)
{
if (policy.AbsoluteExpiration != ObjectCache.InfiniteAbsoluteExpiration
Expand Down Expand Up @@ -477,8 +447,6 @@ public void Dispose()
{
if (Interlocked.Exchange(ref _disposed, 1) == 0)
{
// unhook domain events
DisposeSafeCritical();
// stats must be disposed prior to disposing the stores.
_stats?.Dispose();
if (_storeRefs != null)
Expand All @@ -499,19 +467,6 @@ public void Dispose()
}
}

private void DisposeSafeCritical()
{
AppDomain appDomain = Thread.GetDomain();
if (_onAppDomainUnload != null)
{
appDomain.DomainUnload -= _onAppDomainUnload;
}
if (_onUnhandledException != null)
{
appDomain.UnhandledException -= _onUnhandledException;
}
}

private object GetInternal(string key, string regionName)
{
if (regionName != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,19 +340,8 @@ public void Dispose()
GCHandleRef<Timer> timerHandleRef = _timerHandleRef;
if (timerHandleRef != null && Interlocked.CompareExchange(ref _timerHandleRef, null, timerHandleRef) == timerHandleRef)
{
// If inside an unhandled exception handler, Timers may be succeptible to deadlocks. Use a safer approach.
if (_memoryCache.InUnhandledExceptionHandler)
{
// This does not stop/dispose the timer. But the callback on the timer is protected by _disposed, which we have already
// set above.
timerHandleRef.FreeHandle();
Dbg.Trace("MemoryCacheStats", "Freed CacheMemoryTimers");
}
else
{
timerHandleRef.Dispose();
Dbg.Trace("MemoryCacheStats", "Stopped CacheMemoryTimers");
}
timerHandleRef.Dispose();
Dbg.Trace("MemoryCacheStats", "Stopped CacheMemoryTimers");
}
}
while (_inCacheManagerThread != 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ public T Target
public void Dispose()
{
Target.Dispose();
FreeHandle();
}

internal void FreeHandle()
{
// Safe to call Dispose more than once but not thread-safe
if (_handle.IsAllocated)
{
Expand Down