Address already in use exception when creating a new request socket.

853 views
Skip to first unread message

Dean Roker

unread,
Aug 18, 2014, 1:52:07 PM8/18/14
to netm...@googlegroups.com
Hi Everyone,

I am creating a Request Socket in a Task and I keep getting an exception "Address already in use"?????

I could understand this If I was binding to an address that was already in use but I am connecting a client to an inproc socket. Any ideas as to what is going wrong?

The code that causes the exception is below. I pass the context into the thread from my main application, and create the new sockets there. Should I make the context a ref parameter?

Thanks

Dean

        public FileStorageClient (NetMQContext context, byte[] destinationUUID, string destinationName)
        {
            clientContext = context;
            serverAddress = "inproc://lfs_" + RKRHelper.ByteArrayToHexString(destinationUUID);
            log4net.ThreadContext.Properties[ "destname" ] = destinationName;
                try {
                clientSocket = CreateClientSocket (clientContext, serverAddress);
                } catch (System.Net.Sockets.SocketException e) {
                    log.Error (e.Message);
                    log.Error ("got a Socket Error in File Storeage Client");
                }
            }

        }

        private NetMQSocket CreateClientSocket(NetMQContext context,string address) 
        {
            log.Debug("Connecting to server " + address + "");
            var client = context.CreateRequestSocket();
            client.Options.Linger = TimeSpan.Zero;
            client.ReceiveReady += ClientOnReceiveReady;
            client.Connect(address);
            return client;
        }


And I always dispose of the Socket properly and remove the Receive Ready function so as to not cause a memory leak.

        public
 void Dispose()

        {
            clientSocket.ReceiveReady -= ClientOnReceiveReady;
            clientSocket.Dispose();
        }


And I call the Constructor from this code....

