# Buggy code
def give_terminal_to(pgid):
    oldmask = signal.pthread_sigmask(signal.SIG_BLOCK, _block_when_giving)
    try:
        os.tcsetpgrp(FD_STDERR, pgid)
        return True
    except OSError as e:
        if e.errno == 22:  # [Errno 22] Invalid argument
            # there are cases that all the processes of pgid have
            # finished, then we don't need to do anything here, see
            # issue #2220
            return False
        elif e.errno == 25:  # [Errno 25] Inappropriate ioctl for device
            # There are also cases where we are not connected to a
            # real TTY, even though we may be run in interactive
            # mode. See issue #2267 for an example with emacs
            return False
        else:
            raise
    finally:
        signal.pthread_sigmask(signal.SIG_SETMASK, oldmask)


# Traceback (ChildProcessError)
$ tail -f /tmp/goixy.log &amp;
[1]+ running: tail -f /tmp/some.log &amp; &amp; (30817)

$ fg
[1]+ running: tail -f /tmp/some.log &amp; (30817)
(**Here fg seems brought something into foreground, but stuck, let's ^C it**)

^C
$ Exception in thread Thread-16:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/xonsh/proc.py",n
r = self.f(self.args, sp_stdin, sp_stdout, sp_stderr, spec)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/xonsh/proc.py",o
return f(args, stdin)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/xonsh/jobs.py",g
wait_for_active_job()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/xonsh/jobs.py",b
_, wcode = os.waitpid(obj.pid, os.WUNTRACED)
ChildProcessError: [Errno 10] No child processes

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 916, in _r
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/xonsh/proc.py",n
print_exception()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/xonsh/tools.py"n
traceback.print_exc()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/traceback.py", line 159, in pc
print_exception(*sys.exc_info(), limit=limit, file=file, chain=chain)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/traceback.py", line 101, in pn
print(line, file=file, end="")
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/xonsh/base_shele
self.std.write(std_s)
AttributeError: 'NoneType' object has no attribute 'write'

# Fixed code
def give_terminal_to(pgid):
    oldmask = signal.pthread_sigmask(signal.SIG_BLOCK, _block_when_giving)
    if pgid is None:
        signal.pthread_sigmask(signal.SIG_SETMASK, oldmask)
        return False
    try:
        os.tcsetpgrp(FD_STDERR, pgid)
        return True
    except OSError as e:
        if e.errno == 22:  # [Errno 22] Invalid argument
            # there are cases that all the processes of pgid have
            # finished, then we don't need to do anything here, see
            # issue #2220
            return False
        elif e.errno == 25:  # [Errno 25] Inappropriate ioctl for device
            # There are also cases where we are not connected to a
            # real TTY, even though we may be run in interactive
            # mode. See issue #2267 for an example with emacs
            return False
        else:
            raise
    finally:
        signal.pthread_sigmask(signal.SIG_SETMASK, oldmask)