ibind icon indicating copy to clipboard operation
ibind copied to clipboard

need auto re-authentication when connection get broken

Open skkep opened this issue 9 months ago • 1 comments

2025-03-26 09:05:10,374|I| IbkrWsClient: on_close event while disconnected
2025-03-26 09:05:45,391|I| IbkrWsClient: Connect reattempt 4/10
2025-03-26 09:05:46,481|I| IbkrWsClient: Connection open
2025-03-26 09:05:56,552|E| IbkrWsClient: Reconnecting and recreating subscriptions stopped due to lack of authentication.
2025-03-26 09:06:06,593|E| IbkrWsClient: Reconnecting and recreating subscriptions stopped due to lack of authentication.
2025-03-26 09:06:16,637|E| IbkrWsClient: Reconnecting and recreating subscriptions stopped due to lack of authentication.

I wanted to test the stability of the WebSocket connection. I ran the test for 12 hours and found that the connection had already dropped without automatically reconnecting. This is crucial—once the connection is lost, it should automatically reconnect for stability. I also tested cases where the network was interrupted and then restored, but there were issues, such as exceptions being thrown. In such cases, it should automatically reconnect, and if authentication is required, it should use the stored credentials to re-authenticate. The connection needs to be reconnected in any case, and if it cannot be reconnected, the initialization starts automatically again

2025-03-26 09:03:14,949|I| IbkrWsClient: Connect reattempt 2/10
2025-03-26 09:03:25,129|W| Acquiring session cookie failed, connection to the Gateway may be broken.
2025-03-26 09:03:30,155|E| IbkrWsClient: New WebSocketApp connection timeout
2025-03-26 09:03:30,973|E| IbkrWsClient: Invalid ws returned: <websocket._app.WebSocketApp object at 0x00000118CDDAEA10> | expected: <websocket._app.WebSocketApp object at 0x00000118CF1C7950>
2025-03-26 09:03:30,973|E| IbkrWsClient: on_error: [SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1006)
2025-03-26 09:03:30,973|E| IbkrWsClient: Invalid ws returned: <websocket._app.WebSocketApp object at 0x00000118CDDAEA10> | expected: <websocket._app.WebSocketApp object at 0x00000118CF1C7950>
2025-03-26 09:03:30,973|I| IbkrWsClient: on_close
2025-03-26 09:03:30,973|I| IbkrWsClient: on_close event while disconnected
2025-03-26 09:04:19,540|E| Tickler error: 
  File "C:\ProgramData\Anaconda3\Lib\threading.py", line 995, in _bootstrap
    self._bootstrap_inner()
  File "C:\ProgramData\Anaconda3\Lib\threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "C:\ProgramData\Anaconda3\Lib\threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "C:\ProgramData\Anaconda3\Lib\site-packages\ibind\client\ibkr_utils.py", line 567, in _worker
    _LOGGER.error(f'Tickler error: {exception_to_string(e)}')
  File "C:\ProgramData\Anaconda3\Lib\site-packages\ibind\client\ibkr_utils.py", line 562, in _worker
    self._client.tickle()
  File "C:\ProgramData\Anaconda3\Lib\site-packages\ibind\client\ibkr_client_mixins\session_mixin.py", line 49, in tickle
    return self.post('tickle', log=False)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\Anaconda3\Lib\site-packages\ibind\base\rest_client.py", line 159, in post
    return self.request(method='POST', endpoint=path, base_url=base_url, extra_headers=extra_headers, json=params, log=log)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\Anaconda3\Lib\site-packages\ibind\base\rest_client.py", line 203, in request
    return self._request(method, endpoint, base_url, extra_headers, log, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\Anaconda3\Lib\site-packages\ibind\client\ibkr_client.py", line 129, in _request
    return super()._request(method, endpoint, base_url, extra_headers, log, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\Anaconda3\Lib\site-packages\ibind\base\rest_client.py", line 263, in _request
    raise ExternalBrokerError(f'{self}: request error: {str(e)}') from e
  <class 'ibind.support.errors.ExternalBrokerError'> IbkrClient: request error: HTTPSConnectionPool(host='api.ibkr.com', port=443): Max retries exceeded with url: /v1/api/tickle (Caused by ProxyError('Cannot connect to proxy.', TimeoutError('_ssl.c:989: The handshake operation timed out')))
The below exception was the direct cause of the above exception:
  File "C:\ProgramData\Anaconda3\Lib\site-packages\ibind\base\rest_client.py", line 238, in _request
    response = request_function(method, url, verify=self.cacert, headers=headers, timeout=self._timeout, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\Anaconda3\Lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\Anaconda3\Lib\site-packages\requests\sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\Anaconda3\Lib\site-packages\requests\adapters.py", line 513, in send
    raise ProxyError(e, request=request)
  <class 'requests.exceptions.ProxyError'> HTTPSConnectionPool(host='api.ibkr.com', port=443): Max retries exceeded with url: /v1/api/tickle (Caused by ProxyError('Cannot connect to proxy.', TimeoutError('_ssl.c:989: The handshake operation timed out')))

skkep avatar Mar 26 '25 01:03 skkep

hey @skkep many thanks for reporting this, that's useful to know.

A few points:

How are you authenticating? Gateway, IBeam or OAuth? A lot of what follows will depend on that.

2025-03-26 09:05:56,552|E| IbkrWsClient: Reconnecting and recreating subscriptions stopped due to lack of authentication.

This means that your session needs recreating, whichever means you are using. IbkrWsClient can only report this

I also tested cases where the network was interrupted and then restored, but there were issues, such as exceptions being thrown. In such cases, it should automatically reconnect,

That's all true, that's the design.

and if authentication is required, it should use the stored credentials to re-authenticate.

Again, that depends on the authentication method. If it's not OAuth there's nothing we can do, as authentication happens elsewhere.

Voyz avatar Mar 28 '25 08:03 Voyz

I'm going to close this due to inactivity. Thanks for the contribution so far, feel free to let me know if you'd like it reopen and continue the discussion 👍

Voyz avatar Jun 23 '25 11:06 Voyz