Closed Bug 1706265 Opened 3 years ago Closed 3 years ago

Order of ports in MessageEvent.ports is random

Categories

(Core :: DOM: postMessage, defect, P3)

Firefox 88
defect

Tracking

()

RESOLVED FIXED
95 Branch
Tracking Status
firefox95 --- fixed

People

(Reporter: guanzo91, Assigned: sfink)

References

Details

Attachments

(3 files)

Attached image ff_bug.png

User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36

Steps to reproduce:

Reproduction links
Parent window: https://codesandbox.io/s/wonderful-dew-ktw1v?file=/index.html
Iframe window: https://codesandbox.io/s/hardcore-elbakyan-ou60y?file=/index.html

On the parent window link, click the refresh icon until you see "ERROR: ports swapped".

TL;DR

Parent window:
$iframe.contentWindow.postMessage(null, null, [port1, port2])

Iframe window:
window.addEventListener('message', async event => {
const [port1, port2] = event.ports // Sometimes it's this order
const [port2, port1] = event.ports // Sometimes it's this order
})

  1. Create two MessageChannels. postMessage each channels "port2" to an iframe.
  2. Setup message listeners on each channels "port1"
  3. Inside the iframe, retrieve ports from event.ports, and for each port, postMessage back to parent window.

Actual results:

Messages are sometimes received from the wrong channel. This is because sometimes, the order of ports passed to postMessage is not the same order when iframe retrieves ports from event.ports.

Expected results:

The order of ports in event.ports should stay the same after being postMessaged.

Attached video 2021-04-20 01-11-53.flv

The Bugbug bot thinks this bug should belong to the 'Core::DOM: Core & HTML' component, and is moving the bug to that component. Please revert this change in case you think the bot is wrong.

Component: Untriaged → DOM: Core & HTML
Product: Firefox → Core
Component: DOM: Core & HTML → DOM: postMessage
Severity: -- → S3
Priority: -- → P3

(In reply to Olli Pettay [:smaug] from comment #4)

hmm, is this coming from the JS engine, https://searchfox.org/mozilla-central/rev/489e82dcc1e5afbe691ff3b1c982382914637e38/js/src/vm/StructuredClone.cpp#593-595

Yes, definitely.

Flags: needinfo?(sphink)
Assignee: nobody → sphink
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true

Hm, I see why I didn't have a test for this. Within the JS engine, the order of Transferables is not visible. The only Transferable type is ArrayBuffer.

MessagePorts do a weird thing during transferring where they get appended to an mTransferredPorts array and exposed to the caller.

Pushed by sfink@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/af672c9161d7
retain ordering of transferable objects r=jonco,smaug
Status: ASSIGNED → RESOLVED
Closed: 3 years ago
Resolution: --- → FIXED
Target Milestone: --- → 95 Branch
Blocks: 1738750
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: