Bug 217456 - WKUserContentController.removeScriptMessageHandler(forName:) seems to be racy
Summary: WKUserContentController.removeScriptMessageHandler(forName:) seems to be racy
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebKit API (show other bugs)
Version: WebKit Local Build
Hardware: Mac Other
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2020-10-07 16:41 PDT by Saagar Jha
Modified: 2020-10-07 19:01 PDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Saagar Jha 2020-10-07 16:41:40 PDT
From what I understand, WKUserContentController.add(_:name:) and WKUserContentController.removeScriptMessageHandler(forName:) both end up calling into WebUserContentControllerProxy::addUserScriptMessageHandler()/WebUserContentControllerProxy::removeUserMessageHandlerForName(), which send a message to the web processes asynchronously about the addition/removal. However, if this is how the API is designed, then there is a race where the UI process and the network process have a different idea of what message handlers are installed. I am seeing an assert being thrown that the reply handler in WebUserContentControllerProxy::didPostMessage() is not called, and it seems like what is happening is that this code gets hit:

RefPtr<WebScriptMessageHandler> handler = m_scriptMessageHandlers.get(messageHandlerID);
if (!handler)
    return;

and we bail out without calling the handler. But the real issue is that we get a message ID we supposedly don't know about, and it seems like what is occurring is that WKUserContentController.removeScriptMessageHandler(forName:) is called to remove a listener, we remove it from our mapping in the UI process, and in between the time the RemoveUserScriptMessageHandler message has been received by the web processes (so it can update its own map) if a message handler gets triggered the web process (which doesn't know about the removal yet) sends the UI process a stale message handler ID.

I'm unsure what the fix for this would be–should the IPC be made synchronous, so this kind of de-sync can't occur? Should we just call the reply handler to swallow the assert?
Comment 1 Radar WebKit Bug Importer 2020-10-07 19:01:15 PDT
<rdar://problem/70075142>