# Buggy code
def run_job(
    self,
    tgt,
    fun,
    arg=(),
    expr_form="glob",
    ret="",
    timeout=None,
    jid="",
    kwarg=None,
    **kwargs,
):
    """
    Asynchronously send a command to connected minions

    Prep the job directory and publish a command to any targeted minions.

    :return: A dictionary of (validated) ``pub_data`` or an empty
        dictionary on failure. The ``pub_data`` contains the job ID and a
        list of all minions that are expected to return data.

    .. code-block:: python

        >>> local.run_job('*', 'test.sleep', [300])
        {'jid': '20131219215650131543', 'minions': ['jerry']}
    """
    arg = salt.utils.args.condition_input(arg, kwarg)

    # Subscribe to all events and subscribe as early as possible
    self.event.subscribe(jid)

    pub_data = self.pub(
        tgt,
        fun,
        arg,
        expr_form,
        ret,
        jid=jid,
        timeout=self._get_timeout(timeout),
        **kwargs,
    )

    return self._check_pub_data(pub_data)


# Traceback (KeyError)
# salt-run
[ERROR   ] An un-handled exception was caught by salt's global exception handler:
KeyError:
Traceback (most recent call last):
File "/usr/local/bin/salt-run", line 10, in <module>
salt_run()
File "/root/SaltStack/salt/salt/scripts.py", line 243, in salt_run
client.run()
File "/root/SaltStack/salt/salt/cli/run.py", line 54, in run
runner.run()
File "/root/SaltStack/salt/salt/runner.py", line 138, in run
self.functions[low['fun']],
File "/root/SaltStack/salt/salt/loader.py", line 854, in __getitem__
mod = super(LazyLoader, self).__getitem__(key)
File "/root/SaltStack/salt/salt/utils/lazy.py", line 62, in __getitem__
if self._load(key):
File "/root/SaltStack/salt/salt/loader.py", line 1052, in _load
raise KeyError
KeyError
Traceback (most recent call last):
File "/usr/local/bin/salt-run", line 10, in <module>
salt_run()
File "/root/SaltStack/salt/salt/scripts.py", line 243, in salt_run
client.run()
File "/root/SaltStack/salt/salt/cli/run.py", line 54, in run
runner.run()
File "/root/SaltStack/salt/salt/runner.py", line 138, in run
self.functions[low['fun']],
File "/root/SaltStack/salt/salt/loader.py", line 854, in __getitem__
mod = super(LazyLoader, self).__getitem__(key)
File "/root/SaltStack/salt/salt/utils/lazy.py", line 62, in __getitem__
if self._load(key):
File "/root/SaltStack/salt/salt/loader.py", line 1052, in _load
raise KeyError
KeyError

# Fixed code
def run_job(
    self,
    tgt,
    fun,
    arg=(),
    expr_form="glob",
    ret="",
    timeout=None,
    jid="",
    kwarg=None,
    **kwargs,
):
    """
    Asynchronously send a command to connected minions

    Prep the job directory and publish a command to any targeted minions.

    :return: A dictionary of (validated) ``pub_data`` or an empty
        dictionary on failure. The ``pub_data`` contains the job ID and a
        list of all minions that are expected to return data.

    .. code-block:: python

        >>> local.run_job('*', 'test.sleep', [300])
        {'jid': '20131219215650131543', 'minions': ['jerry']}
    """
    arg = salt.utils.args.condition_input(arg, kwarg)

    # Subscribe to all events and subscribe as early as possible
    self.event.subscribe(jid)

    try:
        pub_data = self.pub(
            tgt,
            fun,
            arg,
            expr_form,
            ret,
            jid=jid,
            timeout=self._get_timeout(timeout),
            **kwargs,
        )
    except SaltClientError:
        # Re-raise error with specific message
        print("The salt master could not be contacted. Is master running?")
        sys.exit(1)

    return self._check_pub_data(pub_data)