-
Notifications
You must be signed in to change notification settings - Fork 17
Concurrency issue with Subscriber sockets (m_subscriptions) #15
Description
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?