IB().connect() always fails the first time upon a fresh IBG/TWS start
See original GitHub issueNoticed that IB().connect() always fails with asyncio.exceptions.TimeoutError
the very first time it is called upon a fresh IBG/TWS start / re-start. The error occurs with and without providing timeout in connect.
The second time on it works without an issue.
Error traceback
Traceback (most recent call last):
File "c:\Users\User\Documents\Business\Projects\learn\ib\main.py", line 142, in <module>
all_base(MARKET, ip)
File "c:\Users\User\Documents\Business\Projects\learn\ib\base.py", line 555, in all_base
with IB().connect(HOST, PORT, CID, timeout=2) as ib:
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\ib_insync\ib.py", line 260, in connect
return self._run(self.connectAsync(
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\ib_insync\ib.py", line 292, in _run
return util.run(*awaitables, timeout=self.RequestTimeout)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\ib_insync\util.py", line 311, in run
result = loop.run_until_complete(task)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 612, in run_until_complete
return future.result()
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\ib_insync\ib.py", line 1619, in connectAsync
await asyncio.wait_for(connect(), timeout or None)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\asyncio\tasks.py", line 490, in wait_for
raise exceptions.TimeoutError()
asyncio.exceptions.TimeoutError
Probable causes:
Investigating the traceback:
-
It looks like it is caused by _run in
ib.py
, which somehow misses the *awaitables in def run ofutil.py
that gets into run_until_complete ofbase_events.py
and falls through to line:612 (unreachable by pylance) to return future.result() -
ib.py
seems to take this result forward totasks.py
to raise the exceptions.TimeoutError(). There is a comment intasks.py
pointing to a python bug, that seems to have been abandoned in May 2018. However, on August 27, there seems to have been some changes made in the code base to address fut.cancel.
Queries
- Does this happen to others too?
- Is there something that can be done to address this in ib_insync?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:22 (7 by maintainers)
Top GitHub Comments
@jkleckner thanks for the debug log. What is remarkable about it is that IBKR is not sending back the
accountDownloadEnd
marker anymore. There should be aline but there isn’t. This leaves the
reqAccountUpdates
request hanging, which then times out. On the insync user group it is mentioned that specifying an account, as inib.connect(account=XXXXXXX
), fixes the issues. Could it be that the issue only manifests for advisor accounts?What I can do is to not automatically request account updates when there are multiple accounts.
@drusakov778 that’s 100% correct, even the regular
ib.connect()
method does not use nesting.This was failing for me the past few days even running the basic ib_insync examples. I checked and TWS had updated to 980.4l and I think that was when the problem started. I downgraded TWS to 978.2g and now everything works properly again. So it would seem that the latest version of TWS has changed something that broke how ib_insync connects.