NSWorkspace.openURL hangs in Mojave

Since Mojave, i'm noticing odd behaviors when usijng NSWorkspace.openNURL, selectFile, and other APIs.


Very often (bit not consistently), these rwquests will simply hang indefinitely. I'm assuming this has to do with the new ockdowmn for AppleEvents & Co in Mojave. A typical hang will have this callstack — it doesn't matter whether i call openURL (& Co) on the main thread or ion a backgrous queue, it will hang (of course, calling it on main means ther whole app gangs) — looks like this:


F273

semaphore_wait_trap (libsystem_kernel.dylib)

_dispatch_sema4_wait (libdispatch.dylib)

_dispatch_semaphore_wait_slow (libdispatch.dylib)

_AppleEventsLookupApplicationPort (AE)

copyPortByPSN (AE)

EventAddress::copyAddressPort() const (AE)

AESendMessage (AE)

_LSAnnotateAndSendAppleEvent(AEDesc*, AEDesc const*, audit_token_t const*, _LSOpen2Options const*, NSError**) (LaunchServices)

_LSAnnotateAndSendAppleEvent(LSOpenState*, AEDesc*) (LaunchServices)

_LSOpenItemsWithHandler_CFDictionaryApplier(void const*, void const*, void*) (LaunchServices)

__CFDictionaryApplyFunction_block_invoke (CoreFoundation)

CFBasicHashApply (CoreFoundation)

CFDictionaryApplyFunction (CoreFoundation)

_LSOpenStuffCallLocal (LaunchServices)

_LSOpenStuff (LaunchServices)

_LSOpenURLsWithRole_Common (LaunchServices)

_LSOpenURLsWithRole_CommonLegacy(__CFArray const*, unsigned int, AEKeyDesc const*, LSApplicationParameters_V1 const*, _LSOpen2Options const*, ProcessSerialNumber*, long, __CFURL const**, unsigned char*) (LaunchServices)

LSOpenFromURLSpec (LaunchServices)

-[NSWorkspace openURL:] (AppKit)


I tried calling AEDeterminePermissionToAutomateTarget, but that *too* simply hangs. I do have a `NSAppleEventsUsageDescription` key in my Info.plist.


i notiuce then hang is more likewly to happen when my app is not codesigned (i.e. whiule working on it/debugging), while the (Developer ID) code-signed production build *mostly* works.


Is anyone else seeing this? Is this *expected* behavior (for non-signed apps, at least)? Am i missing something else that needs to be set/called?


thanx!

marc

Replies

You should only ever call NSWorkspace APIs from the main thread. I've never seen them lock up.

> I've never seen them lock up.


Well, thats super-cool for you. they do for me... <shrugs>

Full callstack, just for completeness; in this case calling openURL with a remote "https" url, hence expecting it to open in Safari:


3146 (Main)
  semaphore_wait_trap (libsystem_kernel.dylib)
  _dispatch_sema4_wait (libdispatch.dylib)
  _dispatch_semaphore_wait_slow (libdispatch.dylib)
  _AppleEventsLookupApplicationPort (AE)
  copyPortByPSN (AE)
  EventAddress::copyAddressPort() const (AE)
  AESendMessage (AE)
  _LSAnnotateAndSendAppleEvent(AEDesc*, AEDesc const*, audit_token_t const*, _LSOpen2Options const*, NSError**) (LaunchServices)
  _LSAnnotateAndSendAppleEvent(LSOpenState*, AEDesc*) (LaunchServices)
  _LSOpenItemsWithHandler_CFDictionaryApplier(void const*, void const*, void*) (LaunchServices)
  __CFDictionaryApplyFunction_block_invoke (CoreFoundation)
  CFBasicHashApply (CoreFoundation)
  CFDictionaryApplyFunction (CoreFoundation)
  _LSOpenStuffCallLocal (LaunchServices)
  _LSOpenStuff (LaunchServices)
  _LSOpenURLsWithRole_Common (LaunchServices)
  _LSOpenURLsWithRole_CommonLegacy(__CFArray const*, unsigned int, AEKeyDesc const*, LSApplicationParameters_V1 const*, _LSOpen2Options const*, ProcessSerialNumber*, long, __CFURL const**, unsigned char*) (LaunchServices)
  LSOpenFromURLSpec (LaunchServices)
  -[NSWorkspace openURL:] (AppKit)
  -[MainWindowController doubleClick:] (Bugs)
  -[NSApplication(NSResponder) sendAction:to:from:] (AppKit)
  -[NSControl sendAction:to:] (AppKit)
  -[NSTableView _sendAction:to:row:column:] (AppKit)
  -[NSTableView mouseDown:] (AppKit)
  -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] (AppKit)
  -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] (AppKit)
  -[NSWindow(NSEventRouting) sendEvent:] (AppKit)
  -[NSApplication(NSEvent) sendEvent:] (AppKit)
  -[NSApplication run] (AppKit)
  NSApplicationMain (AppKit)
  start (libdyld.dylib)

Calling AEDeterminePermissionToAutomateTarget (docs for this one explicitly say NOT to call it on main, so i call it in a global background queue — but again, it makes no diffewrence whether i call it from main or not) hangs too: (also, no, it's not that these two don't interlock each other. either one will hang, even if i remove the other call).


