public void OnCriticalError(string errorMessage, Exception exception) { //TODO: Decide if shutting down the process is the best response to a critical error //http://docs.particular.net/nservicebus/hosting/critical-errors var fatalMessage = string.Format("The following critical error was encountered:\n{0}\nProcess is shutting down.", errorMessage); Logger.Fatal(fatalMessage, exception); Environment.FailFast(fatalMessage, exception); }
public void FailFast(string message) { Env.FailFast(message); }
public void FailFast(string message, Exception exception) { Env.FailFast(message, exception); }
private static string?InternalGetResourceString(string?key) { if (string.IsNullOrEmpty(key)) { Debug.Fail("SR::GetResourceString with null or empty key. Bug in caller, or weird recursive loading problem?"); return(key !); } // We have a somewhat common potential for infinite // loops with mscorlib's ResourceManager. If "potentially dangerous" // code throws an exception, we will get into an infinite loop // inside the ResourceManager and this "potentially dangerous" code. // Potentially dangerous code includes the IO package, CultureInfo, // parts of the loader, some parts of Reflection, Security (including // custom user-written permissions that may parse an XML file at // class load time), assembly load event handlers, etc. Essentially, // this is not a bounded set of code, and we need to fix the problem. // Fortunately, this is limited to mscorlib's error lookups and is NOT // a general problem for all user code using the ResourceManager. // The solution is to make sure only one thread at a time can call // GetResourceString. Also, since resource lookups can be // reentrant, if the same thread comes into GetResourceString // twice looking for the exact same resource name before // returning, we're going into an infinite loop and we should // return a bogus string. bool lockTaken = false; try { Monitor.Enter(_lock, ref lockTaken); // Are we recursively looking up the same resource? Note - our backout code will set // the ResourceHelper's currentlyLoading stack to null if an exception occurs. if (_currentlyLoading != null && _currentlyLoading.Count > 0 && _currentlyLoading.LastIndexOf(key) != -1) { // We can start infinitely recursing for one resource lookup, // then during our failure reporting, start infinitely recursing again. // avoid that. if (_infinitelyRecursingCount > 0) { return(key); } _infinitelyRecursingCount++; // Note: our infrastructure for reporting this exception will again cause resource lookup. // This is the most direct way of dealing with that problem. string message = $"Infinite recursion during resource lookup within {System.CoreLib.Name}. This may be a bug in {System.CoreLib.Name}, or potentially in certain extensibility points such as assembly resolve events or CultureInfo names. Resource name: {key}"; Environment.FailFast(message); } _currentlyLoading ??= new List <string>(); // Call class constructors preemptively, so that we cannot get into an infinite // loop constructing a TypeInitializationException. If this were omitted, // we could get the Infinite recursion assert above by failing type initialization // between the Push and Pop calls below. if (!_resourceManagerInited) { RuntimeHelpers.RunClassConstructor(typeof(ResourceManager).TypeHandle); RuntimeHelpers.RunClassConstructor(typeof(ResourceReader).TypeHandle); RuntimeHelpers.RunClassConstructor(typeof(RuntimeResourceSet).TypeHandle); RuntimeHelpers.RunClassConstructor(typeof(BinaryReader).TypeHandle); _resourceManagerInited = true; } _currentlyLoading.Add(key); // Push string?s = ResourceManager.GetString(key, null); _currentlyLoading.RemoveAt(_currentlyLoading.Count - 1); // Pop Debug.Assert(s != null, "Managed resource string lookup failed. Was your resource name misspelled? Did you rebuild mscorlib after adding a resource to resources.txt? Debug this w/ cordbg and bug whoever owns the code that called SR.GetResourceString. Resource name was: \"" + key + "\""); return(s ?? key); } catch { if (lockTaken) { // Backout code - throw away potentially corrupt state s_resourceManager = null; _currentlyLoading = null; } throw; } finally { if (lockTaken) { Monitor.Exit(_lock); } } }
public static void Abandon() { Environment.FailFast("A program error has occurred."); }
private static void AppendExceptionStackFrame(object exceptionObj, IntPtr IP, int flags) { // This method is called by the runtime's EH dispatch code and is not allowed to leak exceptions // back into the dispatcher. try { Exception ex = exceptionObj as Exception; if (ex == null) { Environment.FailFast("Exceptions must derive from the System.Exception class"); } if (!RuntimeExceptionHelpers.SafeToPerformRichExceptionSupport) { return; } bool isFirstFrame = (flags & (int)RhEHFrameType.RH_EH_FIRST_FRAME) != 0; bool isFirstRethrowFrame = (flags & (int)RhEHFrameType.RH_EH_FIRST_RETHROW_FRAME) != 0; // When we're throwing an exception object, we first need to clear its stacktrace with two exceptions: // 1. Don't clear if we're rethrowing with `throw;`. // 2. Don't clear if we're throwing through ExceptionDispatchInfo. // This is done through invoking RestoreDispatchState which sets the last frame to EdiSeparator followed by throwing normally using `throw ex;`. if (!isFirstRethrowFrame && isFirstFrame && ex._idxFirstFreeStackTraceEntry > 0 && ex._corDbgStackTrace[ex._idxFirstFreeStackTraceEntry - 1] != StackTraceHelper.SpecialIP.EdiSeparator) { ex._idxFirstFreeStackTraceEntry = 0; } // If out of memory, avoid any calls that may allocate. Otherwise, they may fail // with another OutOfMemoryException, which may lead to infinite recursion. bool fatalOutOfMemory = ex == PreallocatedOutOfMemoryException.Instance; if (!fatalOutOfMemory) { ex.AppendStackIP(IP, isFirstRethrowFrame); } // UNIX-TODO: RhpEtwExceptionThrown #if TARGET_WINDOWS if (isFirstFrame) { string typeName = !fatalOutOfMemory?ex.GetType().ToString() : "System.OutOfMemoryException"; string message = !fatalOutOfMemory ? ex.Message : "Insufficient memory to continue the execution of the program."; unsafe { fixed(char *exceptionTypeName = typeName, exceptionMessage = message) RuntimeImports.RhpEtwExceptionThrown(exceptionTypeName, exceptionMessage, IP, ex.HResult); } } #endif } catch { // We may end up with a confusing stack trace or a confusing ETW trace log, but at least we // can continue to dispatch this exception. } }
public static void FailFast(string message, Exception exception) { Environment.FailFast(message, exception); }
public static void FailFast(string message) { Environment.FailFast(message); }