Repository navigation
fix(bots): refresh account balances once a bot is archived - #257
Closed
carlito-hummingbot wants to merge 3 commits into
Closed
carlito-hummingbot wants to merge 3 commits into
carlito-hummingbot wants to merge 3 commits into
Conversation
/portfolio/state serves an in-memory snapshot that only the background loop
refreshes, every account_update_interval minutes (default 5). Archiving a bot
frees the margin it was holding but touched nothing in that snapshot, so for up
to a loop interval the portfolio still reported those funds as committed - and
since `refresh` defaults to false, that is the figure a dashboard shows.
Measured on a live box, bitget_perpetual USDT:
while running: refresh=false 50.46641187 refresh=true 50.67436934
after archive: refresh=false 50.67436934 refresh=true 50.67436934
Before this change the post-archive unrefreshed read reported 50.46641187.
[Medium impact] The PR appears safe to merge, with no outstanding findings. SummaryRefreshes cached CEX balances after a bot is archived.
The previous endpoint-test finding is fixed: the archive remains the first queued task, with its original arguments. Changes since the previous review only reformat imports and introduce no new issues. Reviews (3) · Last reviewed commit: "style: apply the repo's pinned isort (5...." · Reviewed by Greptile |
The first version wrapped stop_and_archive_bot in a closure. That changed the identity of the scheduled task, so the test pinning that the endpoint hands the orchestrator the path parameter unmodified failed for a reason unrelated to what it guards. Starlette awaits background tasks in order, so the refresh is its own task after the archive instead: the archive call stays exactly as it was, and test_endpoint_schedules_the_background_task_with_the_path_name keeps its original assertions with the count adjusted to two.
pre-commit rewrote the imports in both files. Import formatting only.
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this fixes
POST /portfolio/stateserves an in-memory snapshot (accounts_service.get_accounts_state())that is only re-read by
update_account_state_loop, everyaccount_update_intervalminutes (default 5), or when a caller passesrefresh: true.refreshdefaults to false.stop-and-archive-botfrees the margin the bot was holding but touches nothingin that snapshot, so immediately after an archive the portfolio still reports
the archived bot's funds as committed — which is what a dashboard renders, and
it reads as "the archive didn't take".
Measured, on a live box
bitget_perpetual USDT, quad-threat's E4 bot deployed then archived through
POST /bot-orchestration/stop-and-archive-bot/{bot_name}:Before the fix the unrefreshed read kept the committed figure until the next
5-minute loop. After it, the cached row already agrees with the exchange.
The change
The route's background task is wrapped so that, once
stop_and_archive_bot(...)returns, it callsaccounts_service.update_account_state(skip_gateway=True).Best-effort by design: a failed balance refresh logs a warning and does not turn
a completed archive into an error.
skip_gateway=Truekeeps it to CEX balances,matching what the portfolio route does for a fast query.
Verification
Run the way CI runs it (
pytest -q -m "not integration"), in the repo'shummingbot-apiconda env, against the PR head:The one failure is pre-existing on
main, verified by running it in aworktree of
origin/mainwith the same image:It is a timing-sensitive backtest-supervisor test and is unrelated to this
change (which touches only
routers/bot_orchestration.py).Correction to an earlier revision of this description: it said this repo had
no test suite. That was wrong — it does, and the first version of this PR broke
one of its tests (
test_endpoint_schedules_the_background_task_with_the_path_name),which is what CI caught. The design was changed rather than the test being
adjusted away: the archive task is scheduled exactly as before and the refresh is
a second, ordered background task.
Why a second task rather than a wrapper
The first attempt wrapped
stop_and_archive_botin a closure that archived andthen refreshed. That changed the identity of the scheduled task, so a test
guarding "the endpoint hands the orchestrator the path parameter, unmodified"
failed — for a reason that had nothing to do with what it was guarding.
Starlette runs background tasks in order, awaiting each, so the refresh is
scheduled after the archive instead. The archive call is byte-identical to
main's, and the refresh is a separate unit with its own test(
test_a_failed_balance_refresh_does_not_fail_the_archive).