Skip to content

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

@kuishou68

Description

@kuishou68

Bug Description

MovingAverage.reset() in livekit-agents/livekit/agents/utils/moving_average.py resets _count and _sum to zero but does not clear the _hist ring-buffer. This causes stale values from before the reset to corrupt the moving average once the window fills up again.

Root Cause

def reset(self) -> None:
    self._count = 0
    self._sum = 0
    # BUG: self._hist is not cleared!

After reset(), as add_sample() is called:

  • _count increments from 0, _sum is rebuilt correctly until the window is full.
  • Once _count > len(self._hist), the code subtracts self._hist[index] from _sum to evict the oldest entry. But _hist[index] still contains the pre-reset value, so the sum is incorrectly reduced by that stale value.

Fix

Clear _hist in reset():

def reset(self) -> None:
    self._hist = [0.0] * len(self._hist)
    self._count = 0
    self._sum = 0.0

Impact

MovingAverage is used in metrics tracking across the agents framework. Incorrect averages after a reset can lead to skewed performance metrics and incorrect adaptive behaviour.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions