Repository: spyder-ide/spyder

Issue:
RuntimeError when trying to interrupt a dead kernel
## Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->


Running the python script with <F5> causes the runfile function to freeze.

### Traceback
```python-traceback
Traceback (most recent call last):
  File "/home/dj3l4l/anaconda3/envs/py39/lib/python3.10/site-packages/spyder/plugins/ipythonconsole/widgets/debugging.py", line 714, in eventFilter
    return super().eventFilter(obj, event)
  File "/home/dj3l4l/anaconda3/envs/py39/lib/python3.10/site-packages/qtconsole/console_widget.py", line 430, in eventFilter
    return self._event_filter_console_keypress(event)
  File "/home/dj3l4l/anaconda3/envs/py39/lib/python3.10/site-packages/spyder/plugins/ipythonconsole/widgets/debugging.py", line 690, in _event_filter_console_keypress
    self)._event_filter_console_keypress(event)
  File "/home/dj3l4l/anaconda3/envs/py39/lib/python3.10/site-packages/qtconsole/frontend_widget.py", line 359, in _event_filter_console_keypress
    self.request_interrupt_kernel()
  File "/home/dj3l4l/anaconda3/envs/py39/lib/python3.10/site-packages/qtconsole/frontend_widget.py", line 342, in request_interrupt_kernel
    self.interrupt_kernel()
  File "/home/dj3l4l/anaconda3/envs/py39/lib/python3.10/site-packages/spyder/plugins/ipythonconsole/widgets/shell.py", line 259, in interrupt_kernel
    super(ShellWidget, self).interrupt_kernel()
  File "/home/dj3l4l/anaconda3/envs/py39/lib/python3.10/site-packages/qtconsole/frontend_widget.py", line 643, in interrupt_kernel
    self.kernel_manager.interrupt_kernel()
  File "/home/dj3l4l/anaconda3/envs/py39/lib/python3.10/site-packages/jupyter_client/utils.py", line 30, in wrapped
    raise e
  File "/home/dj3l4l/anaconda3/envs/py39/lib/python3.10/site-packages/jupyter_client/utils.py", line 27, in wrapped
    return loop.run_until_complete(future)
  File "/home/dj3l4l/anaconda3/envs/py39/lib/python3.10/site-packages/nest_asyncio.py", line 89, in run_until_complete
    return f.result()
  File "/home/dj3l4l/anaconda3/envs/py39/lib/python3.10/asyncio/futures.py", line 201, in result
    raise self._exception.with_traceback(self._exception_tb)
  File "/home/dj3l4l/anaconda3/envs/py39/lib/python3.10/asyncio/tasks.py", line 232, in __step
    result = coro.send(None)
  File "/home/dj3l4l/anaconda3/envs/py39/lib/python3.10/site-packages/jupyter_client/manager.py", line 605, in _async_interrupt_kernel
    raise RuntimeError("Cannot interrupt kernel. No kernel is running!")
RuntimeError: Cannot interrupt kernel. No kernel is running!
```

## Versions

* Spyder version: 5.3.3  (conda)
* Python version: 3.10.8 64-bit
* Qt version: 5.15.2
* PyQt5 version: 5.15.7
* Operating System: Linux 5.15.0-56-generic

### Dependencies

```
# Mandatory:
atomicwrites >=1.2.0          :  1.4.0 (OK)
chardet >=2.0.0               :  4.0.0 (OK)
cloudpickle >=0.5.0           :  2.0.0 (OK)
cookiecutter >=1.6.0          :  1.7.3 (OK)
diff_match_patch >=20181111   :  20200713 (OK)
intervaltree >=3.0.2          :  3.1.0 (OK)
IPython >=7.31.1;<8.0.0       :  7.31.1 (OK)
jedi >=0.17.2;<0.19.0         :  0.18.1 (OK)
jellyfish >=0.7               :  0.9.0 (OK)
jsonschema >=3.2.0            :  4.16.0 (OK)
keyring >=17.0.0              :  23.4.0 (OK)
nbconvert >=4.0               :  6.5.4 (OK)
numpydoc >=0.6.0              :  1.5.0 (OK)
parso >=0.7.0;<0.9.0          :  0.8.3 (OK)
pexpect >=4.4.0               :  4.8.0 (OK)
pickleshare >=0.4             :  0.7.5 (OK)
psutil >=5.3                  :  5.9.0 (OK)
pygments >=2.0                :  2.11.2 (OK)
pylint >=2.5.0;<3.0           :  2.14.5 (OK)
pyls_spyder >=0.4.0           :  0.4.0 (OK)
pylsp >=1.5.0;<1.6.0          :  1.5.0 (OK)
pylsp_black >=1.2.0           :  1.2.1 (OK)
qdarkstyle >=3.0.2;<3.1.0     :  3.0.2 (OK)
qstylizer >=0.1.10            :  0.1.10 (OK)
qtawesome >=1.0.2             :  1.0.3 (OK)
qtconsole >=5.3.2;<5.4.0      :  5.3.2 (OK)
qtpy >=2.1.0                  :  2.2.0 (OK)
rtree >=0.9.7                 :  0.9.7 (OK)
setuptools >=49.6.0           :  65.5.0 (OK)
sphinx >=0.6.6                :  5.0.2 (OK)
spyder_kernels >=2.3.3;<2.4.0 :  2.3.3 (OK)
textdistance >=4.2.0          :  4.2.1 (OK)
three_merge >=0.1.1           :  0.1.1 (OK)
watchdog >=0.10.3             :  2.1.6 (OK)
xdg >=0.26                    :  0.27 (OK)
zmq >=22.1.0                  :  23.2.0 (OK)

# Optional:
cython >=0.21                 :  None (NOK)
matplotlib >=3.0.0            :  3.6.2 (OK)
numpy >=1.7                   :  1.23.4 (OK)
pandas >=1.1.1                :  1.5.2 (OK)
scipy >=0.17.0                :  1.9.3 (OK)
sympy >=0.7.3                 :  None (NOK)
```



