Skip to content

make the channel response table thread safe #26

@HaraldWikeroy

Description

@HaraldWikeroy

Occasionally, I would get exceptions when assigning new events to the _bindings dictionary in Channel.cs .
With the attached code, the error would appear after approx. 1000 pushes.

These exceptions happen because the socket replies are received on another thread. The _bindings dictionary is not thread safe so adds and removes crash.

So, in Channel.cs, change the type of SubscriptionTable to ConcurrentDictionary:
using SubscriptionTable = System.Collections.Concurrent.ConcurrentDictionary<string, System.Collections.Generic.List<Phoenix.ChannelSubscription>>;

and use TryRemove on _bindings:
public bool Off(string anyEvent) => _bindings.TryRemove(anyEvent, out _);

I've tested the updated code with tens of thousands of pushes and it doesn't crash now:

Snag_173ceda8

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions