# Buggy code
def clean_proc_dir(opts):
    """
    Loop through jid files in the minion proc directory (default /var/cache/salt/minion/proc)
    and remove any that refer to processes that no longer exist
    """

    for basefilename in os.listdir(salt.minion.get_proc_dir(opts["cachedir"])):
        fn = os.path.join(salt.minion.get_proc_dir(opts["cachedir"]), basefilename)
        with salt.utils.fopen(fn, "r") as fp_:
            job = salt.payload.Serial(opts).load(fp_)
            log.debug(
                "schedule.clean_proc_dir: checking job {0} for process "
                "existence".format(job)
            )
            if job is not None and "pid" in job:
                if salt.utils.process.os_is_running(job["pid"]):
                    log.debug(
                        "schedule.clean_proc_dir: Cleaning proc dir, "
                        "pid {0} still exists.".format(job["pid"])
                    )
                else:
                    # Windows cannot delete an open file
                    if salt.utils.is_windows():
                        fp_.close()
                    # Maybe the file is already gone
                    try:
                        os.unlink(fn)
                    except OSError:
                        pass


# Traceback (msgpack.exceptions.UnpackValueError)
Traceback (most recent call last):
File "<string>", line 6, in <module>
File "__main__.py", line 726, in <module>
File "__main__.py", line 332, in bootstrap
File "__main__.py", line 359, in chainload
File "__main__.py", line 715, in _chainload
File "__main__.py", line 128, in <module>
File "__main__salt-minion__.py", line 17, in <module>
File "salt/scripts.py", line 35, in salt_minion
File "salt/__init__.py", line 221, in start
File "salt/__init__.py", line 209, in prepare
File "salt/minion.py", line 543, in __init__
File "salt/utils/schedule.py", line 73, in __init__
File "salt/utils/schedule.py", line 265, in clean_proc_dir
File "salt/payload.py", line 108, in load
File "salt/payload.py", line 95, in loads
File "msgpack/fallback.py", line 95, in unpackb
msgpack.exceptions.UnpackValueError: Data is not enough.

# Fixed code
def clean_proc_dir(opts):
    """
    Loop through jid files in the minion proc directory (default /var/cache/salt/minion/proc)
    and remove any that refer to processes that no longer exist
    """

    for basefilename in os.listdir(salt.minion.get_proc_dir(opts["cachedir"])):
        fn_ = os.path.join(salt.minion.get_proc_dir(opts["cachedir"]), basefilename)
        with salt.utils.fopen(fn_, "r") as fp_:
            job = None
            try:
                job = salt.payload.Serial(opts).load(fp_)
            except Exception:  # It's corrupted
                try:
                    os.unlink(fn_)
                    continue
                except OSError:
                    continue
            log.debug(
                "schedule.clean_proc_dir: checking job {0} for process "
                "existence".format(job)
            )
            if job is not None and "pid" in job:
                if salt.utils.process.os_is_running(job["pid"]):
                    log.debug(
                        "schedule.clean_proc_dir: Cleaning proc dir, "
                        "pid {0} still exists.".format(job["pid"])
                    )
                else:
                    # Windows cannot delete an open file
                    if salt.utils.is_windows():
                        fp_.close()
                    # Maybe the file is already gone
                    try:
                        os.unlink(fn_)
                    except OSError:
                        pass