LPMetadataProvider never returns in SFSafariExtensionHandler

I am creating a Safari App Extension for users to save web links and quotes to my app. I would like to cache web page metadata when links are saved by the extension.

I am using a context menu command to capture information with the code below.

override func contextMenuItemSelected(
    withCommand command: String,
    in page: SFSafariPage,
    userInfo: [String : Any]? = nil)
 {
    switch command {
        case "sendToApp":

        Task {
            guard let properties = await page.properties(),
                  let pageURL = properties.url else {
                return
                }

                let provider = LPMetadataProvider()
                provider.timeout = 0.01

                os_log("*** Starting metadata call ***")

                let metadata = try? await provider.startFetchingMetadata(for: pageURL)

                os_log("*** Continued past metadata call ***")

                // ...
            }
        }

I get the log:

*** Starting metadata call ***
LPMetadataProvider<1>: start fetching for URL

...but I am never seeing the log "*** Continued past metadata call ***"

I wonder if the task is being killed for some reason?

I thought maybe async code was an issue in SFSafariExtensionHandler, but the first await call in the guard passes successfully.

I thought that the default timeout of 30s on LPMetadataProvider may be too great, but it still fails with a tiny timeout of 0.01s.

I have added com.apple.security.network.client to the entitlements of the extension.

Is there something I am missing please?

Answered by Gillies in 711861022

It seems that LPMetadataProvider can only be used on the main thread. This doesn't seem to be documented anywhere!

So, the fix is:

    Task { @MainActor
        guard let properties = await page.properties(),
        ...
Accepted Answer

It seems that LPMetadataProvider can only be used on the main thread. This doesn't seem to be documented anywhere!

So, the fix is:

    Task { @MainActor
        guard let properties = await page.properties(),
        ...
LPMetadataProvider never returns in SFSafariExtensionHandler
 
 
Q