Fix NullReferenceException in Assets.Merge when Discord sends null assets #282
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a
NullReferenceExceptionthrown insideAssets.Merge(Assets other)when Discord sends presence updates where the assets payload isnullor missing.The bug does not prevent Rich Presence updates from being applied in the Discord client, but it does cause spurious error logs and can be confusing for users embedding the library in games or mods.
Problem
DiscordRpcClient.ProcessMessagecallsRichPresence.Mergeto keepCurrentPresencein sync with incoming presence updates from Discord:Inside
RichPresence.Merge, assets are merged like this (simplified):When Discord sends a presence update without assets,
presence.Assetscan benull. In that situation,Assets.Merge(presence.Assets)is invoked withother == null, butAssets.Mergedoes not guard against this and unconditionally dereferencesother:This leads to a
NullReferenceExceptionsimilar to:In practice:
This is especially visible in game/mod environments where logs are surfaced prominently.
Reproduction
One way to reproduce:
Assets(large/small images).Assets.Mergemay be called withother == null, triggering aNullReferenceExceptionin the RPC event loop.This can happen in real applications that dynamically toggle or clear presence images.
Fix
The fix is minimal and fully backwards compatible:
Assets.Merge(Assets other)to handleother == null.otheris null, the method now resets local asset fields and returns early, effectively treating it as “clear all assets”.This aligns with the idea that presence updates without assets should simply clear them instead of causing an exception.
Impact
other != nullis unchanged.NullReferenceExceptionto be logged.fx 4.5, which is important for engines like Unity, MonoGame, Godot, and game mods embedding the library.Testing
Built the library targeting
fx 4.5.Integrated the patched
DiscordRPC.dllinto a game mod environment.Verified:
NullReferenceExceptionin the logs.