bybit-exchange / pybit

Official Python3 API connector for Bybit's HTTP and WebSockets APIs.
Other
418 stars 135 forks source link

websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed. #246

Open panshuaiyin opened 2 months ago

panshuaiyin commented 2 months ago

when i run:

  symbol = option_info['symbol'].tolist()[0]
  bybit_ws = WebSocket(testnet=False,channel_type="option",)
  def handle_message(message):
      print(message)
  bybit_ws.orderbook_stream(depth=25,symbol=symbol,callback=handle_message)
  sleep(3)
  bybit_ws.exit()

it returns:

  {'topic': 'orderbook.25.BTC-13SEP24-58500-P', 'ts': 1725967410822, 'type': 'snapshot', 'id': 'orderbook.25.BTC-13SEP24-58500-P-26718245417-1725967410822', 'data': {'s': 'BTC-13SEP24-58500-P', 'b': [['1940', '28.71'], ['1525', '2'], ['5', '6']], 'a': [['2075', '1'], ['2080', '7.14'], ['2100', '21.57'], ['2495', '2']], 'u': 19527, 'seq': 26718245417}, 'cts': 1725967407642}
  Exception in thread Thread-14:
  Traceback (most recent call last):
    File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
      self.run()
    File "/usr/lib/python3.10/threading.py", line 1378, in run
      self.function(*self.args, **self.kwargs)
    File "/home/ubuntu/.local/lib/python3.10/site-packages/pybit/_websocket_stream.py", line 258, in _send_custom_ping
      self.ws.send(self.custom_ping_message)
    File "/home/ubuntu/.local/lib/python3.10/site-packages/websocket/_app.py", line 253, in send
      raise WebSocketConnectionClosedException(
  websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.

i'm confused. When bybit_ws.exit(), i think it should return nothing and exit successfully. But it still runs self.ws.send(self.custom_ping_message) and returns websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.

yzoz commented 2 months ago

I have the same! And this is very awkward, because the script works with orders and their cancellation.. I'm thinking of using systemd or some other autorestart thing. But I decided to consult with the developers: maybe this is a temporary phenomenon?

python3.12 - maybe that's the point?

FeastyPets commented 1 month ago

Yes, the socket is indeed not gracefully closed and you may get banned easily. Should be marked as severe issue with highest priority.