Skip to content

Fix: cap aggtrade pagination to prevent candle-hole-fill hang#167

Draft
Dhiyaahaq33 wants to merge 2 commits into
cunarist:mainfrom
Dhiyaahaq33:fix-candle-hole-fill-hang
Draft

Fix: cap aggtrade pagination to prevent candle-hole-fill hang#167
Dhiyaahaq33 wants to merge 2 commits into
cunarist:mainfrom
Dhiyaahaq33:fix-candle-hole-fill-hang

Conversation

@Dhiyaahaq33

Copy link
Copy Markdown

Summary

  • Collector._fetch_aggtrades_for_gap paginates Binance's /fapi/v1/aggTrades (max 1000 trades/page) in an unbounded while loop until the whole 10-second window is covered.
  • For a symbol with very high trade volume, a single 10-second window can contain far more than 1000 trades, so this loop can make hundreds of sequential API round trips before returning.
  • _fill_candle_data_holes runs as a cron job every 10 seconds with APScheduler's default max_instances=1. While one call is stuck deep in that pagination loop, every subsequent scheduled run is skipped with "maximum number of running instances reached" — exactly the log pattern reported in Filling recent 24 hours of candle data sometimes hangs #140, which happened "when there was a symbol that has [a massive] amount of aggtrade data... in the past 24 hours."
  • Fix: cap pagination at a new MAX_AGGTRADE_PAGES_PER_GAP (50) pages per gap-fill call. The caller (_find_full_symbols / _fill_symbol_holes) already treats a partially-filled gap as progress (did_fill) and re-queues the symbol on the next cron tick, so this doesn't drop data — it just spreads an extreme-volume symbol's backlog across multiple cron ticks instead of blocking one tick indefinitely.

Fixes #140

Test plan

  • ruff check on changed files passes
  • python -m py_compile on changed files passes
  • Reproducing the original hang requires a live data folder against a real high-volume Binance symbol backlog, which I don't have set up locally — happy to test further if a maintainer can point me at a good way to simulate it (e.g. a fixture with a large synthetic aggtrade response).

🤖 Generated with Claude Code

https://claude.ai/code/session_01WJ386AEgV8Z796N4TSJtPn

Dhiyaahaq33 and others added 2 commits July 19, 2026 11:29
_save_calculation_results wrote asset_record, unrealized_changes,
scribbles, and account_state, but never virtual_state.pickle, despite
a comment claiming it was intentionally skipped. _load_or_create_previous_state
unconditionally tries to read all five pickle files inside one try
block, so the missing virtual_state file raised FileNotFoundError on
every subsequent run, which discarded the other four successfully-saved
pieces of state too and reset the calculation to start from scratch.

Fixes cunarist#146

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WJ386AEgV8Z796N4TSJtPn
…scheduler

_fetch_aggtrades_for_gap paginated Binance's /fapi/v1/aggTrades (max 1000
trades per page) in an unbounded while loop until it covered a single
10-second window. For a symbol with a very high trade volume, a single
10-second window can contain far more than 1000 trades, so this loop could
make hundreds of sequential API round trips before returning.

Collector._fill_candle_data_holes runs as an APScheduler cron job every 10
seconds with the default max_instances=1, so while one call was stuck deep in
this pagination loop, every subsequent scheduled run got skipped with
"maximum number of running instances reached" -- exactly the symptom
reported in cunarist#140.

Cap the pagination at MAX_AGGTRADE_PAGES_PER_GAP (50) pages per gap. The
caller already treats a partially-filled gap as progress and re-queues the
symbol on the next cron tick (see Collector._find_full_symbols /
_fill_symbol_holes), so capping here doesn't lose data -- it just spreads
filling an extreme-volume symbol's backlog across multiple cron ticks
instead of blocking one tick indefinitely.

Fixes cunarist#140

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WJ386AEgV8Z796N4TSJtPn
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.

Filling recent 24 hours of candle data sometimes hangs

1 participant