diff --git a/src/main.swift b/src/main.swift index 1cb89a14..b5da40b1 100644 --- a/src/main.swift +++ b/src/main.swift @@ -1,6 +1,23 @@ import AppKit import Darwin +// - if the app is quit/force-quit from Activity Monitor, it will receive SIGTERM and applicationWillTerminate won't be called +// - if the app crashes in swift code (e.g. unexpected nil object), SIGTRAP is sent +// we intercept these signals, and do an emergency exit +[SIGTERM, SIGTRAP].forEach { + signal($0) { s in + emergencyExit("Exiting after receiving signal", s) + } +} + +// - if the app crashes in objective-c code, an NSException may be sent +// we intercept the exception, and do an emergency exit +NSSetUncaughtExceptionHandler { (exception) in + emergencyExit("Exiting after receiving uncaught NSException", exception) +} + +App.shared.run() + func printStackTrace() { let stackSymbols = Thread.callStackSymbols for symbol in stackSymbols { @@ -8,25 +25,10 @@ func printStackTrace() { } } -// - if the app is quit/force-quit from Activity Monitor, it will receive SIGTERM and applicationWillTerminate won't be called -// - if the app crashes in swift code (e.g. unexpected nil object), SIGTRAP is sent -// we intercept these signals, and reset the native command-tab shortcut -[SIGTERM, SIGTRAP].forEach { - signal($0) { s in - setNativeCommandTabEnabled(true) - debugPrint("Exiting after receiving signal", s) - printStackTrace() - exit(0) - } -} - -// - if the app crashes in objective-c code, an NSException may be sent -// we intercept the exception, and reset the native command-tab shortcut -NSSetUncaughtExceptionHandler { (exception) in +// during an emergency exit, we re-enable the native command+tab, and log +fileprivate func emergencyExit(_ logs: Any?...) { setNativeCommandTabEnabled(true) - debugPrint("Exiting after receiving uncaught NSException", exception) + print(logs) printStackTrace() exit(0) } - -App.shared.run()