Pieter Hintjens gave me the hint on the mailing list:
The call to zmq_close
initiates the socket shutdown. This is done in a special "reaper" thread started by ZeroMQ to make the call to zmq_close
asynchronous and non-blocking. See "The reaper thread" in a whitepaper about ZeroMQ's architecture.
The code above does not wait for the thread doing the actual work, so the endpoint will not become available immediately.