Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/project_x_py/trading_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,17 +982,22 @@ async def disconnect(self) -> None:
"""
logger.info("Disconnecting TradingSuite...")

# Disconnect realtime FIRST so the SignalR background reader thread is
# quiesced before any Polars data frames it writes into are freed. The
# previous order (cleanup contexts -> disconnect realtime) freed the
# frames while the reader was still live, racing native SignalR threads
# against Polars memory and corrupting the heap on disconnect
# (Windows STATUS_HEAP_CORRUPTION / 0xc0000374). See issue #98.
if self.realtime:
await self.realtime.disconnect()

if self._instruments:
# Multi-instrument mode - disconnect all contexts
await self._disconnect_instrument_contexts()
else:
# Legacy single-instrument mode
await self._disconnect_legacy_single_instrument()

# Disconnect realtime
if self.realtime:
await self.realtime.disconnect()

# Clean up client context
if hasattr(self, "_client_context") and self._client_context:
try:
Expand Down