[ BUG ] System.NullReferenceException when using LavaPlayer.PlayAsync
Jaika1 opened this issue · 8 comments
Describe the bug/issue.
Please provide as many details as possible. Such as line number, file name, if the bug is reproducible, etc.
The bug appears to occur almost completely at random, but seems to be common with v5.2.1, and unavoidable with v5.2.2. When awaiting LavaPlayer.PlayAsync, playback seems to begin just fine when stepped through with breakpoints. When the scope of the calling function is left, however, music playback abruptly stops, along with a System.NullReferenceException (always the same stack-trace shown in the below section). The library will successfully reconnect with LavaLink and seemingly attempt to resume the session, but no audio will pass through (even when calling PlayAsync again) until the bot rejoins the Voice Channel. This seems to occur independently of the position of which track in the queue is played. I've currently downgraded to v5.2.0 and have yet to see this issue there. I'm currently using LavaLink v3.3.2.5.
A few examples of code that causes this exception are:
// Called by a Discord user, (parameter 'pos' is 1 indexed)
public async Task<Embed> JumpAsync(int pos, IGuild guild)
{
if (!_lavaNode.TryGetPlayer(guild, out LoopingLavaPlayer _player))
return new EmbedBuilder().WithDescription("Player isn't playing.").WithColor(Color.Red).Build();
if (_player.Queue.Count == 0)
return new EmbedBuilder().WithDescription("Queue is empty.").WithColor(Color.Red).Build();
if (pos < 1)
return new EmbedBuilder().WithDescription("Index can not be less than 1.").WithColor(Color.Red).Build();
if (pos > _player.Queue.Count)
return new EmbedBuilder().WithDescription("Provided index is larger than the queue!").WithColor(Color.Red).Build();
int realIndex = pos - 1;
LavaTrack jumpTo = _player.Queue.ElementAt(realIndex);
await _player.PlayAsync(jumpTo);
return new EmbedBuilder().WithDescription($"Jumped to track: [{jumpTo.Title}]({jumpTo.Url})").WithColor(Color.Purple).Build();
}
// Invoked by the LavaNode.OnTrackFinished event.
private async Task TrackFinished(TrackEndedEventArgs arg)
{
if (arg.Reason == TrackEndReason.Stopped || arg.Reason == TrackEndReason.Replaced)
return;
LoopingLavaPlayer loopPlayer = arg.Player as LoopingLavaPlayer;
//await loopPlayer.StopAsync();
//await Task.Delay(500);
int trackIndex = GetTrackIndex(loopPlayer, arg.Track);
if (trackIndex == -1 || (IsLastTrack(loopPlayer, trackIndex) && !loopPlayer.Loop))
{
await arg.Player.TextChannel.SendMessageAsync(embed: new EmbedBuilder().WithDescription($"Playback ended.").WithColor(Color.Purple).Build());
return;
}
trackIndex = IsLastTrack(loopPlayer, trackIndex) ? 0 : trackIndex + 1;
//LavaTrack nextTrack = loopPlayer.Queue.ElementAt(trackIndex);
await loopPlayer.PlayAsync(loopPlayer.Queue.ElementAt(trackIndex));
//await Task.Delay(500);
}
Stacktrace / Screenshots
When debugging yourself, you can attach screenshots of Autos/Locals tabs in Visual Studio.
- First 5 messages happen before leaving scope, music begins to play through bot
- Succeeding lines are after code execution is resumed with F5 and scope is left, where playback stops and can't be restored without reconnecting to the VC.
[09/01/2021 01:01 PM] Victoria : {"op":"event","reason":"FINISHED","type":"TrackEndEvent","track":"QAAAcQIABkVuY29yZQARVGVlIExvcGVzIC0gVG9waWMAAAAAAAHM8AALWEcweHEtVzhXX00AAQAraHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1YRzB4cS1XOFdfTQAHeW91dHViZQAAAAAAActM","guildId":"769514756465360929"}
[09/01/2021 01:02 PM] Gateway : Sent Heartbeat
[09/01/2021 01:02 PM] Victoria : {"op":"playerUpdate","state":{"position":0,"time":1630501322593},"guildId":"769514756465360929"}
[09/01/2021 01:02 PM] Gateway : Received HeartbeatAck
[09/01/2021 01:02 PM] Gateway : Latency = 219 ms
[09/01/2021 01:02 PM] Victoria : System.NullReferenceException: Object reference not set to an instance of an object.
at Victoria.LavaNode`1.DataAsyncAsync(Byte[] data) in Victoria.dll:token 0x600005c+0x799
at Victoria.LavaSocket.ReceiveAsync() in Victoria.dll:token 0x6000099+0x1cc
[09/01/2021 01:02 PM] Victoria : Lavalink reconnect attempt #1. Waiting 10s before next attempt.
[09/01/2021 01:02 PM] Victoria : Websocket connection established.
Victoria 5.1.11 works fine.
Victoria 5.1.11 works fine.
v5.2.0 seems to be fine on top of that as mentioned above, but this seems to be present past that. I'll currently be sticking with v5.2.0 myself, since there are some types I'm using that were introduced in that version.
Did you try 5.2.2 by any chance?
Did you try 5.2.2 by any chance?
Yes I did, which seemingly made the 2 provided methods always cause the issue in my experience.
Since, it is random, it's difficult to reproduce. If my suspicion is correct and having encountered this problem randomly few times, I imagine it must be a concurrency problem. I'll try few other things and provide an update in few days.
Can you please try 5.2.3 and see if the issue persists?
Can you please try 5.2.3 and see if the issue persists?
I've been playing with it for about an hour now, and I've seen no issues so far! I'll let you know in detail if that changes anytime in the future.
I haven't had this issue emerge since, seems safe to call this one closed for now! 👏