Skip to content

Fix time-sync median: sort clock offsets numerically#165

Open
xn101de wants to merge 1 commit into
snapcast:developfrom
xn101de:fix/timesync-median-numeric-sort
Open

Fix time-sync median: sort clock offsets numerically#165
xn101de wants to merge 1 commit into
snapcast:developfrom
xn101de:fix/timesync-median-numeric-sort

Conversation

@xn101de

@xn101de xn101de commented Jun 18, 2026

Copy link
Copy Markdown

TimeProvider.setDiff computes the median client/server clock offset to drive audio/video synchronization, but sorted the offset buffer with a bare Array.prototype.sort(). With no comparator, sort() coerces elements to strings and orders them lexicographically, so the "median" was taken from a wrongly ordered array (e.g. [2, 10, -5, 100] -> [-5, 10, 100, 2]).

The resulting offset is unstable and frequently wrong, corrupting every serverTime() calculation and causing playback drift and "Chunk too old, dropping" stalls.

Sort numerically with an explicit comparator.

TimeProvider.setDiff computes the median client/server clock offset to
drive audio/video synchronization, but sorted the offset buffer with a
bare Array.prototype.sort(). With no comparator, sort() coerces elements
to strings and orders them lexicographically, so the "median" was taken
from a wrongly ordered array (e.g. [2, 10, -5, 100] -> [-5, 10, 100, 2]).

The resulting offset is unstable and frequently wrong, corrupting every
serverTime() calculation and causing playback drift and "Chunk too old,
dropping" stalls.

Sort numerically with an explicit comparator.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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