Fatal CLR Error when using VoiceNext

Created by: FruitBoi

Make sure you familiarize yourself with our contributing guidelines.

Summary

Randomly, while using VoiceNext, the application will error with "Fatal error. Internal CLR error. (0x80131506)" and close, with a stacktrace pointing to DSharpPlus.VoiceNext.VoiceNextConnection.ProcessPacket(System.ReadOnlySpan 1, System.Memory1<Byte> ByRef, System.Memory1 ByRef, System.Colle ctions.Generic.IList1<System.ReadOnlyMemory1>, DSharpPlus.VoiceNext.Enti ties.AudioSender ByRef, DSharpPlus.VoiceNext.AudioFormat ByRef)`

Details

I am using DSharpPlus 4.1.0-nightly-00921 and VoiceNext 4.1.0-nightly-00921, on .NET Core 3.1, running on Windows Server 2012 R2.

Fatal error. Internal CLR error. (0x80131506)
   at DSharpPlus.VoiceNext.VoiceNextConnection.ProcessPacket(System.ReadOnlySpan
`1<Byte>, System.Memory`1<Byte> ByRef, System.Memory`1<Byte> ByRef, System.Colle
ctions.Generic.IList`1<System.ReadOnlyMemory`1<Byte>>, DSharpPlus.VoiceNext.Enti
ties.AudioSender ByRef, DSharpPlus.VoiceNext.AudioFormat ByRef)
   at DSharpPlus.VoiceNext.VoiceNextConnection+<ProcessVoicePacket>d__209.MoveNe
xt()
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[DSharpPlus.V
oiceNext.VoiceNextConnection+<ProcessVoicePacket>d__209, DSharpPlus.VoiceNext, V
ersion=4.1.0.921, Culture=neutral, PublicKeyToken=null]](<ProcessVoicePacket>d__
209 ByRef)
   at DSharpPlus.VoiceNext.VoiceNextConnection.ProcessVoicePacket(Byte[])
   at DSharpPlus.VoiceNext.VoiceNextConnection+<UdpReceiverTask>d__211.MoveNext(
)
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[DSharpPlus.V
oiceNext.VoiceNextConnection+<UdpReceiverTask>d__211, DSharpPlus.VoiceNext, Vers
ion=4.1.0.921, Culture=neutral, PublicKeyToken=null]](<UdpReceiverTask>d__211 By
Ref)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[[DSharpPlus.V
oiceNext.VoiceNextConnection+<UdpReceiverTask>d__211, DSharpPlus.VoiceNext, Vers
ion=4.1.0.921, Culture=neutral, PublicKeyToken=null]](<UdpReceiverTask>d__211 By
Ref)
   at DSharpPlus.VoiceNext.VoiceNextConnection.UdpReceiverTask()
   at System.Threading.Tasks.Task`1[[System.__Canon, System.Private.CoreLib, Ver
sion=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].InnerInvoke()
   at System.Threading.Tasks.Task+<>c.<.cctor>b__274_0(System.Object)
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(System.Thr
eading.Thread, System.Threading.ExecutionContext, System.Threading.ContextCallba
ck, System.Object)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.
Task ByRef, System.Threading.Thread)
   at System.Threading.Tasks.Task.ExecuteEntryUnsafe(System.Threading.Thread)
   at System.Threading.Tasks.Task.ExecuteFromThreadPool(System.Threading.Thread)

   at System.Threading.ThreadPoolWorkQueue.Dispatch()

Steps to reproduce

  1. Send and receive audio using Voice Next
  2. Wait for error to occur

Notes

In here you can put additional notes (such as what you tried) about the issue, or maybe some self-diagnosis.

This problem also occurred on .NET Framework 4.7.2 and .NET Framework 4.8, I read online that the 0x80131506 code was fixed in .Net Core, so I tried that to no avail. If I had to guess, there's something going on when we're calling into unmanaged code for the opus decoding, perhaps corrupting the memory. I have also attached a .Net Debugger to the application, and the debugger points to the line where we convert the byte array into a Span<byte> right after we call audioFormat.SampleCountToSampleSize(); in ProcessPacket.