3BDC
  semaphore_wait_trap (libsystem_kernel.dylib)
  _dispatch_sema4_wait (libdispatch.dylib)
  _dispatch_semaphore_wait_slow (libdispatch.dylib)
  _AppleEventsLookupApplicationPort (AE)
  copyPortByBundleID (AE)
  EventAddress::copyAddressPort() const (AE)
  AEDeterminePermissionToAutomateTarget (AE)
  AppDelegate__<>c__DisplayClass0 <requestAppleEvents>b__0$0 (Bugs)
  _dispatch_call_block_and_release (libdispatch.dylib)
  _dispatch_client_callout (libdispatch.dylib)
  _dispatch_queue_override_invoke (libdispatch.dylib)
  _dispatch_root_queue_drain (libdispatch.dylib)
  _dispatch_worker_thread2 (libdispatch.dylib)
  _pthread_wqthread (libsystem_pthread.dylib)
  start_wqthread (libsystem_pthread.dylib)

You did specifically ask if anyone else was seeing this and if it was expected behaviour.

Sometimes, even using the document icon in the title bar to go to the containing folder will hang. None of my own code involved at all:


  semaphore_wait_trap (libsystem_kernel.dylib)
  _dispatch_sema4_wait (libdispatch.dylib)
  _dispatch_semaphore_wait_slow (libdispatch.dylib)
  _AppleEventsLookupApplicationPort (AE)
  copyPortBySIG (AE)
  AESendMessage (AE)
  -[NSWorkspace _sendFinderAppleEvent:class:file:] (AppKit)
  -[NSWorkspace activateFileViewerSelectingURLs:] (AppKit)
  -[NSWorkspace selectFile:inFileViewerRootedAtPath:] (AppKit)
  -[NSThemeDocumentButton(NSThemeFramePathExtensions) pathCellClicked:] (AppKit)
  -[NSApplication(NSResponder) sendAction:to:from:] (AppKit)
  -[NSControl sendAction:to:] (AppKit)
  -[NSPathCell _menuItemClick:] (AppKit)
  -[NSApplication(NSResponder) sendAction:to:from:] (AppKit)
  -[NSMenuItem _corePerformAction] (AppKit)
  -[NSCarbonMenuImpl performActionWithHighlightingForItemAtIndex:] (AppKit)
  -[NSMenu performActionForItemAtIndex:] (AppKit)
  -[NSMenu _internalPerformActionForItemAtIndex:] (AppKit)
  -[NSCarbonMenuImpl _carbonCommandProcessEvent:handlerCallRef:] (AppKit)
  NSSLMMenuEventHandler (AppKit)
  DispatchEventToHandlers(EventTargetRec*, OpaqueEventRef*, HandlerCallRec*) (HIToolbox)
  SendEventToEventTargetInternal(OpaqueEventRef*, OpaqueEventTargetRef*, HandlerCallRec*) (HIToolbox)
  SendEventToEventTarget (HIToolbox)
  SendHICommandEvent(unsigned int, HICommand const*, unsigned int, unsigned int, unsigned char, void const*, OpaqueEventTargetRef*, OpaqueEventTargetRef*, OpaqueEventRef**) (HIToolbox)
  SendMenuCommandWithContextAndModifiers (HIToolbox)
  SendMenuItemSelectedEvent (HIToolbox)
  FinishMenuSelection(SelectionData*, MenuResult*, MenuResult*) (HIToolbox)
  PopUpMenuSelectCore(MenuData*, Point, double, Point, unsigned short, unsigned int, unsigned int, Rect const*, unsigned short, unsigned int, Rect const*, Rect const*, __CFDictionary const*, __CFString const*, OpaqueMenuRef**, unsigned short*) (HIToolbox)
  _HandlePopUpMenuSelection8(OpaqueMenuRef*, OpaqueEventRef*, unsigned int, Point, unsigned short, unsigned int, unsigned int, Rect const*, unsigned short, Rect const*, Rect const*, __CFDictionary const*, __CFString const*, OpaqueMenuRef**, unsigned short*) (HIToolbox)
  _HandlePopUpMenuSelectionWithDictionary (HIToolbox)
  _NSSLMPopUpCarbonMenu3 (AppKit)
  -[NSCarbonMenuImpl popUpMenu:atLocation:width:forView:withSelectedItem:withFont:withFlags:withOptions:] (AppKit)
  -[NSThemeDocumentButton(NSThemeFramePathExtensions) showPopup] (AppKit)
  -[NSThemeFrame handleRightMouseDown:] (AppKit)
  -[NSTitlebarView rightMouseDown:] (AppKit)
  forwardMethod (AppKit)
  -[NSView rightMouseDown:] (AppKit)
  -[NSControl _rightMouseUpOrDown:] (AppKit)
  -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] (AppKit)
  -[NSWindow(NSEventRouting) sendEvent:] (AppKit)
  -[NSApplication(NSEvent) sendEvent:] (AppKit)
  -[NSApplication run] (AppKit)
  NSApplicationMain (AppKit)
  +[Program Main::] (Program.cs, line 35)
  main (Fire)
  start (libdyld.dylib)