Skip to content

Commit

Permalink
Fix some Node close cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
EvieePy committed Feb 17, 2024
1 parent 90b1da8 commit af792e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
22 changes: 18 additions & 4 deletions wavelink/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,19 +273,30 @@ async def _pool_closer(self) -> None:
if not self._has_closed:
await self.close()

async def close(self) -> None:
async def close(self, eject: bool = False) -> None:
"""Method to close this Node and cleanup.
After this method has finished, the event ``on_wavelink_node_closed`` will be fired.
This method renders the Node websocket disconnected and disconnects all players.
Parameters
----------
eject: bool
If ``True``, this will remove the Node from the Pool. Defaults to ``False``.
.. versionchanged:: 3.2.1
Added the ``eject`` parameter. Fixed a bug where the connected Players were not being disconnected.
"""
disconnected: list[Player] = []

for player in self._players.values():
for player in self._players.copy().values():
try:
await player._destroy()
except LavalinkException:
await player.disconnect()
except Exception as e:
logger.debug("An error occured while disconnecting a player in the close method of %r: %s", self, e)
pass

disconnected.append(player)
Expand All @@ -299,6 +310,9 @@ async def close(self) -> None:

self._has_closed = True

if eject:
getattr(Pool, "_Pool__nodes").pop(self.identifier, None)

# Dispatch Node Closed Event... node, list of disconnected players
if self.client is not None:
self.client.dispatch("wavelink_node_closed", self, disconnected)
Expand Down
8 changes: 4 additions & 4 deletions wavelink/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,15 @@ def dispatch(self, event: str, /, *args: Any, **kwargs: Any) -> None:
logger.debug(f"{self.node!r} dispatched the event 'on_wavelink_{event}'")

async def cleanup(self) -> None:
if self.socket:
if self.keep_alive_task:
try:
await self.socket.close()
self.keep_alive_task.cancel()
except:
pass

if self.keep_alive_task:
if self.socket:
try:
self.keep_alive_task.cancel()
await self.socket.close()
except:
pass

Expand Down

0 comments on commit af792e2

Please sign in to comment.