Skip to content

Commit aa9e71b

Browse files
authored
Update ensure_connected for websockets (#2486)
* Updates `ensure_connected` for the change to websockets. * Regenerates the websocket connection if it is closed.
1 parent 059a05d commit aa9e71b

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

bittensor/core/subtensor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def _get_substrate(self):
216216
"""Establishes a connection to the Substrate node using configured parameters."""
217217
try:
218218
# Set up params.
219-
if not self.websocket:
219+
if self.websocket is None or self.websocket.close_code is not None:
220220
self.websocket = ws_client.connect(
221221
self.chain_endpoint,
222222
open_timeout=self._connection_timeout,

bittensor/utils/networking.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import netaddr
1111
import requests
1212
from retry import retry
13-
from websocket import WebSocketConnectionClosedException
13+
from websockets.exceptions import ConnectionClosed
1414

1515
from bittensor.utils.btlogging import logging
1616

@@ -193,7 +193,7 @@ def wrapper(self, *args, **kwargs):
193193

194194
try:
195195
return func(self, *args, **kwargs)
196-
except WebSocketConnectionClosedException:
196+
except ConnectionClosed:
197197
logging.console.warning(
198198
"WebSocket connection closed. Attempting to reconnect 5 times..."
199199
)

tests/helpers/helpers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ def get_mock_neuron_by_uid(uid: int, **kwargs) -> NeuronInfo:
125125

126126

127127
class FakeWebsocket(ClientConnection):
128+
close_code = None
129+
128130
def __init__(self, *args, seed, **kwargs):
129131
protocol = ClientProtocol(parse_uri("ws://127.0.0.1:9945"))
130132
super().__init__(socket=None, protocol=protocol, **kwargs)

0 commit comments

Comments
 (0)