# Buggy code
def _get_call_return_value(self, call_dict, call_data, comm_id):
    """
    Interupt the kernel if needed.
    """
    settings = call_dict["settings"]
    blocking = "blocking" in settings and settings["blocking"]

    if not self.kernel_client.is_alive():
        if blocking:
            raise RuntimeError("Kernel is dead")
        else:
            # The user has other problems
            return

    settings = call_dict["settings"]
    interrupt = "interrupt" in settings and settings["interrupt"]

    try:
        with self.comm_channel_manager(comm_id, queue_message=not interrupt):
            return super(KernelComm, self)._get_call_return_value(
                call_dict, call_data, comm_id
            )
    except RuntimeError:
        if blocking:
            raise
        else:
            # The user has other problems
            return


# Traceback (TimeoutError)
File "/home/nerohmot/my-envs/s4/lib/python3.7/site-packages/qtconsole/base_frontend_mixin.py", line 138, in _dispatch
handler(msg)
File "/home/nerohmot/my-envs/s4/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/widgets/namespacebrowser.py", line 172, in _handle_execute_reply
self.refresh_namespacebrowser()
File "/home/nerohmot/my-envs/s4/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/widgets/namespacebrowser.py", line 63, in refresh_namespacebrowser
callback=self.set_namespace_view
File "/home/nerohmot/my-envs/s4/lib/python3.7/site-packages/spyder_kernels/comms/commbase.py", line 538, in __call__
call_dict, call_data, self._comm_id)
File "/home/nerohmot/my-envs/s4/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/comms/kernelcomm.py", line 130, in _get_call_return_value
with self.comm_channel_manager(comm_id, queue_message=not interrupt):
File "/home/nerohmot/my-envs/s4/lib/python3.7/contextlib.py", line 112, in __enter__
return next(self.gen)
File "/home/nerohmot/my-envs/s4/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/comms/kernelcomm.py", line 87, in comm_channel_manager
timeout)
File "/home/nerohmot/my-envs/s4/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/comms/kernelcomm.py", line 168, in _wait
raise TimeoutError(timeout_msg)
TimeoutError: Timeout while waiting for comm port.
Traceback (most recent call last):
File "/home/nerohmot/my-envs/s4/lib/python3.7/site-packages/qtconsole/base_frontend_mixin.py", line 138, in _dispatch
handler(msg)
File "/home/nerohmot/my-envs/s4/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/widgets/namespacebrowser.py", line 172, in _handle_execute_reply
self.refresh_namespacebrowser()
File "/home/nerohmot/my-envs/s4/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/widgets/namespacebrowser.py", line 63, in refresh_namespacebrowser
callback=self.set_namespace_view
File "/home/nerohmot/my-envs/s4/lib/python3.7/site-packages/spyder_kernels/comms/commbase.py", line 538, in __call__
call_dict, call_data, self._comm_id)
File "/home/nerohmot/my-envs/s4/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/comms/kernelcomm.py", line 130, in _get_call_return_value
with self.comm_channel_manager(comm_id, queue_message=not interrupt):
File "/home/nerohmot/my-envs/s4/lib/python3.7/contextlib.py", line 112, in __enter__
return next(self.gen)
File "/home/nerohmot/my-envs/s4/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/comms/kernelcomm.py", line 87, in comm_channel_manager
timeout)
File "/home/nerohmot/my-envs/s4/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/comms/kernelcomm.py", line 168, in _wait
raise TimeoutError(timeout_msg)
TimeoutError: Timeout while waiting for comm port.
Traceback (most recent call last):
File "/home/nerohmot/my-envs/s4/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/plugin.py", line 554, in set_current_client_working_directory
shellwidget.set_cwd(directory)
File "/home/nerohmot/my-envs/s4/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/widgets/shell.py", line 178, in set_cwd
self.call_kernel(interrupt=True).set_cwd(dirname)
File "/home/nerohmot/my-envs/s4/lib/python3.7/site-packages/spyder_kernels/comms/commbase.py", line 538, in __call__
call_dict, call_data, self._comm_id)
File "/home/nerohmot/my-envs/s4/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/comms/kernelcomm.py", line 130, in _get_call_return_value
with self.comm_channel_manager(comm_id, queue_message=not interrupt):
File "/home/nerohmot/my-envs/s4/lib/python3.7/contextlib.py", line 112, in __enter__
return next(self.gen)
File "/home/nerohmot/my-envs/s4/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/comms/kernelcomm.py", line 87, in comm_channel_manager
timeout)
File "/home/nerohmot/my-envs/s4/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/comms/kernelcomm.py", line 168, in _wait
raise TimeoutError(timeout_msg)
TimeoutError: Timeout while waiting for comm port.

# Fixed code
def _get_call_return_value(self, call_dict, call_data, comm_id):
    """
    Interupt the kernel if needed.
    """
    settings = call_dict["settings"]
    blocking = "blocking" in settings and settings["blocking"]

    if not self.kernel_client.is_alive():
        if blocking:
            raise RuntimeError("Kernel is dead")
        else:
            # The user has other problems
            logger.info("Dropping message because kernel is dead: ", str(call_dict))
            return

    settings = call_dict["settings"]
    interrupt = "interrupt" in settings and settings["interrupt"]
    interrupt = interrupt or blocking
    # Need to make sure any blocking call is replied rapidly.
    if interrupt and not self.comm_channel_connected():
        # Ask again for comm config
        self.remote_call()._send_comm_config()
        # Can not interrupt if comm not connected
        interrupt = False
        logger.debug(
            "Dropping interrupt because comm is disconnected: " + str(call_dict)
        )
        if blocking:
            raise CommError("Cannot block on a disconnected comm")
    try:
        with self.comm_channel_manager(comm_id, queue_message=not interrupt):
            return super(KernelComm, self)._get_call_return_value(
                call_dict, call_data, comm_id
            )
    except RuntimeError as e:
        if blocking:
            raise
        else:
            # The user has other problems
            logger.info(
                "Dropping message because of exception: ", str(e), str(call_dict)
            )
            return