# Buggy code
def _kill_kernel(self):
    """
    Kill the running kernel.

    Override private method to be able to correctly close kernel that was
    started via a batch/bash script for correct conda env activation.
    """
    if self.has_kernel:
        # Signal the kernel to terminate (sends SIGKILL on Unix and calls
        # TerminateProcess() on Win32).
        try:
            if hasattr(signal, "SIGKILL"):
                self.signal_kernel(signal.SIGKILL)
            else:
                # This is the additional line that was added to properly
                # kill the spyder started kernels.
                self.kill_proc_tree(self.kernel.pid)

                self.kernel.kill()
        except OSError as e:
            # In Windows, we will get an Access Denied error if the process
            # has already terminated. Ignore it.
            if sys.platform == "win32":
                if e.winerror != 5:
                    raise
            # On Unix, we may get an ESRCH error if the process has already
            # terminated. Ignore it.
            else:
                from errno import ESRCH

                if e.errno != ESRCH:
                    raise

        # Block until the kernel terminates.
        self.kernel.wait()
        self.kernel = None
    else:
        raise RuntimeError("Cannot kill kernel. No kernel is running!")


# 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_kernel(self):
    """
    Kill the running kernel.

    Override private method to be able to correctly close kernel that was
    started via a batch/bash script for correct conda env activation.
    """
    if self.has_kernel:
        # Signal the kernel to terminate (sends SIGKILL on Unix and calls
        # TerminateProcess() on Win32).
        try:
            if hasattr(signal, "SIGKILL"):
                self.signal_kernel(signal.SIGKILL)
            else:
                # This is the additional line that was added to properly
                # kill the kernel started by Spyder.
                self.kill_proc_tree(self.kernel.pid)

                self.kernel.kill()
        except OSError as e:
            # In Windows, we will get an Access Denied error if the process
            # has already terminated. Ignore it.
            if sys.platform == "win32":
                if e.winerror != 5:
                    raise
            # On Unix, we may get an ESRCH error if the process has already
            # terminated. Ignore it.
            else:
                from errno import ESRCH

                if e.errno != ESRCH:
                    raise

        # Block until the kernel terminates.
        self.kernel.wait()
        self.kernel = None
    else:
        raise RuntimeError("Cannot kill kernel. No kernel is running!")