Skip to content

Concurrency issue with Subscriber sockets (m_subscriptions) #15

@wiz0u

Description

@wiz0u

Hello
I have a NetMQPoller thread, started by RunAsync(), polling on a SubscriberSocket, receiving messages.
On my Main Thread, I randomly call subscriber.Subscribe(...) on new topics

At some point, this will crash, usually with the following stack: (release build)

System.IndexOutOfRangeException
   at NetMQ.Core.Patterns.Utils.Trie.Check(Byte[], Int32, Int32)
   at NetMQ.Core.Patterns.XSub.XHasIn()
   at NetMQ.Core.SocketBase.GetSocketOption(NetMQ.Core.ZmqSocketOption)
   at NetMQ.Core.Utils.Selector.Select(NetMQ.Core.Utils.SelectItem[], Int32, Int64)
   at NetMQ.NetMQPoller.Run()
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ThreadHelper.ThreadStart()

As far as I analyzed NetMQ source code, it is due to the NetMQPollerThread trying to Check() the m_subscriptions Trie, while the main thread (going synchronously through Subscribe/SetSocketOption/XSetSocketOption/XSend) is trying to Add() a new topic to the m_subscriptions Trie.
Access to m_subscriptions seems not MT safe.

If this is by-design, what is the proper way to call Subscribe ?
Shouldn't the XSetSocketOption/XSend be marshalled via pipes through the Selector (or something like that) so it is handled in the same thread as NetMQPoller?

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