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

    # Kernel manager
    try:
        kernel_manager = QtKernelManager(
            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

    # Save stderr in a file to read it later in case of errors
    if stderr_file is not None:
        # Needed to prevent any error that could appear.
        # See issue 6267
        try:
            stderr = codecs.open(stderr_file, "w", encoding="utf-8")
        except Exception:
            stderr = None
    else:
        stderr = None
    kernel_manager.start_kernel(stderr=stderr)

    # Kernel client
    kernel_client = kernel_manager.client()

    # Increase time to detect if a kernel is alive
    # See Issue 3444
    kernel_client.hb_channel.time_to_dead = 18.0

    return kernel_manager, kernel_client


# Traceback (PermissionError)
File "C:\Users\Ziru\Anaconda3\envs\tf_gpu\lib\site-packages\spyder\plugins\__init__.py", line 514, in <lambda>
toggled=lambda checked: self.toggle_view(checked),
File "C:\Users\Ziru\Anaconda3\envs\tf_gpu\lib\site-packages\spyder\plugins\ipythonconsole.py", line 725, in toggle_view
self.create_new_client(give_focus=False)
File "C:\Users\Ziru\Anaconda3\envs\tf_gpu\lib\site-packages\spyder\plugins\ipythonconsole.py", line 1046, in create_new_client
self.connect_client_to_kernel(client, is_cython=is_cython)
File "C:\Users\Ziru\Anaconda3\envs\tf_gpu\lib\site-packages\spyder\plugins\ipythonconsole.py", line 1074, in connect_client_to_kernel
is_cython=is_cython)
File "C:\Users\Ziru\Anaconda3\envs\tf_gpu\lib\site-packages\spyder\plugins\ipythonconsole.py", line 1522, in create_kernel_manager_and_kernel_client
kernel_manager.start_kernel(stderr=stderr)
File "C:\Users\Ziru\Anaconda3\envs\tf_gpu\lib\site-packages\jupyter_client\manager.py", line 259, in start_kernel
**kw)
File "C:\Users\Ziru\Anaconda3\envs\tf_gpu\lib\site-packages\jupyter_client\manager.py", line 204, in _launch_kernel
return launch_kernel(kernel_cmd, **kw)
File "C:\Users\Ziru\Anaconda3\envs\tf_gpu\lib\site-packages\jupyter_client\launcher.py", line 128, in launch_kernel
proc = Popen(cmd, **kwargs)
File "C:\Users\Ziru\Anaconda3\envs\tf_gpu\lib\subprocess.py", line 676, in __init__
restore_signals, start_new_session)
File "C:\Users\Ziru\Anaconda3\envs\tf_gpu\lib\subprocess.py", line 957, in _execute_child
startupinfo)
PermissionError: [WinError 5] Access is denied

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

    # Kernel manager
    try:
        kernel_manager = QtKernelManager(
            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

    # Save stderr in a file to read it later in case of errors
    if stderr_file is not None:
        # Needed to prevent any error that could appear.
        # See issue 6267
        try:
            stderr = codecs.open(stderr_file, "w", encoding="utf-8")
        except Exception:
            stderr = None
    else:
        stderr = None

    # Catch any error generated when trying to start the kernel
    # See issue 7302
    try:
        kernel_manager.start_kernel(stderr=stderr)
    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 Issue 3444
    kernel_client.hb_channel.time_to_dead = 18.0

    return kernel_manager, kernel_client