6

Context: Linux (Ubuntu), C, ZeroMQ

I have a server which listens on ipc:// SUB ZeroMQ socket (which physically is a Unix domain socket).

I have a client which should connect to the socket, publish its message and disconnect.

The problem: If server is killed (or otherwise dies unnaturally), socket file stays in place. If client attempts to connect to this stale socket, it blocks in zmq_term().

I need to prevent client from blocking if server is not there, but guarantee delivery if server is alive but busy.

Assume that I can not track server lifetime by some external magic (e.g. by checking a PID file).

Any hints?

Alexander Gladysh
  • 39,865
  • 32
  • 103
  • 160

4 Answers4

4

Non-portable solution seems to be to read /proc/net/unix and search there for a socket name.

Alexander Gladysh
  • 39,865
  • 32
  • 103
  • 160
1

Without showing your code all of this is guesswork... that said...

If you have a PUB/SUB pair, the PUB will hang around to make sure that its message gets through. Perhaps you're not using the right type of zmq pair. Sounds more like you have a REP/REQ pair instead.

This way, once you connect from the client (the REQ side), you can do a zmq_poll to determine if the socket is available for writing. If yes, then go ahead with your write, otherwise shutdown the client and handle the error condition (if it is an error in your system).

unpythonic
  • 4,020
  • 19
  • 20
1

There is another solution. Don't use ipc:// sockets. Instead use something like tcp://127.0.0.101:10001. On most UNIXes that will be almost as fast as IPC because the OS recognizes that it is a local connection and shortcuts the full IP stack processing.

Michael Dillon
  • 31,973
  • 6
  • 70
  • 106
0

Maybe you can try to connect to the socket first, with your own native socket. If the connection succeeds, it's quite high possibility your publisher could work fine.

Gary Shi
  • 645
  • 6
  • 13