question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IB().connect() always fails the first time upon a fresh IBG/TWS start

See original GitHub issue

Noticed 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:

  1. It looks like it is caused by _run in ib.py, which somehow misses the *awaitables in def run of util.py that gets into run_until_complete of base_events.py and falls through to line:612 (unreachable by pylance) to return future.result()

  2. ib.py seems to take this result forward to tasks.py to raise the exceptions.TimeoutError(). There is a comment in tasks.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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:22 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
erdewitcommented, Oct 20, 2020

@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 a

<<< 54,1,...

line 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 in ib.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.

2reactions
nahallcommented, Oct 6, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

socket programming connect() fails the second time I run the ...
I moved the connect() outside the for loop and it worked. But if I have multiple threads on the client side, should I...
Read more >
Disconnect a specific clientId - insync@groups.io
Though documentation says that ib.disconnect()...will clear all session state.... , I sometimes see something connected in API Client in IBG and ...
Read more >
Troubleshooting connection issues | Socket.IO
IO client will always try to reconnect, unless specifically told otherwise. Let's review how you can troubleshoot a connection failure.
Read more >
socket — Low-level networking interface — Python 3.11.1 ...
This module does not work or is not available on WebAssembly platforms ... This is a higher-level function than socket.connect() : if host...
Read more >
5 Socket Error: The connection timed-out" attempting to ...
If you are unable to connect via the Admin Client from a remote workstation, first verify that the MOVEit Central Service is started...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found