Skip to content

NullReferenceException in Presence.EnterMembersFromInternalPresenceMap when using Blazor #1299

@fernandomondo

Description

@fernandomondo

SDK Version
ably.io 1.2.15 (.NET)

Environment

  • Blazor Server (.NET 9)
  • Also reproducible in Blazor WebAssembly
  • Windows 10/11

Description
We are seeing a NullReferenceException inside the Ably .NET Realtime SDK when working with Presence.

The application code has been stable for months and started failing recently without any code changes.

Here is the stack trace:

Error Processing command: IO.Ably.Realtime.Workflow.RealtimeCommand[]
Error code: 50000
Reason: Unexpected error :Arg_NullReferenceException
[ErrorInfo Reason: Unexpected error :Arg_NullReferenceException (See https://help.ably.io/error/50000
); Code: 50000; Href: https://help.ably.io/error/50000
]

Inner exception:
Arg_NullReferenceException
at IO.Ably.Realtime.Presence.<>c__DisplayClass62_0.<EnterMembersFromInternalPresenceMap>b__1(Boolean success, ErrorInfo info)
at IO.Ably.Realtime.Presence.FailQueuedMessages(ErrorInfo reason)
at IO.Ably.Realtime.Presence.ChannelDetachedOrFailed(ErrorInfo error)
at IO.Ably.Realtime.RealtimeChannel.HandleStateChange(ChannelState state, ErrorInfo error, ProtocolMessage protocolMessage)
at IO.Ably.Realtime.RealtimeChannel.SetChannelState(ChannelState state, ErrorInfo error, ProtocolMessage protocolMessage, Boolean emitUpdate)
at IO.Ably.Realtime.ChannelMessageProcessor.MessageReceived(ProtocolMessage protocolMessage, RealtimeState state)
at IO.Ably.Realtime.Workflow.RealtimeWorkflow.<ProcessCommandInner>g__ProcessMessage|40_0(ProtocolMessage message)

Steps to reproduce
The issue is intermittent but happens after some reconnects or channel state changes.
Minimal reproduction flow:

  1. Create a Blazor component wrapping Ably Realtime.
  2. Attach to a channel.
  3. Call channel.Presence.Enter(PresenceMetadata) once the component renders.
  4. After some reconnects / detach events, Ably throws a NullReferenceException.

Relevant code excerpt

_channel = Ably.GetChannel(Channel);
_channel.Subscribe(OnMessage);

if (OnPresenceUpdated.HasDelegate || OnPresenceUpdatedNoCallback is not null)
    _channel.Presence.Subscribe(OnPresence);

if (NoPresence is false)
    _channel.Presence.Enter(PresenceMetadata);

_channel.Unsubscribe(OnMessage);
_channel.Presence.Unsubscribe(OnPresence);
if (NoPresence is false)
    _channel.Presence.Leave();
Ably.GetValue().ReleaseChannel(_channel);

Expected behavior
Presence operations should fail gracefully with an ErrorInfo if the channel is detached or failed.
They should not result in an unhandled NullReferenceException inside the SDK.

Additional context

This started failing without code changes.

Might be related to how presence messages are enqueued and then failed when a channel transitions to Detached or Failed.

We are using AuthCallback for token auth, Blazor Server, and multiple components can open/close channels with Presence.

Our code ensures Presence.Enter is called only once on initialization, not before attaching manually.

Please advise if this is a known issue in 1.2.15 or if there’s a workaround (e.g. delaying Presence.Enter until ChannelState.Attached).

┆Issue is synchronized with this Jira Task by Unito

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions