Skip to content

Commit

Permalink
Fix out of bounds error in the resampler
Browse files Browse the repository at this point in the history
When awaiting the resamplers to finish resampling, we need to make a
copy of the resamplers dict, because new resamplers could be added or
removed while we are awaiting, which would cause the results to be
out of sync.

Signed-off-by: Leandro Lucarella <[email protected]>
  • Loading branch information
llucax committed Nov 25, 2024
1 parent 448e6fc commit ba57eb1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

## Bug Fixes

<!-- Here goes notable bug fixes that are worth a special mention or explanation -->
- Fix a bug in the resampler that could end up with an *IndexError: list index out of range* exception when a new resampler was added while awaiting the existing resampler to finish resampling.
9 changes: 7 additions & 2 deletions src/frequenz/sdk/timeseries/_resampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,13 @@ async def resample(self, *, one_shot: bool = False) -> None:
self._config.resampling_period,
)

# We need to make a copy here because we need to match the results to the
# current resamplers, and since we await here, new resamplers could be added
# or removed from the dict while we awaiting the resampling, which would
# cause the results to be out of sync.
resamplers = self._resamplers.copy()
results = await asyncio.gather(
*[r.resample(self._window_end) for r in self._resamplers.values()],
*[r.resample(self._window_end) for r in resamplers.values()],
return_exceptions=True,
)

Expand All @@ -508,7 +513,7 @@ async def resample(self, *, one_shot: bool = False) -> None:
dict[Source, Exception | asyncio.CancelledError],
{
source: results[i]
for i, source in enumerate(self._resamplers)
for i, source in enumerate(resamplers)
# CancelledError inherits from BaseException, but we don't want
# to catch *all* BaseExceptions here.
if isinstance(results[i], (Exception, asyncio.CancelledError))
Expand Down

0 comments on commit ba57eb1

Please sign in to comment.