# Buggy code
def create_kernel_manager_and_kernel_client(
    self,
    connection_file,
    stderr_handle,
    is_cython=False,
    is_pylab=False,
    is_sympy=False,
):
    """Create kernel manager and client."""
    # Kernel spec
    kernel_spec = self.create_kernel_spec(
        is_cython=is_cython, is_pylab=is_pylab, is_sympy=is_sympy
    )

    # Kernel manager
    try:
        kernel_manager = SpyderKernelManager(
            connection_file=connection_file,
            config=None,
            autorestart=True,
        )
    except Exception:
        error_msg = _("The error is:<br><br><tt>{}</tt>").format(traceback.format_exc())
        return (error_msg, None)
    kernel_manager._kernel_spec = kernel_spec

    kwargs = {}
    if os.name == "nt":
        # avoid closing fds on win+Python 3.7
        # which prevents interrupts
        # jupyter_client > 5.2.3 will do this by default
        kwargs["close_fds"] = False
    # Catch any error generated when trying to start the kernel.
    # See spyder-ide/spyder#7302.
    try:
        kernel_manager.start_kernel(stderr=stderr_handle, **kwargs)
    except Exception:
        error_msg = _("The error is:<br><br><tt>{}</tt>").format(traceback.format_exc())
        return (error_msg, None)

    # Kernel client
    kernel_client = kernel_manager.client()

    # Increase time to detect if a kernel is alive.
    # See spyder-ide/spyder#3444.
    kernel_client.hb_channel.time_to_dead = 45.0

    return kernel_manager, kernel_client


# 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 create_kernel_manager_and_kernel_client(
    self,
    connection_file,
    stderr_handle,
    is_cython=False,
    is_pylab=False,
    is_sympy=False,
):
    """Create kernel manager and client."""
    # Kernel spec
    kernel_spec = self.create_kernel_spec(
        is_cython=is_cython, is_pylab=is_pylab, is_sympy=is_sympy
    )

    # Kernel manager
    try:
        kernel_manager = SpyderKernelManager(
            connection_file=connection_file,
            config=None,
            autorestart=True,
        )
    except Exception:
        error_msg = _("The error is:<br><br><tt>{}</tt>").format(traceback.format_exc())
        return (error_msg, None)
    kernel_manager._kernel_spec = kernel_spec

    # Catch any error generated when trying to start the kernel.
    # See spyder-ide/spyder#7302.
    try:
        kernel_manager.start_kernel(stderr=stderr_handle)
    except Exception:
        error_msg = _("The error is:<br><br><tt>{}</tt>").format(traceback.format_exc())
        return (error_msg, None)

    # Kernel client
    kernel_client = kernel_manager.client()

    # Increase time to detect if a kernel is alive.
    # See spyder-ide/spyder#3444.
    kernel_client.hb_channel.time_to_dead = 45.0

    return kernel_manager, kernel_client