jeromq: Socket.close() not freeing resources after send/recv timeout

I am running into a problem where jeromq is not freeing resources if the Socket is unable to connect. This is reproducible with the following code:

import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Context;
import org.zeromq.ZMQ.Socket;

public class rrclient {
    public static void main(String[] args) {
        Context context = ZMQ.context(1);

        while (true) {
            try (Socket socket = context.socket(ZMQ.REQ)) {
                socket.connect("tcp://localhost:33333");
                socket.setSendTimeOut(1000);
                socket.setReceiveTimeOut(1000);
                socket.send("Hello");
                String reply = socket.recvStr(0);
                if (reply == null) {
                    System.out.println("TIMEOUT");
                } else {
                    System.out.println("Received reply [" + reply + "]");
                }
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
        }
        context.term();
    }
}

Simply run this code without a server listening on the other end, and the process will eventually consume all file handles on the machine. I was able to reproduce this on linux and macos.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 18 (9 by maintainers)

Most upvoted comments

It looks like ZContext automatically sets linger (to 0 by default) when destroying sockets, but ZMQ.Context does not, which could be part of the issue.

Reproduced it on my machine as well.

Inserting that helps to speed up the failing process:

        Context context = ZMQ.context(1);
        context.setMaxSockets(10);

Setting this line as the last one in the try block fixes the situation:

                socket.setLinger(0);
            }
            catch (Exception e) {
...