Skip to content

ArraySchema: fix sort() in the same tick as another operation - #250

Closed
JROtto5 wants to merge 1 commit into
colyseus:masterfrom
JROtto5:fix/arrayschema-sort-same-tick
Closed

JROtto5 wants to merge 1 commit into
colyseus:masterfrom
JROtto5:fix/arrayschema-sort-same-tick

Conversation

@JROtto5

@JROtto5 JROtto5 commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Closes #249

sort() in the same patch as another operation on the same array left clients out of sync: ops staged earlier in the tick resolved against the re-sorted tmpItems, a clean-tick sort's refCount-neutral REPLACEs broke a later removal, and a non-deterministic comparator ordered items and tmpItems differently.

  • With pending changes, sort() falls back to CLEAR + re-ADD, like reverse() (aadcd9c).
  • A clean-tick sort is unchanged, and sorting again in the same tick stays in place; tmpItems is copied from the sorted items.
  • For Schema children, a removal after a sort in the same tick re-states the array first.

Encoder only, no new ops. On fallback and re-state ticks clients get onRemove/onAdd for every element, as with reverse(). 20 new tests (11 fail on master) and three self-verifying SDK fixtures; npm test and bench:gate pass.

endel added a commit that referenced this pull request Nov 5, 2026
@endel

endel commented Nov 5, 2026

Copy link
Copy Markdown
Member

Thanks @JROtto5, both for the report and for the fix. We ended up landing a fix directly on master (390cf38...f512336), released as 5.0.44, so I'm closing this in favor of it. Your paused-tracking guard and the three SDK fixtures are in it, with you as co-author, and the changelog credits you.

What's different from this PR:

  • A clean sort() sends only the slots that moved. On a 500-entry board with one score change, that's 20 bytes per tick instead of 2747.
  • sort() on a @view() array was sending REPLACE ops with a refId where clients read a slot index, even on a clean tick. That's fixed in encodeArray.
  • The CLEAR + re-ADD fallback only runs when the slot being touched actually moved. An index write + sort on a number array, or a pop() of an unmoved slot, no longer fires onRemove/onAdd for every element.

All 20 of your tests pass on master. The untracked-delete crash I hit along the way is now #251.

@endel endel closed this Nov 5, 2026
@JROtto5

JROtto5 commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @endel. Sending only the moved slots is a much better fix than the full re-send I went with, and I had missed the @view() REPLACE case entirely. Appreciate the co-author credit.

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.

ArraySchema: sort() in the same patch as another operation desyncs clients

2 participants