"No current extension context; trying most recent context" errors in Safari App Extensions

Hello,

I've encoutered an issue with Safari App Extensions. My extension prints lots of suspect error logs in the Xcode console and inside Console.app. This happens basically whenever I make any interaction with the App Extension or with Safari. The most common and predictable error log I get is:

No current extension context; trying most recent context
(Subsystem: com.apple.SafariServices)

However, I also sometimes get the following error messages, albeit less frequently, which may be related:

No extension context for best match

No extension context for remote object

Error connecting back to host for remote object: NSCocoaErrorDomain, code: 4099

No known extension contexts for profile 00000000-0000-0000-0000-000000000000

Most recent extension context B7223E12-B563-45E0-97F8-50500BC6B994 does not have connection back to host; trying best match context

I haven't been able to find anything about these error logs in Apple documentation or on the Internet, so I did a bit of empirical investigation. I reproduced the bug in the following basic scenario: I've created a new Safari App Extension project in Xcode by going to File > New > Project > Safari Extension App. I've selected "Safari App extension" for the type and "Swift" for the language. The project comes by default with a "SafariExtensionHandler.swift" file, which includes the following code:

override func validateToolbarItem(in window: SFSafariWindow, validationHandler: @escaping ((Bool, String) -> Void)) {
    validationHandler(true, "")
}

No issues so far.

If I add the following call:

override func validateToolbarItem(in window: SFSafariWindow, validationHandler: @escaping ((Bool, String) -> Void)) {
    validationHandler(true, "")

    SFSafariApplication.getActiveWindow { window in
        // code
    }
}

There are still no error messages logged in the Console.

However, if I do this instead:

override func validateToolbarItem(in window: SFSafariWindow, validationHandler: @escaping ((Bool, String) -> Void)) {
    validationHandler(true, "")

    DispatchQueue.main.async {
        SFSafariApplication.getActiveWindow { window in
            // code
        }
    }
}

Then my Xcode console starts being spammed with "No current extension context; trying most recent context" error logs.

With some more testing, it seems that the most common/predictable situation that causes the error log seems to be when calling any Safari API (e.g. SFSafariApplication.getActiveWindow{} or even SFSafariApplication.setToolbarItemsNeedUpdate()) outside of a direct method call provided by the Safari App Extension API. So making API calls directly from inside validateToolbarItem(in:, validationHandler:) or messageReceived(withName:from:userInfo:) calls is fine, but anything else causes "No extension context" logs. The bug even reproduces if you make a Safari API call directly inside of an @IBAction method call caused by a button click inside the Safari popover of the Safari App Extension.

With this being the case, it seems to be impossible to make clean Safari API calls in an asynchronous or proactive way, which is problematic for our app extension use case and which seems to defeat the purpose of some of the API calls like SFSafariApplication.setToolbarItemsNeedUpdate(). Also, this seems to be a new issue.

I've tested these scenarios on various macOS versions that I had on hand (specifically, on macOS 10.15 Catalina, macOS 13 Ventura and macOS 14 Sonoma) and the bug seems to reproduce only on macOS 14 Sonoma. The Safari App Extension behaves as expected on previous macOS versions, with no suspect error logs.

Does anyone know what this issue is about?

In addition, our crash reporting service has recently received many crash logs for the Safari App Extension in question. The crashes are esoteric (no application code, only library code), they don't reproduce locally and I haven't found any information about them online, so I don't know what causes them. But they also happen only on macOS Sonoma 14 for our users and some libray calls from the stack traces mention the word "context", so this makes me think that the two issues are related. I've included some crash snippets below:

Crashed Thread:        3  Dispatch queue: com.apple.NSXPCConnection.user.endpoint

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000002
Exception Codes:       0x0000000000000001, 0x0000000000000002

Termination Reason:    Namespace SIGNAL, Code 11 Segmentation fault: 11
Terminating Process:   exc handler [74701]

