zeromq / netmq

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

Dealer instance creation or attaching it to a pooler is hanging #1002

Open AnudeepGarge opened 2 years ago

AnudeepGarge commented 2 years ago

Environment

NetMQ Version:    4.0.1.6
Operating System: Windows 10
.NET Version:     4.7.2

Expected behaviour

Dealer must not hang in any circumstances.

Actual behaviour

Dealer hangs intermittently

Steps to reproduce the behaviour

The below code when used in a foreground thread hangs intermittently. I could not reproduce it all the time but I could see the log pattern where "Creating..." entered but ("Created" did not appear.

main () { logger.Log("Creating...") NetMQSocket NetMqSocket = CreateDealer() Pooler = new NetMQPoller { NetMqSocket };

            NetMqSocket.ReceiveReady += OnReady;
            Pooler.RunAsync();

logger.Log("Created") }

private static NetMQSocket CreateDealerSocket() { var zmqSocket = new DealerSocket(); zmqSocket.Options.Identity = Encoding.Unicode.GetBytes(Guid.NewGuid().ToString());

        return zmqSocket;
    }
dxdjgl commented 1 year ago

I am not able to recreate the problem with the code you have described. And it also seems very unlikely that it would ever hang. However since your code would stop immediately after startup, it is quite likely that the logging framework had not yet processed the last log entry before shutdown. I have altered your code to make it compile, and using the console output it work like a charm always. See code snippet below, which will actually compile.

using System.Text; using NetMQ; using NetMQ.Sockets;

class Issue1002 { static void Main() { Console.WriteLine("Creating..."); NetMQSocket NetMqSocket = CreateDealer();

    var Pooler = new NetMQPoller { NetMqSocket };

    NetMqSocket.ReceiveReady += OnReady;
    Pooler.RunAsync();
    Console.WriteLine("Created");
    Pooler.Dispose();
}

static NetMQSocket CreateDealer()
{
    var zmqSocket = new DealerSocket();
    zmqSocket.Options.Identity = Encoding.Unicode.GetBytes(Guid.NewGuid().ToString());

    return zmqSocket;
}

static void OnReady(object? sender, NetMQSocketEventArgs e)
{
    Console.WriteLine("on ready");
}

}