RestClient NullReferenceException on disposal
Created by: PhotonBursted
Summary
When the RestClient is being disposed without having executed a request, a NullReferenceException is thrown.
Details
DSharpPlus: v4.0.0-RC1 .NET Framework: .NET Core 3.1 OS: Debian 9 (stretch) 64-bit IDE: JetBrains Rider 2020.2.1 Exception:
System.NullReferenceException: Object reference not set to an instance of an object.
at DSharpPlus.Net.RestClient.Dispose()
at DSharpPlus.Net.RestClient.Finalize()
Steps to reproduce
- Create a new DiscordClient instance (with valid configuration) Make sure to not execute any calls that make the RestClient send/receive buckets
- Make the instance dispose, either by calling Dispose() or using
using
statements or the like
Notes
I came across this issue while unit testing. Some of the functionalities I've built directly use the DiscordClient's public events and data models. For these unit tests, obviously, I am not allowing the bot to actually connect to anything, instead relying on reflection and mocks to create controlled environments to test in.
From what I could see, the issue arises because there's a token source referenced here which is only set once a bucket is fetched.
To confirm my suspicions, I did try calling a bucket fetching method which doesn't require logins (like DiscordClient.ListVoiceRegionsAsync()
) right after the client object's creation, which successfully prevented the exception on disposal.