Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocketTimeoutException #219

Open
dmlinke opened this issue Jun 1, 2024 · 4 comments
Open

WebSocketTimeoutException #219

dmlinke opened this issue Jun 1, 2024 · 4 comments

Comments

@dmlinke
Copy link

dmlinke commented Jun 1, 2024

Hey there, I am using the Kline subscribe example (Python) from the quickstart examples:
https://github.com/bybit-exchange/pybit/blob/master/examples/websocket_example_quickstart.py

from pybit.unified_trading import WebSocket
from time import sleep

ws = WebSocket(
    testnet=True,
    channel_type="linear",
)

def handle_message(message):
    print(message)

ws.orderbook_stream(50, "BTCUSDT", handle_message)

while True:
    sleep(1)

I get the following error:
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pybit/_websocket_stream.py", line 177, in _connect
raise websocket.WebSocketTimeoutException(
websocket._exceptions.WebSocketTimeoutException: WebSocket Unified V5 (wss://stream.bybit.com/v5/public/linear) connection failed. Too many connection attempts. pybit will no longer try to reconnect.

Any help on this error would be appreciated. I am on Mac. Python 3.11.5. pybit 5.7.0.

@issacnumb
Copy link

Same problem on CentOS

@Cacti2333
Copy link

Cacti2333 commented Aug 27, 2024

Same problem on Ubuntu 22.04

def subscribe(symbol):
    ws = WebSocket(
            testnet=False,
            channel_type="linear",
            trace_logging=True,
        )
    def handle_socket_message(msg):
        print(msg)
    
    ws.orderbook_stream(depth=1, symbol=symbol, callback=handle_socket_message)
    while True:
            time.sleep(0.1)
++Rcv raw: b'\x81~\x00\xc0{"topic":"orderbook.50.BTCUSDT","type":"delta","ts":1724761127354,"data":{"s":"BTCUSDT","b":[],"a":[["62414.30","0.304"],["62414.80","0"]],"u":87208563,"seq":228619353153},"cts":1724761127352}'
++Rcv raw: b'\x81~\x00\xc0{"topic":"orderbook.50.BTCUSDT","type":"delta","ts":1724761127354,"data":{"s":"BTCUSDT","b":[],"a":[["62414.30","0.304"],["62414.80","0"]],"u":87208563,"seq":228619353153},"cts":1724761127352}'
++Rcv raw: b'\x81~\x00\xc0{"topic":"orderbook.50.BTCUSDT","type":"delta","ts":1724761127354,"data":{"s":"BTCUSDT","b":[],"a":[["62414.30","0.304"],["62414.80","0"]],"u":87208563,"seq":228619353153},"cts":1724761127352}'
--- request header ---
++Rcv decoded: fin=1 opcode=1 data=b'{"topic":"orderbook.50.BTCUSDT","type":"delta","ts":1724761127354,"data":{"s":"BTCUSDT","b":[],"a":[["62414.30","0.304"],["62414.80","0"]],"u":87208563,"seq":228619353153},"cts":1724761127352}'
++Rcv decoded: fin=1 opcode=1 data=b'{"topic":"orderbook.50.BTCUSDT","type":"delta","ts":1724761127354,"data":{"s":"BTCUSDT","b":[],"a":[["62414.30","0.304"],["62414.80","0"]],"u":87208563,"seq":228619353153},"cts":1724761127352}'
++Rcv decoded: fin=1 opcode=1 data=b'{"topic":"orderbook.50.BTCUSDT","type":"delta","ts":1724761127354,"data":{"s":"BTCUSDT","b":[],"a":[["62414.30","0.304"],["62414.80","0"]],"u":87208563,"seq":228619353153},"cts":1724761127352}'
Sending ping
GET /v5/public/linear HTTP/1.1
Upgrade: websocket
Host: stream.bybit.com
Origin: https://stream.bybit.com
Sec-WebSocket-Key: NzAkM1lx3qfl61NmDyi87w==
Sec-WebSocket-Version: 13
Connection: Upgrade


Sending ping
++Rcv raw: b'\x81~\x01\xa3{"topic":"orderbook.50.BTCUSDT","type":"delta","ts":1724761127434,"data":{"s":"BTCUSDT","b":[["62409.50","12.620"],["62403.20","0.066"],["62403.00","0.001"]],"a":[["62409.60","7.831"],["62414.60","0.112"],["62414.70","0.016"],["62414.80","0.112"],["62415.10","0.113"],["62415.20","0.112"],["62415.30","0.238"],["62415.40","0.116"],["62419.40","0"],["62419.50","0"]],"u":87208564,"seq":228619353255},"cts":1724761127429}'
++Rcv raw: b'\x81~\x01\xa3{"topic":"orderbook.50.BTCUSDT","type":"delta","ts":1724761127434,"data":{"s":"BTCUSDT","b":[["62409.50","12.620"],["62403.20","0.066"],["62403.00","0.001"]],"a":[["62409.60","7.831"],["62414.60","0.112"],["62414.70","0.016"],["62414.80","0.112"],["62415.10","0.113"],["62415.20","0.112"],["62415.30","0.238"],["62415.40","0.116"],["62419.40","0"],["62419.50","0"]],"u":87208564,"seq":228619353255},"cts":1724761127429}'
++Rcv raw: b'\x81~\x01\xa3{"topic":"orderbook.50.BTCUSDT","type":"delta","ts":1724761127434,"data":{"s":"BTCUSDT","b":[["62409.50","12.620"],["62403.20","0.066"],["62403.00","0.001"]],"a":[["62409.60","7.831"],["62414.60","0.112"],["62414.70","0.016"],["62414.80","0.112"],["62415.10","0.113"],["62415.20","0.112"],["62415.30","0.238"],["62415.40","0.116"],["62419.40","0"],["62419.50","0"]],"u":87208564,"seq":228619353255},"cts":1724761127429}'
++Sent raw: b'\x89\x80nKz*'
-----------------------
Sending ping
++Sent raw: b'\x89\x80T\xf3\x08!'
++Rcv decoded: fin=1 opcode=1 data=b'{"topic":"orderbook.50.BTCUSDT","type":"delta","ts":1724761127434,"data":{"s":"BTCUSDT","b":[["62409.50","12.620"],["62403.20","0.066"],["62403.00","0.001"]],"a":[["62409.60","7.831"],["62414.60","0.112"],["62414.70","0.016"],["62414.80","0.112"],["62415.10","0.113"],["62415.20","0.112"],["62415.30","0.238"],["62415.40","0.116"],["62419.40","0"],["62419.50","0"]],"u":87208564,"seq":228619353255},"cts":1724761127429}'
++Rcv decoded: fin=1 opcode=1 data=b'{"topic":"orderbook.50.BTCUSDT","type":"delta","ts":1724761127434,"data":{"s":"BTCUSDT","b":[["62409.50","12.620"],["62403.20","0.066"],["62403.00","0.001"]],"a":[["62409.60","7.831"],["62414.60","0.112"],["62414.70","0.016"],["62414.80","0.112"],["62415.10","0.113"],["62415.20","0.112"],["62415.30","0.238"],["62415.40","0.116"],["62419.40","0"],["62419.50","0"]],"u":87208564,"seq":228619353255},"cts":1724761127429}'
++Rcv decoded: fin=1 opcode=1 data=b'{"topic":"orderbook.50.BTCUSDT","type":"delta","ts":1724761127434,"data":{"s":"BTCUSDT","b":[["62409.50","12.620"],["62403.20","0.066"],["62403.00","0.001"]],"a":[["62409.60","7.831"],["62414.60","0.112"],["62414.70","0.016"],["62414.80","0.112"],["62415.10","0.113"],["62415.20","0.112"],["62415.30","0.238"],["62415.40","0.116"],["62419.40","0"],["62419.50","0"]],"u":87208564,"seq":228619353255},"cts":1724761127429}'
WebSocket Unified V5 (wss://stream.bybit.com/v5/public/linear) encountered error: ping/pong timed out.
++Sent decoded: fin=1 opcode=9 data=b''
WebSocket Unified V5 (wss://stream.bybit.com/v5/public/linear) encountered error: ping/pong timed out.
--- response header ---
++Sent raw: b'\x89\x80\x93\x83\xee\xbb'
++Sent decoded: fin=1 opcode=9 data=b''
++Rcv raw: b'\x81~\x01a{"topic":"orderbook.50.BTCUSDT","type":"delta","ts":1724761127454,"data":{"s":"BTCUSDT","b":[],"a":[["62409.60","10.461"],["62414.60","0"],["62414.80","0"],["62415.10","0.001"],["62415.20","0"],["62415.30","0.126"],["62415.40","0.004"],["62417.10","0.016"],["62419.40","0.007"],["62419.50","0.151"]],"u":87208565,"seq":228619353276},"cts":1724761127450}'
++Sent raw: b'\x88\x82 \xdf\x99\xac#7'
++Sent raw: b'\x88\x82\xf7q>i\xf4\x99'
++Sent raw: b'\x88\x82\xe7\x11\x18\xb9\xe4\xf9'
++Sent raw: b'\x88\x82Q\xf1\xc1\xc3R\x19'
HTTP/1.1 101 Switching Protocols
++Sent decoded: fin=1 opcode=9 data=b''

Setting trace_logging=False makes it worst:

def subscribe(symbol):
    ws = WebSocket(
            testnet=False,
            channel_type="linear",
            trace_logging=False,
        )
    def handle_socket_message(msg):
        print(msg)
    
    ws.orderbook_stream(depth=1, symbol=symbol, callback=handle_socket_message)
    while True:
            time.sleep(0.1)
Exception in thread Thread-6:
Traceback (most recent call last):
  File "/usr/lib/python3.12/threading.py", line 1075, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.12/threading.py", line 1433, in run
    self.function(*self.args, **self.kwargs)
  File "~/.venv/lib/python3.12/site-packages/pybit/_websocket_stream.py", line 258, in _send_custom_ping
    self.ws.send(self.custom_ping_message)
  File "~/.venv/lib/python3.12/site-packages/websocket/_app.py", line 291, in send
    raise WebSocketConnectionClosedException("Connection is already closed.")
websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.

@dextertd
Copy link
Collaborator

I'm unable to reproduce this. The only thing I can suggest is that it is down to network problems. This may be an unsatisfactory answer but as I can't reproduce it...

Try running the same code with the same pybit version on a headless server, or if already doing that with another provider/location, and see if you still see the same results with that different network setting.

@Cacti2333
Copy link

Dear @dextertd

I narrowed it down to two issues.
1- If one of the provided pairs are not listed
2- If for some reason an exception happens

In both of these cases, the socket will be closed unexpectedly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants