zeromq / netmq

A 100% native C# implementation of ZeroMQ for .NET
Other
2.97k stars 745 forks source link

MQ Server crashes on FaultException #1101

Open WojakGra opened 3 months ago

WojakGra commented 3 months ago

Environment

NetMQ Version: 4.0.1.13
Operating System: Windows 10 Pro x64
.NET Version: 4.7.2

Explanation

I have tasks with a loop, where in this loop I use the function 'NetMQ_SendFrame', which sends information by converting object to JSON string. The server crashes with error down below

Code

Server

private void NetMQ_Start()
{
    mqserver = new PublisherSocket();
    mqserver.Bind("tcp://127.0.0.1:5555");

    poller = new NetMQPoller { mqserver };
    poller.RunAsync();
}
private void NetMQ_SendFrame(PLCController controller, string msg)
{
    try
    {
        mqserver.SendFrame(msg);
    }
    catch (NetMQ.FaultException e)
    {
        LogEvent($"{controller.ID} - {controller.Name}: NetMQ SendFrame Fault Error");
    }
    catch (Exception e)
    {
        LogEvent($"{controller.ID} - {controller.Name}: NetMQ SendFrame Error");
    }
}

Client

private void InitializeNetMQ()
{
    mqclient = new SubscriberSocket();
    mqclient.Connect("tcp://127.0.0.1:5555");
    mqclient.Subscribe("");
    mqclient.ReceiveReady += Subscriber_ReceiveReady;

    poller = new NetMQPoller { mqclient };
    poller.RunAsync();
}
private void Subscriber_ReceiveReady(object sender, NetMQSocketEventArgs e)
{
    string message = e.Socket.ReceiveFrameString();
    if (message != null || message != "")
    {
        UpdatePLCList(message);
    }
}

Model of message

public class TemplateSendPLCList
{
    public string MachineID {  get; set; }
    public string MachineName { get; set; }
    public string Type { get; set; }
    public string IP { get; set; }
    public bool IsRunning { get; set; }
    public string Status { get; set; }
    public DateTime MessageTime { get; set; }
    public object readData { get; set; }
    public object writeData { get; set; }
    public object resultData { get; set; }
}

Error

Unhandled exception: NetMQ.FaultException
   at NetMQ.Msg.Close()
   at NetMQ.Core.Transports.EncoderBase.Encode(NetMQ.Core.Transports.ByteArraySegment ByRef, Int32)
   at NetMQ.Core.Transports.StreamEngine.BeginSending()
   at NetMQ.Core.Transports.StreamEngine.Handle(Action, System.Net.Sockets.SocketError, Int32)
   at NetMQ.Core.Transports.StreamEngine.FeedAction(Action, System.Net.Sockets.SocketError, Int32)
   at NetMQ.Core.Transports.StreamEngine.ActivateOut()
   at NetMQ.Core.SessionBase.ReadActivated(NetMQ.Core.Pipe)
   at NetMQ.Core.Pipe.ProcessActivateRead()
   at NetMQ.Core.ZObject.ProcessCommand(NetMQ.Core.Command)
   at NetMQ.Core.IOThread.Ready()
   at NetMQ.Core.Utils.Proactor.Loop()
   at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
   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()
amosialek commented 2 months ago

I'm Experiencing the same issue in old codebase after switching from ZeroMQ nuget to NetMQ. Issue happens from time to time. Is there option to catch these exceptions just to avoid crash?

WojakGra commented 2 months ago

I tried to implement exceptions but decided to get diffrent approach. I made a class for queue that sends the message with timer inside in Task.

WojakGra commented 2 months ago

In this way I got rid of the problem, where there was a possibility that two messages wanted to send at the same time and an error was shown