Repository navigation
fix(api): refuse to start a second trading engine (BUG-04) - #23
Merged
Merged
Conversation
`POST /api/trading/start` built and started a new engine unconditionally.
`_live_engine` was only ever overwritten:
engine = LiveTradingEngine(...)
engine.start()
_live_engine = engine # replaces, never checks
A second call therefore orphaned the first engine:
- its TradingScheduler thread and its broker kept running
- no handle remained, so `POST /api/trading/stop` could never reach it
- both engines wrote positions / P&L / orders into the same store, and
`positions` is keyed on `code` alone, so their rows overwrote each other
- its kill switch became unreachable too, because `_core_risk` was rebound
`LiveTradingEngine.start()` has its own `if self._running: return` guard, but
that is per-instance and cannot see the new object.
Realistic trigger: a double-clicked button, two browser tabs, or a script --
anything that sends the request twice before the first response returns.
Now returns 409 with an explicit message, and the guard keys on the engine's
`_running` flag rather than on the global merely being set, so a stopped engine
does not block a restart.
Tests (tests/test_api/test_trading_start_guard.py):
- a second start while one is running -> 409
- a stopped engine does not block a restart
- repeated attempts stay refused
Verified by removing the guard: the first and third fail there, the second
still passes (it is the one asserting the guard does not over-block).
1326 passed, 1 skipped.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
P0/P1 — trading safety.
POST /api/trading/startbuilt and started a new engine unconditionally;_live_enginewas only ever overwritten:What a second call used to do
TradingSchedulerthread and broker kept runningPOST /api/trading/stopcould never reach itpositionsis keyed oncodealone, so their rows overwrote each other_core_riskwas reboundLiveTradingEngine.start()does haveif self._running: return, but that is per-instance and cannot see the newly constructed object.Realistic trigger: a double-clicked button, two browser tabs, or a script — anything that sends the request twice before the first response returns.
Fix
Returns 409 with an explicit message. The guard keys on the engine's
_runningflag rather than on the global merely being set, so a stopped engine does not block a restart.Tests
test_a_second_start_is_refused_while_one_is_runningtest_a_stopped_engine_does_not_block_a_restarttest_the_orphaned_engine_is_the_one_the_guard_protectsVerified by removing the guard: tests 1 and 3 fail there, test 2 still passes — which is the point, it is the one asserting the guard does not over-block.
1326 passed, 1 skipped.🤖 Generated with Claude Code