Skip to content

Discord bot repeatedly disconnects with code -1 due to DSharpPlus NullReferenceException #108

Description

@pau1h

Description

Requestrr v2.1.10 was running in Docker but stopped responding to Discord commands. Radarr initially appeared unavailable as well, although that was a separate Docker networking configuration issue.

Environment

  • Requestrr: v2.1.10
  • Docker Compose
  • Bundled DSharpPlus version: 4.5.2
  • Radarr, Sonarr, and Requestrr share the same Docker network

Symptoms

Requestrr repeatedly logged:

[111 /ConnectionCl] [Crit ] Connection terminated (-1, ''), reconnecting

Disconnects often occurred approximately 41 seconds after connecting. Discord commands reached Requestrr, but responding to an interaction sometimes failed with:

DSharpPlus.Exceptions.NotFoundException: Not found: 404

Eventually, Requestrr exited and Docker restarted it.

Diagnostics performed

  • Radarr and Sonarr both returned HTTP 200 from inside the Requestrr container.
  • Requestrr's web interface returned HTTP 200.
  • DNS, routing, TLS, MTU, proxy settings, and container networking appeared healthy.
  • An independent Discord WebSocket client running on the same Docker network authenticated successfully and completed multiple heartbeat/ACK cycles.
  • Updating only DSharpPlus from 4.5.2 to 4.5.3 did not resolve the disconnect.
  • After subscribing to SocketErrored, the previously hidden underlying exception was exposed:
Socket handler suppressed an exception

System.NullReferenceException: Object reference not set to an instance of an object.
   at DSharpPlus.DiscordClient.HandleSocketMessageAsync(String data)
   at DSharpPlus.DiscordClient.<InternalConnectAsync>g__SocketOnMessage|484_1(
       IWebSocketClient sender,
       SocketMessageEventArgs e)

Apparent cause

Requestrr configures DSharpPlus with:

Intents = DiscordIntents.All

A gateway event received under that broad subscription appears to trigger a NullReferenceException inside DSharpPlus.

Requestrr does not subscribe to SocketErrored or ClientErrored, so the useful underlying exception is normally hidden.

Working workaround

Changing the configuration to request only the intent needed for guild/slash-command operation stopped the repeated disconnects during testing:

Intents = DiscordIntents.Guilds

I also added error handlers for diagnostic visibility:

_client.SocketErrored += OnSocketError;
_client.ClientErrored += OnClientError;

The DSharpPlus packages were updated to 4.5.3 as well. However, the package update alone did not solve the problem; limiting the gateway intents was the effective workaround.

After rebuilding:

  • No recurring Connection terminated (-1, '') messages
  • No container restarts
  • Discord remained connected across multiple heartbeat cycles
  • Radarr and Sonarr continued returning HTTP 200

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions