[Webkit-unassigned] [Bug 247372] New: MessagePort failing to receive postmessage events

bugzilla-daemon at webkit.org bugzilla-daemon at webkit.org
Wed Nov 2 11:08:32 PDT 2022


https://bugs.webkit.org/show_bug.cgi?id=247372

            Bug ID: 247372
           Summary: MessagePort failing to receive postmessage events
           Product: WebKit
           Version: Safari 16
          Hardware: All
                OS: All
            Status: NEW
          Severity: Major
          Priority: P2
         Component: JavaScriptCore
          Assignee: webkit-unassigned at lists.webkit.org
          Reporter: twm at outlook.com

See https://github.com/GoogleChromeLabs/comlink/issues/600.

The basic pattern comlink uses creates a MessageChannel, keeps one MessagePort for itself and transfers the other to the web worker.  It subscribes to the port it keeps for itself using AddEventListener, but otherwise does not retain a reference to that port, apart from the callback to AddEventListener which should keep the port alive...except, under certain conditions (memory pressure being a key factor), the port it keeps stops receiving message events from the paired web worker.  Once it stops receiving messages, it appears to stop forever -- it's not the case it drops occasional messages, but rather it just appears to permanently break.

A work around that has 100% mitigated the problem in our application is to patch the MessagePort add/remove event listener methods to add an explicit reference to the MessagePort.  E.g., this isn't what we did exactly, but it illustrates the idea:


        const retainedPorts = new Set();
        const pAddEventListener = MessagePort.prototype.addEventListener;
        MessagePort.prototype.addEventListener = function (
            type: string,
            listener: EventListenerOrEventListenerObject,
            options?: boolean | AddEventListenerOptions
        ) {
            if (type === 'message') {
                retainedPorts.add(this);
            }

            pAddEventListener.call(this, type, listener, options);
        }


With that change, the MessagePort keeps receiving messages.  Since a main effect of adding that reference within the Set is to keep the MessagePort from being garbage collected, my best guess is that Safari is inappropriately garbage collecting the port under certain conditions.  It is far from a 100% repro, however.

Our app uses postmessage to web workers in core infrastructure, so this issue completely breaks us, which is why I marked this as major. I see a couple other issues reported in this space, but these have been active for years without being addressed and are not reflective, I think, of the severity.

https://bugs.webkit.org/show_bug.cgi?id=193184
https://bugs.webkit.org/show_bug.cgi?id=184502

-- 
You are receiving this mail because:
You are the assignee for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-unassigned/attachments/20221102/2159bbb5/attachment-0001.htm>


More information about the webkit-unassigned mailing list