# Buggy code
def _return_pub(self, ret, ret_cmd="_return"):
    """
    Return the data from the executed command to the master server
    """
    jid = ret.get("jid", ret.get("__jid__"))
    fun = ret.get("fun", ret.get("__fun__"))
    if self.opts["multiprocessing"]:
        fn_ = os.path.join(self.proc_dir, jid)
        if os.path.isfile(fn_):
            try:
                os.remove(fn_)
            except (OSError, IOError):
                # The file is gone already
                pass
    log.info("Returning information for job: {0}".format(jid))
    sreq = salt.payload.SREQ(self.opts["master_uri"])
    if ret_cmd == "_syndic_return":
        load = {
            "cmd": ret_cmd,
            "id": self.opts["id"],
            "jid": jid,
            "fun": fun,
            "load": ret.get("__load__"),
        }
        load["return"] = {}
        for key, value in ret.items():
            if key.startswith("__"):
                continue
            load["return"][key] = value
    else:
        load = {"cmd": ret_cmd, "id": self.opts["id"]}
        for key, value in ret.items():
            load[key] = value
    try:
        if hasattr(self.functions[ret["fun"]], "__outputter__"):
            oput = self.functions[ret["fun"]].__outputter__
            if isinstance(oput, string_types):
                load["out"] = oput
    except KeyError:
        pass
    try:
        ret_val = sreq.send("aes", self.crypticle.dumps(load))
    except SaltReqTimeoutError:
        ret_val = ""
    if isinstance(ret_val, string_types) and not ret_val:
        # The master AES key has changed, reauth
        self.authenticate()
        ret_val = sreq.send("aes", self.crypticle.dumps(load))
    if self.opts["cache_jobs"]:
        # Local job cache has been enabled
        fn_ = os.path.join(
            self.opts["cachedir"], "minion_jobs", load["jid"], "return.p"
        )
        jdir = os.path.dirname(fn_)
        if not os.path.isdir(jdir):
            os.makedirs(jdir)
        salt.utils.fopen(fn_, "w+").write(self.serial.dumps(ret))
    return ret_val


# Traceback (ZMQError)
[INFO    ] Authentication with master successful!
Process Process-10:
Traceback (most recent call last):
File "/usr/lib64/python2.6/multiprocessing/process.py", line 232, in _bootstrap
self.run()
File "/usr/lib64/python2.6/multiprocessing/process.py", line 88, in run
self._target(*self._args, **self._kwargs)
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 484, in _thread_return
minion_instance._return_pub(ret)
File "/usr/lib/python2.6/site-packages/salt/minion.py", line 606, in _return_pub
ret_val = sreq.send('aes', self.crypticle.dumps(load))
File "/usr/lib/python2.6/site-packages/salt/payload.py", line 176, in send
self.socket.send(pkg)
File "socket.pyx", line 499, in zmq.core.socket.Socket.send (zmq/core/socket.c:5381)
File "socket.pyx", line 546, in zmq.core.socket.Socket.send (zmq/core/socket.c:5143)
File "socket.pyx", line 175, in zmq.core.socket._send_copy (zmq/core/socket.c:2139)
ZMQError: Operation cannot be accomplished in current state

# Fixed code
def _return_pub(self, ret, ret_cmd="_return"):
    """
    Return the data from the executed command to the master server
    """
    jid = ret.get("jid", ret.get("__jid__"))
    fun = ret.get("fun", ret.get("__fun__"))
    if self.opts["multiprocessing"]:
        fn_ = os.path.join(self.proc_dir, jid)
        if os.path.isfile(fn_):
            try:
                os.remove(fn_)
            except (OSError, IOError):
                # The file is gone already
                pass
    log.info("Returning information for job: {0}".format(jid))
    sreq = salt.payload.SREQ(self.opts["master_uri"])
    if ret_cmd == "_syndic_return":
        load = {
            "cmd": ret_cmd,
            "id": self.opts["id"],
            "jid": jid,
            "fun": fun,
            "load": ret.get("__load__"),
        }
        load["return"] = {}
        for key, value in ret.items():
            if key.startswith("__"):
                continue
            load["return"][key] = value
    else:
        load = {"cmd": ret_cmd, "id": self.opts["id"]}
        for key, value in ret.items():
            load[key] = value
    try:
        if hasattr(self.functions[ret["fun"]], "__outputter__"):
            oput = self.functions[ret["fun"]].__outputter__
            if isinstance(oput, string_types):
                load["out"] = oput
    except KeyError:
        pass
    try:
        ret_val = sreq.send("aes", self.crypticle.dumps(load))
    except SaltReqTimeoutError:
        msg = (
            "The minion failed to return the job information for job "
            "{0}. This is often due to the master being shut down or "
            "overloaded. If the master is running consider incresing "
            "the worker_threads value."
        ).format(jid)
        log.warn(msg)
        return ""
    if isinstance(ret_val, string_types) and not ret_val:
        # The master AES key has changed, reauth
        self.authenticate()
        ret_val = sreq.send("aes", self.crypticle.dumps(load))
    if self.opts["cache_jobs"]:
        # Local job cache has been enabled
        fn_ = os.path.join(
            self.opts["cachedir"], "minion_jobs", load["jid"], "return.p"
        )
        jdir = os.path.dirname(fn_)
        if not os.path.isdir(jdir):
            os.makedirs(jdir)
        salt.utils.fopen(fn_, "w+").write(self.serial.dumps(ret))
    return ret_val