Skip to content

Commit bd29323

Browse files
MemoryCache - Remove unhandled exception handler (#105853)
* Revert "Don't dispose timers if we're in our UnhandledException handler. (#103937)" * MemoryCache - Remove UnhandledException handler. * MemoryCache - Remove AppDomain.DomainUnload handler as well.
1 parent 8fe777f commit bd29323

File tree

3 files changed

+2
-63
lines changed

3 files changed

+2
-63
lines changed

src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/MemoryCache.cs

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ public class MemoryCache : ObjectCache, IEnumerable, IDisposable
3838
private readonly bool _configLess;
3939
private bool _useMemoryCacheManager = true;
4040
private bool _throwOnDisposed;
41-
private EventHandler _onAppDomainUnload;
42-
private UnhandledExceptionEventHandler _onUnhandledException;
43-
private int _inUnhandledExceptionHandler;
4441
#if NET
4542
[UnsupportedOSPlatformGuard("browser")]
4643
private static bool _countersSupported => !OperatingSystem.IsBrowser();
@@ -216,13 +213,6 @@ private void InitDisposableMembers(NameValueCollection config)
216213
_storeRefs[i] = new GCHandleRef<MemoryCacheStore>(new MemoryCacheStore(this, _perfCounters));
217214
}
218215
_stats = new MemoryCacheStatistics(this, config);
219-
AppDomain appDomain = Thread.GetDomain();
220-
EventHandler onAppDomainUnload = new EventHandler(OnAppDomainUnload);
221-
appDomain.DomainUnload += onAppDomainUnload;
222-
_onAppDomainUnload = onAppDomainUnload;
223-
UnhandledExceptionEventHandler onUnhandledException = new UnhandledExceptionEventHandler(OnUnhandledException);
224-
appDomain.UnhandledException += onUnhandledException;
225-
_onUnhandledException = onUnhandledException;
226216
dispose = false;
227217
}
228218
finally
@@ -234,26 +224,6 @@ private void InitDisposableMembers(NameValueCollection config)
234224
}
235225
}
236226

237-
private void OnAppDomainUnload(object unusedObject, EventArgs unusedEventArgs)
238-
{
239-
Dispose();
240-
}
241-
242-
internal bool InUnhandledExceptionHandler => _inUnhandledExceptionHandler > 0;
243-
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs eventArgs)
244-
{
245-
Interlocked.Increment(ref _inUnhandledExceptionHandler);
246-
247-
// if the CLR is terminating, dispose the cache.
248-
// This will dispose the perf counters
249-
if (eventArgs.IsTerminating)
250-
{
251-
Dispose();
252-
}
253-
254-
Interlocked.Decrement(ref _inUnhandledExceptionHandler);
255-
}
256-
257227
private static void ValidatePolicy(CacheItemPolicy policy)
258228
{
259229
if (policy.AbsoluteExpiration != ObjectCache.InfiniteAbsoluteExpiration
@@ -477,8 +447,6 @@ public void Dispose()
477447
{
478448
if (Interlocked.Exchange(ref _disposed, 1) == 0)
479449
{
480-
// unhook domain events
481-
DisposeSafeCritical();
482450
// stats must be disposed prior to disposing the stores.
483451
_stats?.Dispose();
484452
if (_storeRefs != null)
@@ -499,19 +467,6 @@ public void Dispose()
499467
}
500468
}
501469

502-
private void DisposeSafeCritical()
503-
{
504-
AppDomain appDomain = Thread.GetDomain();
505-
if (_onAppDomainUnload != null)
506-
{
507-
appDomain.DomainUnload -= _onAppDomainUnload;
508-
}
509-
if (_onUnhandledException != null)
510-
{
511-
appDomain.UnhandledException -= _onUnhandledException;
512-
}
513-
}
514-
515470
private object GetInternal(string key, string regionName)
516471
{
517472
if (regionName != null)

src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/MemoryCacheStatistics.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -340,19 +340,8 @@ public void Dispose()
340340
GCHandleRef<Timer> timerHandleRef = _timerHandleRef;
341341
if (timerHandleRef != null && Interlocked.CompareExchange(ref _timerHandleRef, null, timerHandleRef) == timerHandleRef)
342342
{
343-
// If inside an unhandled exception handler, Timers may be succeptible to deadlocks. Use a safer approach.
344-
if (_memoryCache.InUnhandledExceptionHandler)
345-
{
346-
// This does not stop/dispose the timer. But the callback on the timer is protected by _disposed, which we have already
347-
// set above.
348-
timerHandleRef.FreeHandle();
349-
Dbg.Trace("MemoryCacheStats", "Freed CacheMemoryTimers");
350-
}
351-
else
352-
{
353-
timerHandleRef.Dispose();
354-
Dbg.Trace("MemoryCacheStats", "Stopped CacheMemoryTimers");
355-
}
343+
timerHandleRef.Dispose();
344+
Dbg.Trace("MemoryCacheStats", "Stopped CacheMemoryTimers");
356345
}
357346
}
358347
while (_inCacheManagerThread != 0)

src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/SRef.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ public T Target
5959
public void Dispose()
6060
{
6161
Target.Dispose();
62-
FreeHandle();
63-
}
64-
65-
internal void FreeHandle()
66-
{
6762
// Safe to call Dispose more than once but not thread-safe
6863
if (_handle.IsAllocated)
6964
{

0 commit comments

Comments
 (0)