# Buggy code
def wait_for_active_job(last_task=None, backgrounded=False):
    """
    Wait for the active job to finish, to be killed by SIGINT, or to be
    suspended by ctrl-z.
    """
    _clear_dead_jobs()
    active_task = get_next_task()
    # Return when there are no foreground active task
    if active_task is None:
        _give_terminal_to(_shell_pgrp)  # give terminal back to the shell
        if backgrounded and hasattr(builtins, "__xonsh_shell__"):
            # restoring sanity could probably be called whenever we return
            # control to the shell. But it only seems to matter after a
            # ^Z event. This *has* to be called after we give the terminal
            # back to the shell.
            builtins.__xonsh_shell__.shell.restore_tty_sanity()
        return last_task
    pgrp = active_task.get("pgrp", None)
    obj = active_task["obj"]
    backgrounded = False
    # give the terminal over to the fg process
    if pgrp is not None:
        _give_terminal_to(pgrp)
    _continue(active_task)
    _, wcode = os.waitpid(obj.pid, os.WUNTRACED)
    if os.WIFSTOPPED(wcode):
        print("^Z")
        active_task["status"] = "stopped"
        backgrounded = True
    elif os.WIFSIGNALED(wcode):
        print()  # get a newline because ^C will have been printed
        obj.signal = (os.WTERMSIG(wcode), os.WCOREDUMP(wcode))
        obj.returncode = None
    else:
        obj.returncode = os.WEXITSTATUS(wcode)
        obj.signal = None
    return wait_for_active_job(last_task=active_task, backgrounded=backgrounded)


# Traceback (ProcessLookupError)
ls | more
xonsh: To log full traceback to a file set: $XONSH_TRACEBACK_LOGFILE = <filename>
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/xonsh/__amalgam__.py", line 17380, in default
run_compiled_code(code, self.ctx, None, 'single')
File "/usr/local/lib/python3.6/site-packages/xonsh/__amalgam__.py", line 1964, in run_compiled_code
func(code, glb, loc)
File "<xonsh-code>", line 1, in <module>
File "/usr/local/lib/python3.6/site-packages/xonsh/__amalgam__.py", line 16159, in subproc_captured_hiddenobject
return run_subproc(cmds, captured='hiddenobject')
File "/usr/local/lib/python3.6/site-packages/xonsh/__amalgam__.py", line 16127, in run_subproc
command.end()
File "/usr/local/lib/python3.6/site-packages/xonsh/__amalgam__.py", line 11116, in end
for _ in self.tee_stdout():
File "/usr/local/lib/python3.6/site-packages/xonsh/__amalgam__.py", line 11041, in tee_stdout
for line in self.iterraw():
File "/usr/local/lib/python3.6/site-packages/xonsh/__amalgam__.py", line 10940, in iterraw
wait_for_active_job()
File "/usr/local/lib/python3.6/site-packages/xonsh/__amalgam__.py", line 3694, in wait_for_active_job
_continue(active_task)
File "/usr/local/lib/python3.6/site-packages/xonsh/__amalgam__.py", line 3607, in _continue
_send_signal(job, signal.SIGCONT)
File "/usr/local/lib/python3.6/site-packages/xonsh/__amalgam__.py", line 3542, in _send_signal
os.kill(pid, signal)
ProcessLookupError: [Errno 3] No such process

# Fixed code
def wait_for_active_job(last_task=None, backgrounded=False):
    """
    Wait for the active job to finish, to be killed by SIGINT, or to be
    suspended by ctrl-z.
    """
    _clear_dead_jobs()
    active_task = get_next_task()
    # Return when there are no foreground active task
    if active_task is None:
        return last_task
    obj = active_task["obj"]
    backgrounded = False
    try:
        _, wcode = os.waitpid(obj.pid, os.WUNTRACED)
    except ChildProcessError:  # No child processes
        return wait_for_active_job(last_task=active_task, backgrounded=backgrounded)
    if os.WIFSTOPPED(wcode):
        print("^Z")
        active_task["status"] = "stopped"
        backgrounded = True
    elif os.WIFSIGNALED(wcode):
        print()  # get a newline because ^C will have been printed
        obj.signal = (os.WTERMSIG(wcode), os.WCOREDUMP(wcode))
        obj.returncode = None
    else:
        obj.returncode = os.WEXITSTATUS(wcode)
        obj.signal = None
    return wait_for_active_job(last_task=active_task, backgrounded=backgrounded)