Delete dead code (dotnet/coreclr#18783)
authorJan Kotas <jkotas@microsoft.com>
Thu, 5 Jul 2018 06:04:02 +0000 (08:04 +0200)
committerGitHub <noreply@github.com>
Thu, 5 Jul 2018 06:04:02 +0000 (08:04 +0200)
- Leftover from unobserved exception desktop quirk
- Unnecessary calls to AppDomain.IsFinalizingForUnload(). They were actually never required even with AppDomain support because of Environment.HasShutdownStarted returns true during AppDomain unload.

Commit migrated from https://github.com/dotnet/coreclr/commit/cd1232d47cc028ebf6d22621ce63903a7e5c0e94

src/coreclr/src/System.Private.CoreLib/src/System/AppDomain.cs
src/coreclr/src/System.Private.CoreLib/src/System/Reflection/Emit/DynamicILGenerator.cs
src/coreclr/src/System.Private.CoreLib/src/System/Reflection/LoaderAllocator.cs
src/coreclr/src/System.Private.CoreLib/src/System/Threading/Tasks/TaskExceptionHolder.cs
src/coreclr/src/System.Private.CoreLib/src/System/Threading/ThreadPool.cs
src/coreclr/src/System.Private.CoreLib/src/System/Threading/Timer.cs
src/coreclr/src/vm/appdomainnative.cpp
src/coreclr/src/vm/appdomainnative.hpp
src/coreclr/src/vm/ecalllist.h

index 292fa926cba778801868ef7e7384d685c4104d48..d96710b8e97c857129e5820ef5d7b029c6a80632 100644 (file)
@@ -267,11 +267,6 @@ namespace System
             return nGetAssemblies(forIntrospection);
         }
 
-        // this is true when we've just started going through the finalizers and are forcing objects to finalize
-        // so must be aware that certain infrastructure may have gone away
-        [MethodImpl(MethodImplOptions.InternalCall)]
-        public extern bool IsFinalizingForUnload();
-
         [MethodImpl(MethodImplOptions.InternalCall)]
         internal static extern void PublishAnonymouslyHostedDynamicMethodsAssembly(RuntimeAssembly assemblyHandle);
 
index 48ad93dbdaa9afeecbb9dbd75e062f938759ce3c..3315daf17319aaa10fdeab18e2af20e0695110d7 100644 (file)
@@ -641,8 +641,7 @@ namespace System.Reflection.Emit
                 // We go over all DynamicMethodDesc during AppDomain shutdown and make sure
                 // that everything associated with them is released. So it is ok to skip reregistration
                 // for finalization during appdomain shutdown
