# Buggy code
def kill_proc_tree(
    pid, sig=signal.SIGTERM, include_parent=True, timeout=None, on_terminate=None
):
    """
    Kill a process tree (including grandchildren) with sig and return a
    (gone, still_alive) tuple.

    "on_terminate", if specified, is a callabck function which is called
    as soon as a child terminates.

    This is an new method not present in QtKernelManager.
    """
    assert pid != os.getpid()  # Won't kill myself!
    parent = psutil.Process(pid)
    children = parent.children(recursive=True)

    if include_parent:
        children.append(parent)

    for child_process in children:
        child_process.send_signal(sig)

    gone, alive = psutil.wait_procs(
        children,
        timeout=timeout,
        callback=on_terminate,
    )

    return (gone, alive)


# Traceback (ProcessLookupError)
File "C:\Users\zhour\Anaconda3\lib\site-packages\psutil\_pswindows.py", line 707, in wrapper
return fun(self, *args, **kwargs)
File "C:\Users\zhour\Anaconda3\lib\site-packages\psutil\_pswindows.py", line 954, in create_time
return cext.proc_create_time(self.pid)
ProcessLookupError: [Errno 3] No such process

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\zhour\Anaconda3\lib\site-packages\psutil\__init__.py", line 474, in _init
self.create_time()
File "C:\Users\zhour\Anaconda3\lib\site-packages\psutil\__init__.py", line 824, in create_time
self._create_time = self._proc.create_time()
File "C:\Users\zhour\Anaconda3\lib\site-packages\psutil\_pswindows.py", line 709, in wrapper
raise convert_oserror(err, pid=self.pid, name=self._name)
psutil.NoSuchProcess: psutil.NoSuchProcess process no longer exists (pid=16124)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\zhour\Anaconda3\lib\site-packages\qtconsole\manager.py", line 27, in poll
super(QtKernelRestarter, self).poll()
File "C:\Users\zhour\Anaconda3\lib\site-packages\jupyter_client\restarter.py", line 113, in poll
self.kernel_manager.restart_kernel(now=True, newports=newports)
File "C:\Users\zhour\Anaconda3\lib\site-packages\jupyter_client\manager.py", line 361, in restart_kernel
self.shutdown_kernel(now=now, restart=True)
File "C:\Users\zhour\Anaconda3\lib\site-packages\jupyter_client\manager.py", line 321, in shutdown_kernel
self._kill_kernel()
File "C:\Users\zhour\Anaconda3\lib\site-packages\spyder\plugins\ipythonconsole\utils\manager.py", line 78, in _kill_kernel
self.kill_proc_tree(self.kernel.pid)
File "C:\Users\zhour\Anaconda3\lib\site-packages\spyder\plugins\ipythonconsole\utils\manager.py", line 44, in kill_proc_tree
parent = psutil.Process(pid)
File "C:\Users\zhour\Anaconda3\lib\site-packages\psutil\__init__.py", line 447, in __init__
self._init(pid)
File "C:\Users\zhour\Anaconda3\lib\site-packages\psutil\__init__.py", line 487, in _init
raise NoSuchProcess(pid, None, msg)
psutil.NoSuchProcess: psutil.NoSuchProcess no process found with pid 16124
WARNING:traitlets:kernel died: 18.000895500183105
WARNING:traitlets:kernel died: 18.000853061676025
WARNING:traitlets:kernel died: 18.000853538513184
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853061676025
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853061676025
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000852823257446
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853061676025

# Fixed code
def kill_proc_tree(
    pid, sig=signal.SIGTERM, include_parent=True, timeout=None, on_terminate=None
):
    """
    Kill a process tree (including grandchildren) with sig and return a
    (gone, still_alive) tuple.

    "on_terminate", if specified, is a callabck function which is called
    as soon as a child terminates.

    This is an new method not present in QtKernelManager.
    """
    assert pid != os.getpid()  # Won't kill myself!

    # This is necessary to avoid showing an error when restarting the
    # kernel after it failed to start in the first place.
    # Fixes spyder-ide/spyder#11872
    try:
        parent = psutil.Process(pid)
    except psutil.NoSuchProcess:
        return ([], [])

    children = parent.children(recursive=True)

    if include_parent:
        children.append(parent)

    for child_process in children:
        child_process.send_signal(sig)

    gone, alive = psutil.wait_procs(
        children,
        timeout=timeout,
        callback=on_terminate,
    )

    return (gone, alive)