[zeromq-dev] Problem with multiple sockets under Windows: Address already in use

Seref Arikan serefarikan at kurumsalteknoloji.com
Mon Jun 20 15:31:11 CEST 2011


Greetings,
I've come across a problem when I tried to use multiple sockets from
Java. I've managed to create the same problem with the sample C code
below, so I'm assuming this is not a Java or JNI issue.

When I try to close a socket and open a new one, I get the following
error and the process dies (both in C and Java cases): Assertion
failed: Address already in use (..\..\..\src\mailbox.cpp:305)

The Java case: I've experimented with it, and it almost feels as if
Windows is running out of sockets. The error does not consistently
show up, sometimes I can execute a thousand calls consequently, and
sometimes the process dies on me with the first ever call.
With Java, if I create a 100 threads and simply keep the Context and
sockets in the thread local storage, there are no issues. Java code
simply uses connected sockets without closing them, and everything
works.

If I add a Thread.sleep(300) to Java code, and close sockets, it takes
longer for the code to crash, but it crashes at the end.

The C case: The code below crashes with the same error :
//-------------------------------------------------------------------------------------
#include "ZMQTests.h"

int _tmain(int argc, _TCHAR* argv[])
{
	for(int i = 0; i < 1000; i++){
		void *context = zmq_init(1);
		void *socket = zmq_socket(context, ZMQ_REQ);
		zmq_connect(socket,"tcp://127.0.0.1:5560");
		//do nothing with the socket		
		zmq_close(socket);
		zmq_term(context);
	}
	
	return 0;
}
//-------------------------------------------------------------------------------------

The code below works fine if the loop is repeated only 100 times. I'm
also almost sure that if I wait between executions of the code, it
takes more time for it to crash. It is almost as if Windows is slowly
releasing some resources at the background. If I try to execute it
immediately after the crash, then it crashes immediately.

I'm using the version of the code I've checked out from the git repo
about two weeks ago. This is Windows xp sp3 32 bit. I've come across
this issue, which is similar, but the error is from a different file:
https://zeromq.jira.com/browse/LIBZMQ-84

Keeping contexts and sockets alive is an acceptable workaround for me
at the moment, but I'd like to know if I'm missing somethings, or if
this is a known issue.

Best regards
Seref



More information about the zeromq-dev mailing list