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.

Connection raises TimeoutError, but IB gateway is running and port is open

See original GitHub issue

I only have one account and a telnet localhost 4002 gives me a shell as I would expect. Sadly I could not find a way to download and try an older version of the gateway. The current version for linux is 981.3c.

API connection failed: TimeoutError()
---------------------------------------------------------------------------
TimeoutError                              Traceback (most recent call last)
<ipython-input-10-13991e279071> in <module>
      1 con = ib.IB()
----> 2 con.connect('localhost', 4002, clientId=1)

~/.local/lib/python3.8/site-packages/ib_insync/ib.py in connect(self, host, port, clientId, timeout, readonly, account)
    269             account: Main account to receive updates for.
    270         """
--> 271         return self._run(self.connectAsync(
    272             host, port, clientId, timeout, readonly, account))
    273 

~/.local/lib/python3.8/site-packages/ib_insync/ib.py in _run(self, *awaitables)
    308 
    309     def _run(self, *awaitables: Awaitable):
--> 310         return util.run(*awaitables, timeout=self.RequestTimeout)
    311 
    312     def waitOnUpdate(self, timeout: float = 0) -> bool:

~/.local/lib/python3.8/site-packages/ib_insync/util.py in run(timeout, *awaitables)
    320         globalErrorEvent.connect(onError)
    321         try:
--> 322             result = loop.run_until_complete(task)
    323         except asyncio.CancelledError as e:
    324             raise globalErrorEvent.value() or e

~/.local/lib/python3.8/site-packages/nest_asyncio.py in run_until_complete(self, future)
     68                 raise RuntimeError(
     69                     'Event loop stopped before Future completed.')
---> 70             return f.result()
     71 
     72     def _run_once(self):

/usr/lib/python3.8/asyncio/futures.py in result(self)
    176         self.__log_traceback = False
    177         if self._exception is not None:
--> 178             raise self._exception
    179         return self._result
    180 

/usr/lib/python3.8/asyncio/tasks.py in __step(***failed resolving arguments***)
    278                 # We use the `send` method directly, because coroutines
    279                 # don't have `__iter__` and `__next__` methods.
--> 280                 result = coro.send(None)
    281             else:
    282                 result = coro.throw(exc)

~/.local/lib/python3.8/site-packages/ib_insync/ib.py in connectAsync(self, host, port, clientId, timeout, readonly, account)
   1624         try:
   1625             # establish API connection
-> 1626             await self.client.connectAsync(host, port, clientId, timeout)
   1627 
   1628             # autobind manual orders

~/.local/lib/python3.8/site-packages/ib_insync/client.py in connectAsync(self, host, port, clientId, timeout)
    216                 b' ' + self.connectOptions if self.connectOptions else b''))
    217             self.conn.sendMsg(msg)
--> 218             await asyncio.wait_for(self.apiStart, timeout)
    219             self._logger.info('API connection ready')
    220         except Exception as e:

/usr/lib/python3.8/asyncio/tasks.py in wait_for(fut, timeout, loop)
    499             # See https://bugs.python.org/issue32751
    500             await _cancel_and_wait(fut, loop=loop)
--> 501             raise exceptions.TimeoutError()
    502     finally:
    503         timeout_handle.cancel()

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:24 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
stopdesigncommented, Aug 2, 2021

Is there a socket connection in CLOSE_WAIT state during the issue?

I noticed that both TWS (on Mac) and IBGateway (in Docker) would set the socket to CLOSE_WAIT state when the connection was opened and wasn’t closed with the disconnect() method. It happens even if I use python socket connect to test if the port is open. Even if I close it explicitly.

import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect_ex(('127.0.0.1', 4001))
    s.close()

…and it is locked:

# lsof -i -P -n
COMMAND NODE  NAME
socat    TCP  *:4001 (LISTEN)
java     TCP  *:7462 (LISTEN)
java     TCP  127.0.0.1:4002->127.0.0.1:41652 (CLOSE_WAIT)              <<<<<<    THIS ONE
java     TCP  172.25.0.2:55792->64.190.197.40:4001 (ESTABLISHED)
java     TCP  172.25.0.2:37634->64.190.197.40:4000 (ESTABLISHED)
java     TCP  *:4002 (LISTEN)
java     TCP  172.25.0.2:37636->64.190.197.40:4000 (ESTABLISHED)
x11vnc   TCP  *:5900 (LISTEN)
x11vnc   TCP  *:5900 (LISTEN)
x11vnc   TCP  172.25.0.2:5900->172.25.0.1:51436 (ESTABLISHED)

After that I have to restart IBGateway / TWS or change the port in settings.

1reaction
erdewitcommented, Jan 3, 2022

It’s a Java process.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connection raises TimeoutError, but IB gateway is running ...
I only have one account and a telnet localhost 4002 gives me a shell as I would expect. Sadly I could not find...
Read more >
How To Disconnect An Existing Connection Of Ib Gateway ...
Connection raises TimeoutError but IB gateway is running and port is open.I only have one account and a telnet localhost 4002 gives me...
Read more >
insync@groups.io | Newbie: IB gateway problem
When I try to connect with logging on: >>> ib.connect(host="localhost", port=5000, clientId=1) 2020-04-08 03:13:20,819 ib_insync.client INFO ...
Read more >
TWS API v9.72+: Connectivity
Most commonly error 502 will indicate that TWS is not running with the API enabled, or it is listening for connections on a...
Read more >
Program cannot connect to interactive broker - Stack Overflow
I use the following class to handle IB connections: ... if not wait: raise TimeoutError('Could not connect to IB gateway') def ...
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