Skip to content

Commit

Permalink
Add exception handling for the CreatePlayerAsync in LavalinkPlayerHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
SKProCH committed Sep 1, 2024
1 parent a3407b1 commit 7a489bc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
25 changes: 19 additions & 6 deletions src/Lavalink4NET/Players/LavalinkPlayerHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,25 @@ private async ValueTask CompleteAsync(bool isVoiceServerUpdated, CancellationTok
if (_value is TaskCompletionSource<ILavalinkPlayer> taskCompletionSource)
{
// _value is automatically set by CreatePlayerAsync
var player = await CreatePlayerAsync(cancellationToken).ConfigureAwait(false);

taskCompletionSource.TrySetResult(player);

Interlocked.Decrement(ref Diagnostics.PendingHandles);
Interlocked.Increment(ref Diagnostics.ActivePlayers);
try
{
// CreatePlayerAsync can throw if request to lavalink fails
// We should handle this to avoid never completed lavalink player handle
var player = await CreatePlayerAsync(cancellationToken).ConfigureAwait(false);

taskCompletionSource.TrySetResult(player);

Interlocked.Decrement(ref Diagnostics.PendingHandles);
Interlocked.Increment(ref Diagnostics.ActivePlayers);
}
catch (Exception e)
{
// Here we're passing CancellationToken.None to ensure what the player disposing event will properly
// clean up this handle from cache
await _playerContext.LifecycleNotifier!.NotifyDisposeAsync(_guildId, CancellationToken.None);
taskCompletionSource.TrySetException(e);
await DisposeAsync();
}
}
else
{
Expand Down
5 changes: 2 additions & 3 deletions src/Lavalink4NET/Players/PlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,10 @@ async ValueTask IPlayerLifecycleNotifier.NotifyDisposeAsync(ulong guildId, Cance

await using var _ = playerHandle.ConfigureAwait(false);

Debug.Assert(playerHandle.Player is not null);
Debug.Assert(playerHandle.Player is { State: PlayerState.Destroyed, });

// Player can be null if lavalink node will throw while creating player
if (playerHandle.Player is not null)
{
Debug.Assert(playerHandle.Player is { State: PlayerState.Destroyed, });
var eventArgs = new PlayerDestroyedEventArgs(playerHandle.Player);

await PlayerDestroyed
Expand Down

0 comments on commit 7a489bc

Please sign in to comment.