SafariWebExtensionHandler Sometimes Doesn't Respond

My Safari Web Extension app for iOS sometimes exhibits an issue where the request to SafariWebExtensionHandler sometimes doesn't return.

This never happens on the simulator, and never happens when I'm actively debugging the SafariWebExtensionHandler process. It only happens on a physical device, and it only happens about 5% of the time. Note that the request happens RIGHT when the page loads (document_start), so I wonder if there's some kind of race condition happening, or a bug in iOS.

No errors are thrown, and I've pared back the logic to be extremely simple, and I still see the issue persist. I'm PRETTY sure it's the SafariWebExtensionHandler because I've tested bypassing it completely (returning a dummy response from background.js), and when I do, the issue never happens.

I've seen this issue posted before:, but without a resolution.

SafariWebExtensionHandler.m?:

- (void)beginRequestWithExtensionContext:(NSExtensionContext *)context {
    NSExtensionItem *response = [[NSExtensionItem alloc] init];
    response.userInfo = @{ SFExtensionMessageKey: @{
        @"op2": @(YES),
        @"op3": @(YES),
        @"op4": @(YES),
        @"op5": @(YES),
        @"op6":@(YES),
        @"op1": @(NO)
    } };
    [context completeRequestReturningItems:@[ response ] completionHandler:nil];
}

background.js (removed proprietary logic so excuse syntax errors):

browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
    console.log("Received request: ", request);
    if (request.action === "getUserSettings") {
        var payload = JSON.stringify({cmd: "getUserSettings"});
        sendMessageToNative(payload, async function(response) {
            sendResponse(response);
        });
        return true;
    }
    return true;
});

content.js

browser.runtime.sendMessage({ action: "getUserSettings" }).then((response) => {
...
});

I've worked around this by waiting for a few seconds and then resending the request if I never got a response (and this workaround works!), but this results in a bad UX. So, does anyone have any tips, pointers, etc?

SafariWebExtensionHandler Sometimes Doesn't Respond
 
 
Q