Skip to content

fix: clear _hist buffer in MovingAverage.reset() to prevent stale averages#5522

Merged
longcw merged 1 commit intolivekit:mainfrom
kuishou68:fix/issue-5521-moving-average-reset
Apr 22, 2026
Merged

fix: clear _hist buffer in MovingAverage.reset() to prevent stale averages#5522
longcw merged 1 commit intolivekit:mainfrom
kuishou68:fix/issue-5521-moving-average-reset

Conversation

@kuishou68
Copy link
Copy Markdown
Contributor

Closes #5521

Problem

MovingAverage.reset() resets _count and _sum to zero but does not clear the _hist ring-buffer. After a reset, once the window fills again, the eviction path subtracts stale pre-reset values from _sum, producing incorrect moving averages.

Fix

Clear _hist in reset() by replacing with a zero-initialized list of the same length.

Changes

  • livekit-agents/livekit/agents/utils/moving_average.py: added self._hist = [0.0] * len(self._hist) as first line of reset()

…ues (Closes livekit#5521)

Signed-off-by: Cocoon-Break <54054995+kuishou68@users.noreply.github.com>
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 22, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 1 additional finding.

Open in Devin Review

return self._sum / self.size()

def reset(self) -> None:
self._hist = [0.0] * len(self._hist)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the hist is used only when count > len(hist), that means all values in hist were overwritten already. but I think it's good to also reset it here.

@longcw
Copy link
Copy Markdown
Contributor

longcw commented Apr 22, 2026

could you sign the CLA so we can merge it?

@kuishou68
Copy link
Copy Markdown
Contributor Author

could you sign the CLA so we can merge it?

OK

@longcw longcw merged commit 9e7e877 into livekit:main Apr 22, 2026
18 checks passed
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.

Bug: MovingAverage.reset() does not clear internal history buffer, causing incorrect averages after reset

3 participants