Search code examples
c#.netsocketszeromqnetmq

Cannot reuse an endpoint after NetMQ socket disposal


In my application I am creating and disposing NetMQ sockets. I have noticed that I cannot bind to an endpoint for some time after the disposal of the previous socket bound to that endpoint. A minimal example:

const string endpoint = "tcp://127.0.0.1:1234";

using (var publisher = new PublisherSocket())
    publisher.Bind(endpoint);

using (var publisher = new PublisherSocket())
    publisher.Bind(endpoint); //throws a NetMQException: "Unknown error (0xffffffff)"

The Linger option is set to 0, but I still have to wait (up to several seconds in my experiments) before the second bind.

I could add a Thread.Sleep or some other delaying instruction, but this is neither robust nor elegant.

Is this a bug? If not, what is the proper way of making sure that I can already bind to an endpoint?


Solution

  • Not a bug, the dispose of a socket is async and happen in a background thread. You can try and call Unbind before the dispose, it might be faster.