Discussion:
Hey @dj3l4l, thanks for reporting. It seems you started to debug some code and at some point that caused a kernel crash. That's why you can't stop the debugging session by interrupting the kernel.

We'll catch this error and post an appropriate message in our next version (5.4.1), to be released in two weeks. In the meantime, please start a new console and don't report this error if you encounter it again.
Ah I see, thanks so much for the help!
-------- Original Message --------
On 16 Dec 2022, 16:20, Carlos Cordoba wrote:

> Hey ***@***.***(https://github.com/dj3l4l), thanks for reporting. It seems you started to debug some code and at some point that caused a kernel crash. That's why you can't stop the debugging session by interrupting the kernel.
>
> We'll catch this error and post an appropriate message in our next version (5.4.1), to be released in two weeks. In the meantime, please start a new console and don't report this error if you encounter it again.
>
> —
> Reply to this email directly, [view it on GitHub](https://github.com/spyder-ide/spyder/issues/20212#issuecomment-1355165133), or [unsubscribe](https://github.com/notifications/unsubscribe-auth/AWEDIOXTGIOAPND4VU6Y3VDWNSJFRANCNFSM6AAAAAATBCVPOQ).
> You are receiving this because you were mentioned.Message ID: ***@***.***>

Resolving patch:
diff --git a/spyder/plugins/ipythonconsole/widgets/shell.py b/spyder/plugins/ipythonconsole/widgets/shell.py
--- a/spyder/plugins/ipythonconsole/widgets/shell.py
+++ b/spyder/plugins/ipythonconsole/widgets/shell.py
@@ -256,7 +256,17 @@ def interrupt_kernel(self):
         # Empty queue when interrupting
         # Fixes spyder-ide/spyder#7293.
         self._execute_queue = []
-        super(ShellWidget, self).interrupt_kernel()
+
+        # Check if there is a kernel that can be interrupted before trying to
+        # do it.
+        # Fixes spyder-ide/spyder#20212
+        if self.kernel_manager.has_kernel:
+            super(ShellWidget, self).interrupt_kernel()
+        else:
+            self._append_html(
+                _("The kernel appears to be dead, so it can't be interrupted. "
+                  "Please open a new console to keep working.")
+            )
 
     def execute(self, source=None, hidden=False, interactive=False):
         """
diff --git a/spyder/widgets/collectionseditor.py b/spyder/widgets/collectionseditor.py
--- a/spyder/widgets/collectionseditor.py
+++ b/spyder/widgets/collectionseditor.py
@@ -1870,7 +1870,6 @@ def __init__(self):
             'float16_scalar': np.float16(16),
             'float32_scalar': np.float32(32),
             'float64_scalar': np.float64(64),
-            'bool_scalar': np.bool(8),
             'bool__scalar': np.bool_(8),
             'timestamp': test_timestamp,
             'timedelta_pd': test_pd_td,


Test patch:
diff --git a/spyder/plugins/variableexplorer/widgets/tests/test_dataframeeditor.py b/spyder/plugins/variableexplorer/widgets/tests/test_dataframeeditor.py
--- a/spyder/plugins/variableexplorer/widgets/tests/test_dataframeeditor.py
+++ b/spyder/plugins/variableexplorer/widgets/tests/test_dataframeeditor.py
@@ -648,7 +648,7 @@ def test_dataframemodel_set_data_bool(monkeypatch):
                      '.dataframeeditor.QMessageBox')
     monkeypatch.setattr(attr_to_patch, MockQMessageBox)
 
-    test_params = [numpy.bool_, numpy.bool, bool]
+    test_params = [numpy.bool_, bool]
     test_strs = ['foo', 'false', 'f', '0', '0.', '0.0', '', ' ']
     expected_df = DataFrame([1, 0, 0, 0, 0, 0, 0, 0, 0], dtype=bool)
 
@@ -676,7 +676,7 @@ def test_dataframeeditor_edit_bool(qtbot, monkeypatch):
                      '.dataframeeditor.QMessageBox')
     monkeypatch.setattr(attr_to_patch, MockQMessageBox)
 
-    test_params = [numpy.bool_, numpy.bool, bool]
+    test_params = [numpy.bool_, bool]
     test_strs = ['foo', 'false', 'f', '0', '0.', '0.0', '', ' ']
     expected_df = DataFrame([1, 0, 0, 0, 0, 0, 0, 0, 0], dtype=bool)