2
  • Race condition when calling `chrome.runtime.connect` and immediately sending message

  • BugsDesktopDone

Steps to reproduce:
Install this demo extension and visit any webpage. Look at the console log for both the webpage and the extension's background page.

The extension contains:

  • A content script which calls chrome.runtime.connect(null, {name: 'asdf'}) and then immediately calls postMessage on the resulting port.
  • A background page which listens to chrome.runtime.onConnect; the handler in turn listens to onMessage on the new port, and both the connection and messages received are logged to the console.

Also, to help illustrate the race condition, the background page's onConnect handler has a random chance of spinning in a while loop for 500ms before adding the onMessage event listener. This shouldn't affect the results because the onMessage listener is still added synchronously from within the onConnect event listener; the JS event loop does not get a chance to run in between.

Expected behavior:
Every onConnect event should be followed by exactly one onMessage event, regardless of whether the 500ms spin is triggered. This works in Chrome and Firefox.

Actual behavior:
The onMessage event is sometimes missed. It is always missed if the 500ms spin is triggered, and sometimes missed even if it's not.

This suggests that Orion is dropping the message if there is no onMessage handler at the time the message is sent, even if the background page is still processing the onConnect handler at that point. This makes it impossible to reliably receive messages that are sent immediately after connecting.

Orion, OS version; hardware type:

Orion Version 0.99.122-beta (WebKit 615.1.11.7)

MacBook Pro (macOS Ventura 13.1 build 22C65)

M1

Note:
This appears to be one root cause of Yomichan not working.

    4 months later
    10 months later

    Hello, did you managed to get Yomichan working?

      No one is typing