I'm on Pythonista 3 and atexit.register on its own doesn't quite work - running sys.exit will call the registered atexit function but forcibly terminating the program (via the stop button) doesn't do that.