I don't know how much time you get here, so I'd keep the workload as light as possible (avoid starting mission-critical stuff here, I guess?). in applicationDidFinishLaunching, and you should now have a chance to do some work before your app is Force Quit. Call setHandler() somewhere in the beginning of your application's lifecycle, e.g. You could place the above in a C/Objective-C file and use it in Swift via a Bridging Header. If (signal(SIGTERM, handleSignal) = SIG_ERR) to set a signal handler.") Ĭall only asynchronous-safe functions within signal handlers. This will let us set a handler for a specific signal (SIGTERM in this case) If you Force Quit the application, it will still eventuallyĮxit, suggesting a follow-up SIGKILL is sent. We can actually omit the exit(), and everything SIGTERM is a clear directive to quit, so we exitĪnd return the signal number for us to inspect if we desire.
This will handle signals for us, specifically SIGTERM. The following has been shamelessly ripped from Wikipedia and reworked: #import
#How to force close on mac osx code#
I don't know how the following code is viewed by Apple for submissions to the App Store, so YMMV. Unfortunately, I believe that's a necessary aspect of this, if you want it to be reliable.ĭisclaimer: I'm not an expert, and researching for this post has stretched the limits of my rusty C/Objective-C knowledge. However, it could be challenging to move your operations out-of-process.
#How to force close on mac osx free#
This is a safe, deadlock-risk free way to monitor for arbitrary process exits. If/when that process disappears, you can then execute your code. The idea is you start up a child process, and in that child, observe the parent. I think you're only safe option is to use a sentinel process. This may not matter to do, depending on how strong a guarantee you need about detecting your process termination. Third, there are fatal events that aren't mapped to signals, on top of SIGKILL. And, those deadlocks will occur during unpredictable points of its execution. In short: it is almost certainly unsafe to run your code in a signal handler, and it will deadlock sometimes. However, since it can be delivered at any time, it still will be subject to async safety issues, even thought it is not a crash. Now, I will admit that my experience with signals does not include use of SIGTERM. But, will also sometimes deadlock, depending on what the thread was doing at the time. Calling unsafe functions, like NSLog, will sometimes work. If you check out "man sigaction" (note, sigaction is a much improved API over signal) you see that there are actually a very small number of functions that are safe to call from a signal handler. This is an part of a class of problems known as "async safety". Examples of things that hold locks - malloc/free, the Objective-C runtime. Second, signals interrupt a thread, and that thread can be doing anything, including holding locks. There are other, legitimate uses of signals, and installing handlers will negatively impact their behavior without very careful work. Unfortunately, there are, in fact, some major issues with the suggested approach.įirst, signal handlers are per-process. Major credit goes to for putting work in here to come up with an answer.