Skip to content

Commit

Permalink
Update ensure_connected for websockets (#2486)
Browse files Browse the repository at this point in the history
* Updates `ensure_connected` for the change to websockets.

* Regenerates the websocket connection if it is closed.
  • Loading branch information
thewhaleking authored Nov 27, 2024
1 parent 059a05d commit aa9e71b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def _get_substrate(self):
"""Establishes a connection to the Substrate node using configured parameters."""
try:
# Set up params.
if not self.websocket:
if self.websocket is None or self.websocket.close_code is not None:
self.websocket = ws_client.connect(
self.chain_endpoint,
open_timeout=self._connection_timeout,
Expand Down
4 changes: 2 additions & 2 deletions bittensor/utils/networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import netaddr
import requests
from retry import retry
from websocket import WebSocketConnectionClosedException
from websockets.exceptions import ConnectionClosed

from bittensor.utils.btlogging import logging

Expand Down Expand Up @@ -193,7 +193,7 @@ def wrapper(self, *args, **kwargs):

try:
return func(self, *args, **kwargs)
except WebSocketConnectionClosedException:
except ConnectionClosed:
logging.console.warning(
"WebSocket connection closed. Attempting to reconnect 5 times..."
)
Expand Down
2 changes: 2 additions & 0 deletions tests/helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ def get_mock_neuron_by_uid(uid: int, **kwargs) -> NeuronInfo:


class FakeWebsocket(ClientConnection):
close_code = None

def __init__(self, *args, seed, **kwargs):
protocol = ClientProtocol(parse_uri("ws://127.0.0.1:9945"))
super().__init__(socket=None, protocol=protocol, **kwargs)
Expand Down

0 comments on commit aa9e71b

Please sign in to comment.