VM Region Info: 0x2 is not in any region.  Bytes before following region: 4340744190
      REGION TYPE                    START - END         [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      UNUSED SPACE AT START
--->  
      __TEXT                      102ba8000-102c08000    [  384K] r-x/r-x SM=COW  /Applications/myApp.app/Contents/PlugIns/myExtension.appex/Contents/MacOS/myExtension

Thread 3 Crashed::  Dispatch queue: com.apple.NSXPCConnection.user.endpoint
0   libobjc.A.dylib               	       0x18eeb1c34 objc_msgSend + 52
1   SafariServices                	       0x1b4925910 -[_SFSafariExtensionContextManager addContext:] + 248
2   SafariServices                	       0x1b49328d0 -[SFSafariExtensionHandler beginRequestWithExtensionContext:] + 124
3   ExtensionFoundation           	       0x1eadccbb0 __112-[EXConcreteExtensionContextVendor _beginRequestWithExtensionItems:listenerEndpoint:withContextUUID:completion:]_block_invoke.102 + 212
4   CoreFoundation                	       0x18f346694 __invoking___ + 148
5   CoreFoundation                	       0x18f346514 -[NSInvocation invoke] + 428
6   Foundation                    	       0x19045c69c __NSXPCCONNECTION_IS_CALLING_OUT_TO_REPLY_BLOCK__ + 16
7   Foundation                    	       0x19045ad3c -[NSXPCConnection _decodeAndInvokeReplyBlockWithEvent:sequence:replyInfo:] + 520
8   Foundation                    	       0x19045a698 __88-[NSXPCConnection _sendInvocation:orArguments:count:methodSignature:selector:withProxy:]_block_invoke_3 + 188
9   libxpc.dylib                  	       0x18ef96764 _xpc_connection_reply_callout + 116
10  libxpc.dylib                  	       0x18ef9665c _xpc_connection_call_reply_async + 80
11  libdispatch.dylib             	       0x18f0d6468 _dispatch_client_callout3 + 20
12  libdispatch.dylib             	       0x18f0f3fc8 _dispatch_mach_msg_async_reply_invoke + 344
13  libdispatch.dylib             	       0x18f0dd898 _dispatch_lane_serial_drain + 368
14  libdispatch.dylib             	       0x18f0de578 _dispatch_lane_invoke + 432
15  libdispatch.dylib             	       0x18f0e92d0 _dispatch_root_queue_drain_deferred_wlh + 288
16  libdispatch.dylib             	       0x18f0e8b44 _dispatch_workloop_worker_thread + 404
17  libsystem_pthread.dylib       	       0x18f28300c _pthread_wqthread + 288
18  libsystem_pthread.dylib       	       0x18f281d28 start_wqthread + 8

And another crash snippet:

Crashed Thread:        3  Dispatch queue: com.apple.NSXPCConnection.user.endpoint

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000020
Exception Codes:       0x0000000000000001, 0x0000000000000020

Termination Reason:    Namespace SIGNAL, Code 11 Segmentation fault: 11
Terminating Process:   exc handler [30310]

VM Region Info: 0x20 is not in any region.  Bytes before following region: 4329406432
      REGION TYPE                    START - END         [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      UNUSED SPACE AT START
--->  
      __TEXT                      1020d8000-102138000    [  384K] r-x/r-x SM=COW  /Applications/myApp.app/Contents/PlugIns/myExtension.appex/Contents/MacOS/myExtension

Thread 3 Crashed::  Dispatch queue: com.apple.NSXPCConnection.user.endpoint
0   libobjc.A.dylib               	       0x19d303fbc objc_retain + 16
1   SafariServices                	       0x1c2d79910 -[_SFSafariExtensionContextManager addContext:] + 248
2   SafariServices                	       0x1c2d868d0 -[SFSafariExtensionHandler beginRequestWithExtensionContext:] + 124
3   ExtensionFoundation           	       0x1f9220bb0 __112-[EXConcreteExtensionContextVendor _beginRequestWithExtensionItems:listenerEndpoint:withContextUUID:completion:]_block_invoke.102 + 212
4   CoreFoundation                	       0x19d79a694 __invoking___ + 148
5   CoreFoundation                	       0x19d79a514 -[NSInvocation invoke] + 428
6   Foundation                    	       0x19e8b069c __NSXPCCONNECTION_IS_CALLING_OUT_TO_REPLY_BLOCK__ + 16
7   Foundation                    	       0x19e8aed3c -[NSXPCConnection _decodeAndInvokeReplyBlockWithEvent:sequence:replyInfo:] + 520
8   Foundation                    	       0x19e8ae698 __88-[NSXPCConnection _sendInvocation:orArguments:count:methodSignature:selector:withProxy:]_block_invoke_3 + 188
9   libxpc.dylib                  	       0x19d3ea764 _xpc_connection_reply_callout + 116
10  libxpc.dylib                  	       0x19d3ea65c _xpc_connection_call_reply_async + 80
11  libdispatch.dylib             	       0x19d52a468 _dispatch_client_callout3 + 20
12  libdispatch.dylib             	       0x19d547fc8 _dispatch_mach_msg_async_reply_invoke + 344
13  libdispatch.dylib             	       0x19d531898 _dispatch_lane_serial_drain + 368
14  libdispatch.dylib             	       0x19d532578 _dispatch_lane_invoke + 432
15  libdispatch.dylib             	       0x19d53d2d0 _dispatch_root_queue_drain_deferred_wlh + 288
16  libdispatch.dylib             	       0x19d53cb44 _dispatch_workloop_worker_thread + 404
17  libsystem_pthread.dylib       	       0x19d6d700c _pthread_wqthread + 288
18  libsystem_pthread.dylib       	       0x19d6d5d28 start_wqthread + 8

Thanks for this information! Can you please file some feedback at https://feedbackassistant.apple.com with the crash logs/a sysdiagnose after reproducing the crash?

Thanks!

Hello, thanks for you reply!

As I've said, the crashes above have been received on our crash reporter from our Safari App Extension which is running in production on our users' machines. I haven't been able to reproduce any crash locally on my machine. Also, I just suspect that the crashes and the "No context" logs are the same issue, but I don't know this for certain.

With that in mind, I sent a bug submission through Feedback Assistant about this. The submission includes 30 user crashes (from the crash reporter service) that may be relevant, but no crashes from my machine. I've also added a few screenshots.

The feedback submission number is FB14156523.

"No current extension context; trying most recent context" errors in Safari App Extensions
 
 
Q