diff --git a/src/main.swift b/src/main.swift index a6e2c736..681e3dc3 100644 --- a/src/main.swift +++ b/src/main.swift @@ -1,11 +1,22 @@ import AppKit import Darwin -// if the app is quit/force-quit from Activity Monitor, it will receive SIGTERM and applicationWillTerminate won't be called -// we intercept SIGTERM so we can reset the native command-tab shortcut -signal(SIGTERM) { _ in - // symbolic hotkeys state persist after the app is quit; we restore this shortcut before quitting +// - 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) + 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 setNativeCommandTabEnabled(true) + debugPrint("Exiting after receiving uncaught NSException", exception) exit(0) }