using (FileStorageClient _fileStore = new FileStorageClient(_backupContext, _destinationUUID, destName)) 
            using (NetworkClient networkClient = new NetworkClient(_destinationServerAddress,_clientUUID, _backupContext, networkTimeout)) {
                networkClient.Connect ();

Doron Somech

unread,
Aug 18, 2014, 3:45:12 PM8/18/14
to Dean Roker, netm...@googlegroups.com
Can you upload minimal code to reproduce? also with inproc you have to bind before you can connect, so you might check this as well.


--
You received this message because you are subscribed to the Google Groups "netmq-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netmq-dev+...@googlegroups.com.
To post to this group, send email to netm...@googlegroups.com.
Visit this group at http://groups.google.com/group/netmq-dev.
For more options, visit https://groups.google.com/d/optout.

Dean Roker

unread,
Aug 18, 2014, 4:27:22 PM8/18/14
to netm...@googlegroups.com, dean...@gmail.com
Hi Doron,

I bind the socket well in advance, I have a server object that goes with the client that I create when the program starts. The code works for a period of time before stopping with the error so I must have everything the right way around.

As Inproc uses shared memory? Is it OK to have multiple Request Inproc Sockets (Each in its own thread) connected to a single Reply Inproc socket (In another thread)? That is how my program is set up at the moment.

I did not see this error when I wrote some test code, so I am not sure I can reproduce the problem. I will see if I can recreate the problem another way.

Thanks

Dean

Doron Somech

unread,
Aug 19, 2014, 2:21:29 AM8/19/14
to Dean Roker, netm...@googlegroups.com

It sounds that you are doing everything right... try to reproduce and will be able to help you from there...

Dean Roker

unread,
Aug 20, 2014, 3:49:23 AM8/20/14
to netm...@googlegroups.com, dean...@gmail.com
Hi Doron,

I have managed to recreate the problem with some cut down code. Please see the file attached.

I am running under Mono on Linux, not sure if that has anything to do with it.

If you can shed any light on things that would be great.

Thanks

Dean
Program.cs

Dean Roker

unread,
Aug 20, 2014, 7:34:47 AM8/20/14
to netm...@googlegroups.com, dean...@gmail.com
I have run my code a few times and I get several different Exceptions.

I have got this one.....

ystem.Net.Sockets.SocketException: The requested address is not valid in this context
  at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Signaler.MakeFDpair () [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Signaler..ctor () [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Mailbox..ctor (System.String name) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.SocketBase..ctor (NetMQ.zmq.Ctx parent, Int32 threadId, Int32 sid) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Dealer..ctor (NetMQ.zmq.Ctx parent, Int32 threadId, Int32 sid) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Req..ctor (NetMQ.zmq.Ctx parent, Int32 threadId, Int32 sid) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.SocketBase.Create (ZmqSocketType type, NetMQ.zmq.Ctx parent, Int32 tid, Int32 sid) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Ctx.CreateSocket (ZmqSocketType type) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.ZMQ.Socket (NetMQ.zmq.Ctx ctx, ZmqSocketType type) [0x00000] in <filename unknown>:0
  at NetMQ.NetMQContext.CreateRequestSocket () [0x00000] in <filename unknown>:0
  at netmqproblem.Client..ctor (System.String address, NetMQ.NetMQContext context) [0x00000] in <filename unknown>:0

Unhandled Exception:
System.AggregateException:  ---> System.Net.Sockets.SocketException: The requested address is not valid in this context
  at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Signaler.MakeFDpair () [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Signaler..ctor () [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Mailbox..ctor (System.String name) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.SocketBase..ctor (NetMQ.zmq.Ctx parent, Int32 threadId, Int32 sid) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Dealer..ctor (NetMQ.zmq.Ctx parent, Int32 threadId, Int32 sid) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Req..ctor (NetMQ.zmq.Ctx parent, Int32 threadId, Int32 sid) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.SocketBase.Create (ZmqSocketType type, NetMQ.zmq.Ctx parent, Int32 tid, Int32 sid) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Ctx.CreateSocket (ZmqSocketType type) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.ZMQ.Socket (NetMQ.zmq.Ctx ctx, ZmqSocketType type) [0x00000] in <filename unknown>:0
  at NetMQ.NetMQContext.CreateRequestSocket () [0x00000] in <filename unknown>:0
  at netmqproblem.Client..ctor (System.String address, NetMQ.NetMQContext context) [0x00000] in <filename unknown>:0
  --- End of inner exception stack trace ---
  at System.Threading.Tasks.Task`1[System.Boolean].get_Result () [0x00000] in <filename unknown>:0
  at netmqproblem.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0
 --> (Inner exception 0) System.Net.Sockets.SocketException: The requested address is not valid in this context
  at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Signaler.MakeFDpair () [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Signaler..ctor () [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Mailbox..ctor (System.String name) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.SocketBase..ctor (NetMQ.zmq.Ctx parent, Int32 threadId, Int32 sid) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Dealer..ctor (NetMQ.zmq.Ctx parent, Int32 threadId, Int32 sid) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Req..ctor (NetMQ.zmq.Ctx parent, Int32 threadId, Int32 sid) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.SocketBase.Create (ZmqSocketType type, NetMQ.zmq.Ctx parent, Int32 tid, Int32 sid) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Ctx.CreateSocket (ZmqSocketType type) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.ZMQ.Socket (NetMQ.zmq.Ctx ctx, ZmqSocketType type) [0x00000] in <filename unknown>:0
  at NetMQ.NetMQContext.CreateRequestSocket () [0x00000] in <filename unknown>:0
  at netmqproblem.Client..ctor (System.String address, NetMQ.NetMQContext context) [0x00000] in <filename unknown>:0

and sometimes I get this one

System.Net.Sockets.SocketException: Too many open files
  at System.Net.Sockets.Socket..ctor (AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Signaler.MakeFDpair () [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Signaler..ctor () [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Mailbox..ctor (System.String name) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.SocketBase..ctor (NetMQ.zmq.Ctx parent, Int32 threadId, Int32 sid) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Dealer..ctor (NetMQ.zmq.Ctx parent, Int32 threadId, Int32 sid) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Req..ctor (NetMQ.zmq.Ctx parent, Int32 threadId, Int32 sid) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.SocketBase.Create (ZmqSocketType type, NetMQ.zmq.Ctx parent, Int32 tid, Int32 sid) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.Ctx.CreateSocket (ZmqSocketType type) [0x00000] in <filename unknown>:0
  at NetMQ.zmq.ZMQ.Socket (NetMQ.zmq.Ctx ctx, ZmqSocketType type) [0x00000] in <filename unknown>:0
  at NetMQ.NetMQContext.CreateRequestSocket () [0x00000] in <filename unknown>:0
  at netmqproblem.Client..ctor (System.String address, NetMQ.NetMQContext context) [0x00000] in <filename unknown>:0

Anything else I can do to help you trouble shoot this? Seems like something isn't being released correctly.

Thanks

Dean

Dean Roker

unread,
Aug 20, 2014, 7:37:20 AM8/20/14
to netm...@googlegroups.com, dean...@gmail.com
Oh and If I change it to TCP from inproc I get no errors at all.

Thanks

Dean

Dean Roker

unread,
Aug 20, 2014, 8:33:41 AM8/20/14
to netm...@googlegroups.com, dean...@gmail.com
I spoke to soon,

 if you leave it running long enough I get the Address Already in use Exception again.

Thanks

Dean

Doron Somech

unread,
Aug 20, 2014, 9:00:31 AM8/20/14
to Dean Roker, netm...@googlegroups.com
Dean are you running the nuget version or from the master repository?

Jon New

unread,
Sep 2, 2014, 7:48:25 PM9/2/14
to netm...@googlegroups.com, dean...@gmail.com
I'm also seeing this problem on Mono on Linux.  ZMQ version3.3.0.10.  Everything seems to be working fine, then I'll see this exception when the sender calls context.CreateRequestSocket.  Subsequent calls to context.CreateRequestSocket will just hang forever.

Here's my exception:
Caught unexpected exception System.Net.Sockets.SocketException: Address already in use
  at System.Net.Sockets.Socket.Bind (System.Net.EndPoint local_end) [0x00000] in <filename unknown>:0 
  at NetMQ.zmq.Signaler.MakeFDpair () [0x00000] in <filename unknown>:0 
  at NetMQ.zmq.Signaler..ctor () [0x00000] in <filename unknown>:0 
  at NetMQ.zmq.Mailbox..ctor (System.String name) [0x00000] in <filename unknown>:0 
  at NetMQ.zmq.SocketBase..ctor (NetMQ.zmq.Ctx parent, Int32 threadId, Int32 sid) [0x00000] in <filename unknown>:0 
  at NetMQ.zmq.Dealer..ctor (NetMQ.zmq.Ctx parent, Int32 threadId, Int32 sid) [0x00000] in <filename unknown>:0 
  at NetMQ.zmq.Req..ctor (NetMQ.zmq.Ctx parent, Int32 threadId, Int32 sid) [0x00000] in <filename unknown>:0 
  at NetMQ.zmq.SocketBase.Create (ZmqSocketType type, NetMQ.zmq.Ctx parent, Int32 tid, Int32 sid) [0x00000] in <filename unknown>:0 
  at NetMQ.zmq.Ctx.CreateSocket (ZmqSocketType type) [0x00000] in <filename unknown>:0 
  at NetMQ.zmq.ZMQ.Socket (NetMQ.zmq.Ctx ctx, ZmqSocketType type) [0x00000] in <filename unknown>:0 
  at NetMQ.NetMQContext.CreateRequestSocket () [0x00000] in <filename unknown>:0 

Additionally, in this class (https://github.com/zeromq/netmq/blob/f1f780bd011189061732c5ec80e4fe3bd6cf0932/src/NetMQ/zmq/Signaler.cs#L73) , it looks like there's a few things that don't get cleaned up if there's an exception.  Mutex and Socket implement IDisposable, but aren't taking advantage of the using construct.  In my case Bind throws the exception, but sync and listner will never be released or disposed.

Doron Somech

unread,
Sep 3, 2014, 6:18:26 AM9/3/14
to Jon New, netm...@googlegroups.com, Dean Roker
Compile from following location, it should solve the problem:



Jon New

unread,
Sep 5, 2014, 3:38:08 PM9/5/14
to netm...@googlegroups.com, jon...@gmail.com, dean...@gmail.com
This worked great, thanks Doron!

FYI, I had a little trouble compiling because of the SocketOptionName.IPv6Only enum, which wasn't in my version of Mono (3.2.8); I didn't see if it's available in later versions.

Thanks again!

Jon

Doron Somech

unread,
Sep 6, 2014, 2:06:18 AM9/6/14
to Jon New, Dean Roker, netm...@googlegroups.com

You welcome, I will check the IP thing.  ..

Reply all
Reply to author
Forward
0 new messages