diff --git a/piker/tsp/_history.py b/piker/tsp/_history.py index 54cbb3b4..d416c679 100644 --- a/piker/tsp/_history.py +++ b/piker/tsp/_history.py @@ -577,6 +577,25 @@ async def start_backfill( f'{next_start_dt} -> {last_start_dt}' ) + # Check if we're about to exceed buffer capacity BEFORE + # attempting the push + if next_prepend_index - ln < 0: + log.warning( + f'Backfill would exceed buffer capacity!\n' + f'next_prepend_index: {next_prepend_index}\n' + f'frame size: {ln}\n' + f'Truncating to fit remaining space..\n' + ) + # only push what fits + to_push = to_push[-(next_prepend_index):] + ln = len(to_push) + + if ln == 0: + log.warning( + 'No space left in buffer, stopping backfill!' + ) + break + # bail gracefully on shm allocation overrun/full # condition try: @@ -597,6 +616,14 @@ async def start_backfill( next_prepend_index = next_prepend_index - ln last_start_dt = next_start_dt + # Stop if we've hit buffer start + if next_prepend_index <= 0: + log.warning( + f'Reached buffer start (index={next_prepend_index}), ' + f'stopping backfill' + ) + break + except ValueError as ve: _ve = ve log.error(