Skip to content

Sort the keys of Mirror wire JSON so a repeated message has the same bytes - #1182

Merged
onevcat merged 1 commit into
mainfrom
fix/mirror-wire-stable-key-order
Nov 28, 2026
Merged

onevcat merged 1 commit into
mainfrom
fix/mirror-wire-stable-key-order

Conversation

@onevcat

@onevcat onevcat commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Problem

MirrorWireCompressionTests/aStreamRoundTripsEveryPayloadTypeAndShrinksRepeats() fails in CI from time to time. The last failure was run 45992640420, with the sizes [349, 54, 64, 52, 18] against the limit < 64.

MirrorWire.payload used a JSONEncoder without sorted keys. The encoder takes its key order from a dictionary, and that order can change between two calls for the same value in one process. A small program that encoded one value 2000 times, with other allocations between the calls, got 4 different outputs.

When the key order changes, a repeated pane list does not have the same bytes as the one before it. The compressed stream can then refer back to only a part of it, and the repeat is a few bytes larger.

This is not only a test problem: the Mac host compresses the messages that it sends, so the size of a repeated list on the wire also changed from one send to the next.

Change

The wire encoder sorts its keys. The clients parse the JSON and do not depend on the key order. The iOS and Android clients do not compress the messages that they send, so they are not changed.

Verification

  • New test aJSONPayloadHasOneKeyOrder failed before the change and passes after it.
  • make test (5180 tests), make check, make build-app

…bytes

JSONEncoder takes its key order from a dictionary, and that order can change
between two calls for the same value. A repeated pane list then had different
bytes, so the compressed stream could not refer back to all of it. The size
of a repeat changed from run to run, and
`aStreamRoundTripsEveryPayloadTypeAndShrinksRepeats` failed in CI when a
repeat came to 64 bytes.
@onevcat
onevcat merged commit f303d05 into main Nov 28, 2026
2 checks passed
@onevcat
onevcat deleted the fix/mirror-wire-stable-key-order branch November 28, 2026 00:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant