From 76f199df3bc930b3a22a048333fc6386bf5f358b Mon Sep 17 00:00:00 2001 From: goodboy Date: Tue, 27 Jan 2026 23:52:00 -0500 Subject: [PATCH] Add buffer capacity checks to backfill loop Prevent `ValueError` from negative prepend index in `start_backfill()` by checking buffer space before push attempts. Truncate incoming frame if needed and stop gracefully when buffer full. Also, - add pre-push capacity check with frame truncation logic - stop backfill when `next_prepend_index <= 0` - log warnings for capacity exceeded and buffer-full conditions (this patch was generated in some part by [`claude-code`][claude-code-gh]) [claude-code-gh]: https://github.com/anthropics/claude-code --- piker/tsp/_history.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) 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(