-
-
Notifications
You must be signed in to change notification settings - Fork 785
IB().connect() always fails the first time upon a fresh IBG/TWS start #303
Comments
Starting today, I have a similar issue, in my case, I cannot open a second connection. |
If it is of any help, here is: A sample code:
Traceback from first time run:
Successful outcome from later runs (after the first time error):
|
some (potentially) related discussion is here: https://groups.io/g/insync/topic/77079866 |
Commit 74ca552 now gives an additional
|
I now don4't seem to be able to connect at all anymore, even with the abovementioned commit. I get the following error:
The snippet:
Anyone else facing this issue as well? |
I'm having this same issue. Running ib_insync 0.9.62. |
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. |
Hi all, I have not touched my IB gateway installation for months, and it appears to be version 972
the @reservoirinvest sample code from 5 days ago runs fine. So it does appear to be related to latest IB TWS/GW, which potentially may require some workaround/adaptation from ib_insync side systems: IB Gateway on AWS Ubuntu, python client connecting from mac os |
I've tried the latest version of TWS and IB Gateway (980.4l) and can't reproduce the connect issue (this is on Linux). If anyone with connect problems can post a debug log, that would show in more detail what happens. Before posting don't forget to remove any sensitive information! |
I am using latest as well (980.4n) I see a hang on this call: trades = ib.reqCompletedOrders(True) Also, it always says "reqCompletedOrders timed out" on each startup. |
Hmm seeing the same issue now. Peer Closed connection .. File "/usr/lib/python3.8/asyncio/tasks.py", line 490, in wait_for raise exceptions.TimeoutError() I get clientId 10 is alreay in use. Could be related to how the connection was not properly cleaned up. |
I could resolve the issue by adding the following hack:
Additional Notes
|
Sadly there is a risk to using this library unless server side changes run regression tests with it. I will say the code is quite fantastic though. Maybe we could convince IKBR to get some consulting time from @erdewit .. that would be very very helpful for all of us python devs. |
Probing versions on Windows 8.1, the error does not occur in TWS v980.4k but does occur in v980.4l and v980.4n. The error logs are completely unremarkable: |
Part of the problem here might be the nested way that asyncio is being used which contradicts guidance from the asyncio team. The custom run/sleep routines are non standard. So definitely is the patchAsyncio call in the code. See here - erdewit/nest_asyncio#36 The asyncio team is deprecating the loop parameter in many of the API calls, so I can imagine the situation will only get worse as further assumptions are made in the code. What version of python/asycnio is everyone using? There have been some changes recently (3.8 and 3.9). I am using python 3.8.5 that has been certified with focal ubuntu. I've been using the base TWS API + gevent with monkey patch, and so far so good. Though I do have some concerns about the magic of monkey patch. |
re nested asyncio - I've been using ib_insync for the last half a year and always without patching the asyncio, i.e. by calling the ib....Async() function variants, so I doubt the connection problems described about amount to patching the asyncio. The 'patch' itself is a mere technicality allowing unexperienced users to run pseudo-sync functions and having async underneath. Having said that it will be interesting to re-create and confirm/disprove the bug above in pure async setup. |
@jkleckner thanks for the debug log. What is remarkable about it is that IBKR is not sending back the
line but there isn't. This leaves the What I can do is to not automatically request account updates when there are multiple accounts. @drusakov778 that's 100% correct, even the regular |
Hmm, perhaps, though the util run / sleep do seem a bit risky. fwiw however, the connect(account=) fixed the issue for me. |
It is clearly introduced from 480.4k to 480.4l as it is the only thing changed so it seems likely that TWS is the cause of the problem. And reverting from l to k and back reliably demonstrates the problem. Strangely, the account= method does work but only for 1 of 5 possible accounts and it is not completely reliable. @erdewit If you have a patch I should test, I would be happy to try it. |
@jkleckner wrote:
That's interesting, I would have expected all the U* accounts to work. It means the regression in TWS is bigger than I thought. I will patch it in a way that bypasses all the issues. |
FYI, version 981.2b also fails with the original code. It works with the new master branch! Thanks! |
Thanks for confirming Jim. This is now released as v0.9.63. |
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
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
The text was updated successfully, but these errors were encountered: