Search code examples
c#socketszeromqnetmq

ZMQ NetMQ - A non-blocking socket operation could not be completed


I have the following pattern :

Multiples threads are sending messages to a ConcurrentQueue that is polled by a single threaded Dealer in order to send messages to a Router.

The following exception is raised when multiples messages are sent :

"SocketException - A non-blocking socket operation could not be completed"

Here is the code of the thread that dequeues messages and send it to the dealer :

Task.Factory.StartNew((state) =>
        {
            using (NetMQSocket dealerSocket = new DealerSocket(_connectionString))
            using (NetMQPoller poller = new NetMQPoller() { dealerSocket })
            {
                dealerSocket.ReceiveReady += DealerSocketOnReceiveReady;
                poller.RunAsync();

                while (true)
                {
                    Message<T> message;
                    if (!_concurrentQueue.TryDequeue(out message)) continue;

                    _pendingRequests.Add(message.Id, message);
                    var mpm = new NetMQMessage(4);
                    mpm.AppendEmptyFrame();
                    mpm.Append(message.Body);
                    mpm.AppendEmptyFrame();
                    mpm.Append(message.Id.ToString());
                    dealerSocket.SendMultipartMessage(mpm);
                }
            }
        }, TaskCreationOptions.LongRunning, _cancellationTokenSource.Token);

The SocketException occured when sending the MultipartMessage

I have tried to increase the SendBuffer size and/or the SendHighWatermark size but I still have the same error.

Do I need to handle this exception and reset the socket or I should never be in this case?


Solution

  • The dealerSocket was used in 2 threads : main & poller. In order to use the dealerSocket in only one thread we used a NetMQQueue.

    More details here