-                if (!Environment.HasShutdownStarted &&
-                    !AppDomain.CurrentDomain.IsFinalizingForUnload())
+                if (!Environment.HasShutdownStarted)
                 {
                     // Try again later.
                     GC.ReRegisterForFinalize(this);
@@ -667,8 +666,7 @@ namespace System.Reflection.Emit
                 // It is not safe to destroy the method if the managed resolver is alive.
                 if (RuntimeMethodHandle.GetResolver(m_methodHandle) != null)
                 {
-                    if (!Environment.HasShutdownStarted &&
-                        !AppDomain.CurrentDomain.IsFinalizingForUnload())
+                    if (!Environment.HasShutdownStarted)
                     {
                         // Somebody might have been holding a reference on us via weak handle.
                         // We will keep trying. It will be hopefully released eventually.
index 54477734c270211f595b5beb4c6a6da5bf222d57..1119f07fa4efec0a2f1a0146eb4fb8db7a701bd2 100644 (file)
@@ -43,10 +43,9 @@ namespace System.Reflection
 
             // Assemblies and LoaderAllocators will be cleaned up during AppDomain shutdown in
             // unmanaged code
-            // So it is ok to skip reregistration and cleanup for finalization during appdomain shutdown.
+            // So it is ok to skip reregistration and cleanup for finalization during shutdown.
             // We also avoid early finalization of LoaderAllocatorScout due to AD unload when the object was inside DelayedFinalizationList.
-            if (!Environment.HasShutdownStarted &&
-                !AppDomain.CurrentDomain.IsFinalizingForUnload())
+            if (!Environment.HasShutdownStarted)
             {
                 // Destroy returns false if the managed LoaderAllocator is still alive.
                 if (!Destroy(m_nativeLoaderAllocator))
index a96852a6f00df5e810c05063b6c01f294382f905..3046bd9db88c9d2cda0d0dcd22b7ace2447bc9ae 100644 (file)
 // Disable the "reference to volatile field not treated as volatile" error.
 #pragma warning disable 0420
 
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Diagnostics;
+using System.Runtime.ExceptionServices;
+
 namespace System.Threading.Tasks
 {
-    using System;
-    using System.Collections.Generic;
-    using System.Collections.ObjectModel;
-    using System.Diagnostics;
-    using System.Runtime.ExceptionServices;
-    using System.Security;
-
     /// <summary>
     /// An exception holder manages a list of exceptions for one particular task.
     /// It offers the ability to aggregate, but more importantly, also offers intrinsic
     /// support for propagating unhandled exceptions that are never observed. It does
-    /// this by aggregating and throwing if the holder is ever GC'd without the holder's
-    /// contents ever having been requested (e.g. by a Task.Wait, Task.get_Exception, etc).
-    /// This behavior is prominent in .NET 4 but is suppressed by default beyond that release.
+    /// this by aggregating and calling UnobservedTaskException event event if the holder 
+    /// is ever GC'd without the holder's contents ever having been requested 
+    /// (e.g. by a Task.Wait, Task.get_Exception, etc).
     /// </summary>
     internal class TaskExceptionHolder
     {
-        /// <summary>Whether we should propagate exceptions on the finalizer.</summary>
-        private readonly static bool s_failFastOnUnobservedException = ShouldFailFastOnUnobservedException();
-        /// <summary>Whether the AppDomain has started to unload.</summary>
-        private static volatile bool s_domainUnloadStarted;
-        /// <summary>An event handler used to notify of domain unload.</summary>
-        private static volatile EventHandler s_adUnloadEventHandler;
-
         /// <summary>The task with which this holder is associated.</summary>
         private readonly Task m_task;
         /// <summary>
@@ -59,28 +50,6 @@ namespace System.Threading.Tasks
         {
             Debug.Assert(task != null, "Expected a non-null task.");
             m_task = task;
-            EnsureADUnloadCallbackRegistered();
-        }
-
-        private static bool ShouldFailFastOnUnobservedException()
-        {
-            return false;
-        }
-
-        private static void EnsureADUnloadCallbackRegistered()
-        {
-            if (s_adUnloadEventHandler == null &&
-                Interlocked.CompareExchange(ref s_adUnloadEventHandler,
-                                             AppDomainUnloadCallback,
-                                             null) == null)
-            {
-                AppDomain.CurrentDomain.DomainUnload += s_adUnloadEventHandler;
-            }
-        }
-
-        private static void AppDomainUnloadCallback(object sender, EventArgs e)
-        {
-            s_domainUnloadStarted = true;
         }
 
         /// <summary>
@@ -92,8 +61,7 @@ namespace System.Threading.Tasks
             // We need to do this filtering because all TaskExceptionHolders will be finalized during shutdown or unload
             // regardles of reachability of the task (i.e. even if the user code was about to observe the task's exception),
             // which can otherwise lead to spurious crashes during shutdown.
-            if (m_faultExceptions != null && !m_isHandled &&
-                !Environment.HasShutdownStarted && !AppDomain.CurrentDomain.IsFinalizingForUnload() && !s_domainUnloadStarted)
+            if (m_faultExceptions != null && !m_isHandled && !Environment.HasShutdownStarted)
             {
                 // We don't want to crash the finalizer thread if any ThreadAbortExceptions 
                 // occur in the list or in any nested AggregateExceptions.  
@@ -122,22 +90,12 @@ namespace System.Threading.Tasks
                 // other finalizer, and the Task was finalized before the holder, the holder
                 // will have been marked as handled before even getting here.
 
-                // Give users a chance to keep this exception from crashing the process
-
-                // First, publish the unobserved exception and allow users to observe it
+                // Publish the unobserved exception and allow users to observe it
                 AggregateException exceptionToThrow = new AggregateException(
                     SR.TaskExceptionHolder_UnhandledException,
                     m_faultExceptions);
                 UnobservedTaskExceptionEventArgs ueea = new UnobservedTaskExceptionEventArgs(exceptionToThrow);
                 TaskScheduler.PublishUnobservedTaskException(m_task, ueea);
-
-                // Now, if we are still unobserved and we're configured to crash on unobserved, throw the exception.
-                // We need to publish the event above even if we're not going to crash, hence
-                // why this check doesn't come at the beginning of the method.
-                if (s_failFastOnUnobservedException && !ueea.m_observed)
-                {
-                    throw exceptionToThrow;
-                }
             }
         }
 
index 490c805729cb4dcb7a48a31f8733bfc1b510198f..d9b378a2ab917217888676650d06906b7a50a3f1 100644 (file)
@@ -697,7 +697,7 @@ namespace System.Threading
             // if we're in the process of shutting down or unloading the AD.  In those cases, the work won't
             // execute anyway.  And there are subtle race conditions involved there that would lead us to do the wrong
             // thing anyway.  So we'll only clean up if this is a "normal" finalization.
-            if (!(Environment.HasShutdownStarted || AppDomain.CurrentDomain.IsFinalizingForUnload()))
+            if (!Environment.HasShutdownStarted)
                 CleanUp();
         }
     }
@@ -902,7 +902,7 @@ namespace System.Threading
         ~QueueUserWorkItemCallbackBase()
         {
             Debug.Assert(
-                executed != 0 || Environment.HasShutdownStarted || AppDomain.CurrentDomain.IsFinalizingForUnload(),
+                executed != 0 || Environment.HasShutdownStarted,
                 "A QueueUserWorkItemCallback was never called!");
         }
 
index d9aa01e50575966bd25843f21740df6351f3ddf1..b7e0cfb10e3b533edd5e2c8d6ac8ac8332b052d4 100644 (file)
@@ -650,7 +650,7 @@ namespace System.Threading
             // Note that in either case, the Timer still won't fire, because ThreadPool threads won't be
             // allowed to run in this AppDomain.
             //
-            if (Environment.HasShutdownStarted || AppDomain.CurrentDomain.IsFinalizingForUnload())
+            if (Environment.HasShutdownStarted)
                 return;
 
             m_timer.Close();
index 3c1b64a37080628b836f972fc2b041a5ff49cc84..49e0f6201307c58a579939998eaeccc11560f2d5 100644 (file)
@@ -419,22 +419,6 @@ FCIMPL1(Object*, AppDomainNative::GetDynamicDir, AppDomainBaseObject* refThisUNS
 }
 FCIMPLEND
 
-FCIMPL1(FC_BOOL_RET, AppDomainNative::IsFinalizingForUnload, AppDomainBaseObject* refThisUNSAFE)
-{
-    FCALL_CONTRACT;
-
-    BOOL            retVal = FALSE;
-    APPDOMAINREF    refThis = (APPDOMAINREF) refThisUNSAFE;
-    HELPER_METHOD_FRAME_BEGIN_RET_1(refThis);
-
-    AppDomain* pApp = ValidateArg(refThis);
-    retVal = pApp->IsFinalizing();
-
-    HELPER_METHOD_FRAME_END();
-    FC_RETURN_BOOL(retVal);
-}
-FCIMPLEND
-
 FCIMPL2(StringObject*, AppDomainNative::nApplyPolicy, AppDomainBaseObject* refThisUNSAFE, AssemblyNameBaseObject* refAssemblyNameUNSAFE)
 {
     FCALL_CONTRACT;
index e2d564a747da8cf64f7b939a60ece99283f304e8..9728ef8ec03ce60aa781ca9c9258101d08ef9cf7 100644 (file)
@@ -33,7 +33,6 @@ public:
     static FCDECL1(INT32, GetId, AppDomainBaseObject* refThisUNSAFE);
     static FCDECL1(INT32, GetIdForUnload, AppDomainBaseObject* refDomainUNSAFE);
     static FCDECL1(FC_BOOL_RET, IsDomainIdValid, INT32 dwId);
-    static FCDECL1(FC_BOOL_RET, IsFinalizingForUnload, AppDomainBaseObject* refThisUNSAFE);
     static FCDECL1(void, ForceToSharedDomain, Object* pObjectUNSAFE);
     static FCDECL1(LPVOID,  GetFusionContext, AppDomainBaseObject* refThis);
     static FCDECL2(Object*, IsStringInterned, AppDomainBaseObject* refThis, StringObject* pString);
index e575dedfb0dd251d1a97496e273f1e115c542095..8ee51b455b13ab8f1e6cc28518f410c4f0aabd74 100644 (file)
@@ -461,7 +461,6 @@ FCFuncStart(gAppDomainFuncs)
     FCFuncElement("GetOrInternString", AppDomainNative::GetOrInternString)
     QCFuncElement("nSetupBindingPaths", AppDomainNative::SetupBindingPaths)
     QCFuncElement("nSetNativeDllSearchDirectories", AppDomainNative::SetNativeDllSearchDirectories)
-    FCFuncElement("IsFinalizingForUnload", AppDomainNative::IsFinalizingForUnload)
     FCFuncElement("PublishAnonymouslyHostedDynamicMethodsAssembly", AppDomainNative::PublishAnonymouslyHostedDynamicMethodsAssembly)
 #ifdef FEATURE_APPDOMAIN_RESOURCE_MONITORING
     FCFuncElement("nEnableMonitoring", AppDomainNative::EnableMonitoring)