Fixed connection being assumed to be open before first payload received on websocket.
@@ -588,12 +588,12 @@ class ShardConnection:
try:
self._ws = await self._session.ws_connect(**self._ws_connect_kwargs)
-
- self._connected_at = time.perf_counter()
self._zlib = zlib.decompressobj()
self.logger.debug("expecting HELLO")
pl = await self._receive()
+ self._connected_at = time.perf_counter()
+
op = pl["op"]
if op != 10:
raise errors.GatewayError(f"Expected HELLO opcode 10 but received {op}")
@@ -642,17 +642,19 @@ class ShardConnection:
raise ex
finally:
self.closed_event.set()
- self._connected_at = float("nan")
- self.last_heartbeat_sent = float("nan")
- self.heartbeat_latency = float("nan")
- self.last_message_received = float("nan")
- if self._ws is not None:
+ if not math.isnan(self._connected_at):
await self.close(close_code)
self.dispatch(self, "DISCONNECTED", {})
self.disconnect_count += 1
+
self._ws = None
+ self._connected_at = float("nan")
+ self.last_heartbeat_sent = float("nan")
+ self.heartbeat_latency = float("nan")
+ self.last_message_received = float("nan")
+
await self._session.close()
self._session = None
