Skip to content

Commit 2d08c0c

Browse files
committed
Cleanup disconnection logic to reset all state
If you disconnect and reconnect, the connection should act as a new connection without any legacy metadata from the previous session. Also the disconnection now returns the status string of connection/API/transfer details from the active session.
1 parent daacef1 commit 2d08c0c

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

ib_async/ib.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,26 +379,34 @@ def connect(
379379
)
380380
)
381381

382-
def disconnect(self):
382+
def disconnect(self) -> str | None:
383383
"""
384384
Disconnect from a TWS or IB gateway application.
385385
This will clear all session state.
386386
"""
387387
if not self.client.isConnected():
388-
return
388+
return None
389389

390390
stats = self.client.connectionStats()
391-
self._logger.info(
391+
392+
status = (
392393
f"Disconnecting from {self.client.host}:{self.client.port}, "
393394
f"{util.formatSI(stats.numBytesSent)}B sent "
394395
f"in {stats.numMsgSent} messages, "
395396
f"{util.formatSI(stats.numBytesRecv)}B received "
396397
f"in {stats.numMsgRecv} messages, "
397398
f"session time {util.formatSI(stats.duration)}s."
398399
)
400+
401+
self._logger.info(status)
399402
self.client.disconnect()
400403
self.disconnectedEvent.emit()
401404

405+
# clear ALL internal state from this connection
406+
self.wrapper.reset()
407+
408+
return status
409+
402410
def isConnected(self) -> bool:
403411
"""Is there an API connection to TWS or IB gateway?"""
404412
return self.client.isReady()

0 commit comments

Comments
 (0)