# Buggy code
def save_load(jid, clear_load):
    """
    Save the load to the specified jid
    """
    jid_dir = _jid_dir(jid)

    serial = salt.payload.Serial(__opts__)

    # Save the invocation information
    try:
        if not os.path.exists(jid_dir):
            os.makedirs(jid_dir)
        serial.dump(clear_load, salt.utils.fopen(os.path.join(jid_dir, LOAD_P), "w+b"))
    except IOError as exc:
        log.warning("Could not write job invocation cache file: {0}".format(exc))

    # if you have a tgt, save that for the UI etc
    if "tgt" in clear_load:
        ckminions = salt.utils.minions.CkMinions(__opts__)
        # Retrieve the minions list
        minions = ckminions.check_minions(
            clear_load["tgt"], clear_load.get("tgt_type", "glob")
        )
        # save the minions to a cache so we can see in the UI
        try:
            serial.dump(
                minions, salt.utils.fopen(os.path.join(jid_dir, MINIONS_P), "w+b")
            )
        except IOError as exc:
            log.warning(
                "Could not write job cache file for minions: {0}".format(minions)
            )
            log.debug("Job cache write failure: {0}".format(exc))


# Traceback (OSError)
2014-12-08 18:42:10,787 [salt.master                                 ][ERROR   ] Error in function _file_hash:
Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/master.py", line 1687, in run_func
ret = getattr(self, func)(load)
File "/usr/lib/python2.6/site-packages/salt/fileserver/__init__.py", line 354, in file_hash
return self.servers[fstr](load, fnd)
File "/usr/lib/python2.6/site-packages/salt/fileserver/roots.py", line 218, in file_hash
os.makedirs(cache_dir)
File "/usr/lib64/python2.6/os.py", line 157, in makedirs
mkdir(name, mode)
OSError: [Errno 17] File exists: '/var/cache/salt/master/roots/hash/base/common/repos'

# Fixed code
def save_load(jid, clear_load):
    """
    Save the load to the specified jid
    """
    jid_dir = _jid_dir(jid)

    serial = salt.payload.Serial(__opts__)

    # Save the invocation information
    try:
        if not os.path.exists(jid_dir):
            os.makedirs(jid_dir)
    except OSError as exc:
        if exc.errno == errno.EEXIST:
            # rarely, the directory can be already concurrently created between
            # the os.path.exists and the os.makedirs lines above
            pass
        else:
            raise
    try:
        serial.dump(clear_load, salt.utils.fopen(os.path.join(jid_dir, LOAD_P), "w+b"))
    except IOError as exc:
        log.warning("Could not write job invocation cache file: {0}".format(exc))

    # if you have a tgt, save that for the UI etc
    if "tgt" in clear_load:
        ckminions = salt.utils.minions.CkMinions(__opts__)
        # Retrieve the minions list
        minions = ckminions.check_minions(
            clear_load["tgt"], clear_load.get("tgt_type", "glob")
        )
        # save the minions to a cache so we can see in the UI
        try:
            serial.dump(
                minions, salt.utils.fopen(os.path.join(jid_dir, MINIONS_P), "w+b")
            )
        except IOError as exc:
            log.warning(
                "Could not write job cache file for minions: {0}".format(minions)
            )
            log.debug("Job cache write failure: {0}".format(exc))