erdewit / ib_insync

Python sync/async framework for Interactive Brokers API
BSD 2-Clause "Simplified" License
2.75k stars 723 forks source link

client does not seem to disconnect #76

Closed 33327 closed 5 years ago

33327 commented 5 years ago

ERROR:ib_insync.client:Peer closed connection ERROR:ib_insync.client:clientId 1 already in use? ERROR:ib_insync.client:API connection failed: TimeoutError()

Hi I am testing via jupyter notebook and also having this issue when executing orders via python script.

For unknown reason after I connect and send a request for info or an order. API client connection stays on even if I send ib.disconnect() it will not disconnect.

Each time i tried to use same client ID i cant connect and no way to force close it unless IB GW is repopen.

What could be the problem? Thanks

ERROR:asyncio:_GatheringFuture exception was never retrieved future: <_GatheringFuture finished exception=CancelledError()> concurrent.futures._base.CancelledError ERROR:ib_insync.client:Peer closed connection ERROR:ib_insync.client:clientId 1 already in use? ERROR:ib_insync.client:API connection failed: TimeoutError()

erdewit commented 5 years ago

Perhaps the notebook doesn't call util.startLoop() first?

33327 commented 5 years ago

How is it supposed to be working by design of IB GW? If a connection with a client id has been established and no disconnect command issued, but network connection was lost. Will it allow to reconnect with same client id?

Does IB GW close connections itself on a network disconnect by a client?

radekwlsk commented 5 years ago

@erdewit perhaps not, as I am not using Jupyter Notebook and for me clients also do not disconnect sometimes, I think it appeared in one of the last 2-3 updates. Before no problems with that.

radekwlsk commented 5 years ago

@erdewit today I have found how to force disconnect.

Calling IB.disconnect() causes IB.isConnected() to return True when in IB Gateway the client is still visible as connected. Only after calling IB.disconnect() and then IB.waitOnUpdate(timeout=0.1) or any other small value so it just times out it finally disconnects from IB Gateway. Seems like inside Python everything is set as it was disconnected but the Wrapper is somehow not notified? I don't know the internals of API exactly but connection is not really closed, without no easy way to verify that fact as every variable used for that indeed returns that client is disconnected.

erdewit commented 5 years ago

@radekwlsk Thanks for your observation. I've added one iteration of the event loop after client.disconnect() to let it finish, in case there is no event loop running in between doing a disconnect and an immediate reconnect.