PINK: cancel_async, S2 task guard, 29 new regression tests — 346/346 green
Bug fixes:
1. bingx_venue.py: add cancel_async() — async cancel that awaits backend.cancel()
directly in the main event loop. The sync cancel() path goes through _run()
→ thread-pool → asyncio.run() in a new thread, but aiohttp is bound to the
main loop → deadlock. Identical root cause as the old sync submit() → fixed
via submit_async. Remove dead cancel_order branch (BingxDirectExecutionAdapter
has cancel, not cancel_order).
2. rust_backend.py: process_intent_async CANCEL path now uses cancel_async when
available (matching the submit_async pattern for ENTER/EXIT). Sync cancel()
fallback kept for MockVenueAdapter compat.
3. bingx_direct.py: guard S2 background refresh task per symbol. Old code discarded
the task reference; rapid submits piled up concurrent _refresh_state_background
calls all writing self._state in arbitrary completion order (stale last-writer-
wins). Now: skip creating a new task if one is already pending for the symbol;
store reference and clear via done-callback.
Test additions (test_bingx_bugs.py, 29 tests):
- cancel_async: awaitable, calls backend.cancel directly, maps all statuses
- process_intent_async CANCEL: dispatches cancel_async / falls back to sync
- S2 guard: task stored, no duplicates while pending, new task after done
- _events_from_submit with None snapshots: FILLED/NEW/REJECTED/PARTIAL/RATE_LIMITED
- _filled_size_from_snapshots(None, None): safe 0.0 return
- _events_from_cancel: before/after completely ignored
- connect(): no double refresh_state, no-op if backend has no connect
- submit() sync with None snapshots: FULL_FILL still emitted
- cancel() branch audit: uses cancel not cancel_order, raises for no-cancel backend
Fix: test_exchange_event_seam_parity.py TestMockSubscribe — replace deprecated
asyncio.get_event_loop().run_until_complete() with asyncio.run() (Python 3.12
raises RuntimeError when event loop is closed by earlier suite tests).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -969,13 +969,23 @@ class ExecutionKernel:
|
||||
evt_outcome = self.on_venue_event(event)
|
||||
all_venue_transitions.extend(evt_outcome.transitions)
|
||||
elif intent.action == KernelCommandType.CANCEL:
|
||||
# Use cancel_async if available (avoids thread-pool deadlock — same
|
||||
# issue that required submit_async for ENTER/EXIT). Fall back to
|
||||
# sync cancel() for mock/test venues that don't expose cancel_async.
|
||||
slot_view = self.slot(intent.slot_id)
|
||||
cancel_async = getattr(self.venue, "cancel_async", None)
|
||||
order_to_cancel = None
|
||||
if slot_view.active_exit_order is not None:
|
||||
emitted_events = self.venue.cancel(slot_view.active_exit_order, reason=intent.reason)
|
||||
order_to_cancel = slot_view.active_exit_order
|
||||
elif slot_view.active_entry_order is not None and slot_view.fsm_state in {
|
||||
TradeStage.ENTRY_WORKING, TradeStage.ORDER_REQUESTED, TradeStage.ORDER_SENT, TradeStage.IDLE,
|
||||
}:
|
||||
emitted_events = self.venue.cancel(slot_view.active_entry_order, reason=intent.reason)
|
||||
order_to_cancel = slot_view.active_entry_order
|
||||
if order_to_cancel is not None:
|
||||
if cancel_async is not None:
|
||||
emitted_events = await cancel_async(order_to_cancel, reason=intent.reason)
|
||||
else:
|
||||
emitted_events = self.venue.cancel(order_to_cancel, reason=intent.reason)
|
||||
else:
|
||||
emitted_events = []
|
||||
for event in emitted_events:
|
||||
|
||||
Reference in New Issue
Block a user