Skip to content

Commit aef1666

Browse files
authored
fix: Fix race condition when reconnecting (#1090)
* feat: Utilise reconnection lock for packet sending. * chore: bump version * fix: Fix race condition when reconnecting and identifying/resuming using the shared reconnect lock.
1 parent b6bac06 commit aef1666

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

interactions/api/gateway/client.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -961,16 +961,25 @@ async def _send_packet(self, data: Dict[str, Any]) -> None:
961961
packet: str = _data.decode("utf-8") if isinstance(_data, bytes) else _data
962962
log.debug(packet)
963963

964-
async with self.reconnect_lock: # needs to lock while it reconnects.
964+
if data["op"] in {OpCodeType.IDENTIFY.value, OpCodeType.RESUME.value}:
965+
# This can't use the reconnect lock *because* its already referenced in
966+
# self._reconnect(), hence an infinite hang.
965967

966-
if data["op"] != OpCodeType.HEARTBEAT.value:
967-
# This is because the ratelimiter limits already accounts for this.
968-
await self._ratelimiter.block()
969-
970-
if self._client is not None: # this mitigates against another edge case.
968+
if self._client is not None:
971969
self._last_send = perf_counter()
972970

973971
await self._client.send_str(packet)
972+
else:
973+
async with self.reconnect_lock: # needs to lock while it reconnects.
974+
975+
if data["op"] != OpCodeType.HEARTBEAT.value:
976+
# This is because the ratelimiter limits already accounts for this.
977+
await self._ratelimiter.block()
978+
979+
if self._client is not None: # this mitigates against another edge case.
980+
self._last_send = perf_counter()
981+
982+
await self._client.send_str(packet)
974983

975984
async def __identify(
976985
self, shard: Optional[List[Tuple[int]]] = None, presence: Optional[ClientPresence] = None

0 commit comments

Comments
 (0)