Skip to content

fix: get_latest_at() returned wrong data where has_latest_at() was true - #676

Open
louisbels wants to merge 1 commit into
foxssake:mainfrom
louisbels:fix/history-buffer-evicted-gap
Open

louisbels wants to merge 1 commit into
foxssake:mainfrom
louisbels:fix/history-buffer-evicted-gap

Conversation

@louisbels

Copy link
Copy Markdown
Contributor

We got this on a client while it was merging a state from the host:

Invalid call. Nonexistent function 'duplicate' in base 'Nil'.

It comes from _PerObjectHistory.ensure_snapshot(), which checks has_latest_at(tick) and then duplicates get_latest_at(tick). In _HistoryBuffer the two can disagree. A tick with no data points to the last tick written before it, and when that one is pushed out of the buffer, the empty ticks after it stay in with nothing to point to.

With the default capacity of 64, set_at(54, ...) then set_at(118, ...) is enough: 118 takes the slot of 54, and for ticks 55 to 117 has_latest_at() is true while get_latest_at() returns null. A plain push() can get there too.

For us it was most likely a client that stalled for about two seconds while loading, then received a state for one of those ticks.

The fix moves the tail forward to the next tick that has data whenever it advances, so those empty ticks are no longer part of the buffer. pop() gets the same treatment, there the empty ticks still returned the popped item. I added a test for set_at(), push() and pop(), and one that does random writes and checks that every tick with a latest returns it. Tests pass on 4.2.2 and 4.7.

…s true

A gap points back to the last item before it. Once that item leaves the
buffer the tail could land inside the gap, so has_latest_at() was true
there while get_latest_at() read the item's old slot, which holds either
the tick that reused it or the evicted item.

Move the tail on to the next item whenever it advances. push() only
checks when the overflow moved the tail.
@louisbels
louisbels force-pushed the fix/history-buffer-evicted-gap branch from 7770488 to a50fcdb Compare November 26, 2026 22:24
@louisbels louisbels changed the title fix: get_latest_at() returned null where has_latest_at() was true fix: get_latest_at() returned wrong data where has_latest_at() was true Nov 26, 2026

This branch has not been deployed

No deployments
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