From e975cd7294c909e33b4ae154a49895e7c9f7d29a 2016-08-10 11:31:33 From: Min RK Date: 2016-08-10 11:31:33 Subject: [PATCH] run CFRunLoopRun if NSApp:run finishes on its own this seems to happen when the last Window is closed, but the eventloop needs to run a little longer in order to finish closing the Window --- diff --git a/IPython/terminal/pt_inputhooks/osx.py b/IPython/terminal/pt_inputhooks/osx.py index 9f41c3f..efa8854 100644 --- a/IPython/terminal/pt_inputhooks/osx.py +++ b/IPython/terminal/pt_inputhooks/osx.py @@ -7,6 +7,7 @@ Calls NSApp / CoreFoundation APIs via ctypes. import ctypes import ctypes.util +from threading import Event objc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('objc')) @@ -97,8 +98,11 @@ def _wake(NSApp): msg(NSApp, n('postEvent:atStart:'), void_p(event), True) +_triggered = Event() + def _input_callback(fdref, flags, info): """Callback to fire when there's input to be read""" + _triggered.set() CFFileDescriptorInvalidate(fdref) CFRelease(fdref) NSApp = _NSApp() @@ -111,6 +115,7 @@ _c_input_callback = _c_callback_func_type(_input_callback) def _stop_on_read(fd): """Register callback to stop eventloop when there's data on fd""" + _triggered.clear() fdref = CFFileDescriptorCreate(None, fd, False, _c_input_callback, None) CFFileDescriptorEnableCallBacks(fdref, kCFFileDescriptorReadCallBack) source = CFFileDescriptorCreateRunLoopSource(None, fdref, 0) @@ -130,4 +135,9 @@ def inputhook(context): return _stop_on_read(context.fileno()) msg(NSApp, n('run')) - + if not _triggered.is_set(): + # app closed without firing callback, + # probably due to last window being closed. + # Run the loop manually in this case, + # since there may be events still to process (#9734) + CoreFoundation.CFRunLoopRun()