Repository: spyder-ide/spyder

Issue:
Unable to restart kernel with the default interpreter with the standalone Windows installer 
<!--- **PLEASE READ:** When submitting here, please ensure you've completed the following checklist and checked the boxes to confirm. Issue reports without it may be closed. Thanks! --->

## Problem Description

Trying to restart the kernel while using the default interpreter ends in the following being print in the console:

```python-traceback
`Exception in thread Thread-8:
Traceback (most recent call last):
  File "D:\obj\windows-release\37amd64_Release\msi_python\zip_amd64\threading.py", line 926, in _bootstrap_inner
  File "D:\obj\windows-release\37amd64_Release\msi_python\zip_amd64\threading.py", line 870, in run
  File "C:\Users\dalth\AppData\Local\Programs\Spyder\pkgs\spyder_kernels\comms\frontendcomm.py", line 124, in poll_thread
    self.poll_one()
  File "C:\Users\dalth\AppData\Local\Programs\Spyder\pkgs\spyder_kernels\comms\frontendcomm.py", line 144, in poll_one
    self._comm_close(msg)
  File "C:\Users\dalth\AppData\Local\Programs\Spyder\pkgs\spyder_kernels\comms\frontendcomm.py", line 241, in _comm_close
    self.close(comm_id)
  File "C:\Users\dalth\AppData\Local\Programs\Spyder\pkgs\spyder_kernels\comms\frontendcomm.py", line 104, in close
    return super(FrontendComm, self).close(comm_id)
  File "C:\Users\dalth\AppData\Local\Programs\Spyder\pkgs\spyder_kernels\comms\commbase.py", line 173, in close
    self._comms[comm_id]['comm'].close()
  File "C:\Users\dalth\AppData\Local\Programs\Spyder\pkgs\ipykernel\comm\comm.py", line 118, in close
    self.kernel.comm_manager.unregister_comm(self)
  File "C:\Users\dalth\AppData\Local\Programs\Spyder\pkgs\ipykernel\comm\manager.py", line 54, in unregister_comm
    comm = self.comms.pop(comm.comm_id)
KeyError: 'b98c76c05e9e11ec8766f8a2d65bae71'
``` 

Although sometimes no message is printed but the kernel seems like is dead since when trying to interact with the Variable Explore a message appears:

![imagen](https://user-images.githubusercontent.com/16781833/146430956-8f72ae4a-09ba-4885-b1d8-4d889a913975.png)

Checked with a conda installed version and there seems like the kernel restart is working

### What steps reproduce the problem?

1. Try to restart the kernel of the initial IPython Console launched with Spyder

### What is the expected output? What do you see instead?


## Versions
<!--- You can get this information from Help > About Spyder...
or (if Spyder won't launch) the "conda list" command
from the Anaconda Prompt/Terminal/command line. --->


* Spyder version: 5.2.1 None
* Python version: 3.7.9 64-bit
* Qt version: 5.12.10
* PyQt5 version: 5.12.3
* Operating System: Windows 10

### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->

<details>

```
# 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                  :  None (OK)
IPython >=7.6.0               :  7.30.1 (OK)
jedi >=0.17.2;<0.19.0         :  0.18.1 (OK)
jellyfish >=0.7               :  0.8.9 (OK)
jsonschema >=3.2.0            :  4.2.1 (OK)
keyring >=17.0.0              :  23.4.0 (OK)
nbconvert >=4.0               :  6.3.0 (OK)
numpydoc >=0.6.0              :  1.1.0 (OK)
paramiko >=2.4.0              :  2.8.1 (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.8.0 (OK)
pygments >=2.0                :  2.10.0 (OK)
pylint >=2.5.0                :  2.12.2 (OK)
pyls_spyder >=0.4.0           :  0.4.0 (OK)
pylsp >=1.3.2;<1.4.0          :  1.3.3 (OK)
pylsp_black >=1.0.0           :  1.0.1 (OK)
qdarkstyle =3.0.2             :  3.0.2 (OK)
qstylizer >=0.1.10            :  0.2.1 (OK)
qtawesome >=1.0.2             :  1.1.1 (OK)
qtconsole >=5.2.1;<5.3.0      :  5.2.2 (OK)
qtpy >=1.5.0                  :  1.11.3 (OK)
rtree >=0.9.7                 :  0.9.7 (OK)
setuptools >=49.6.0           :  59.6.0 (OK)
sphinx >=0.6.6                :  4.3.1 (OK)
spyder_kernels >=2.2.0;<2.3.0 :  2.2.0 (OK)
textdistance >=4.2.0          :  4.2.2 (OK)
three_merge >=0.1.1           :  0.1.1 (OK)
watchdog                      :  2.1.6 (OK)
zmq >=17                      :  22.3.0 (OK)

# Optional:
cython >=0.21                 :  0.29.25 (OK)
matplotlib >=2.0.0            :  3.5.1 (OK)
numpy >=1.7                   :  1.19.3 (OK)
pandas >=1.1.1                :  1.3.5 (OK)
scipy >=0.17.0                :  1.7.3 (OK)
sympy >=0.7.3                 :  1.9 (OK)

```

</details>


Discussion:
Pinging @ccordoba12 and @impact27 (maybe you have some ideas of why this could be happening)
Hmm, this is bad news. What happens if you enclose this line

    spyder_kernels\comms\commbase.py", line 173

in a `try/except KeyError` and `pass` on the error? I think that should solve the problem.
> Hmm, this is bad news. What happens if you enclose this line
> 
> spyder_kernels\comms\commbase.py", line 173
> 
> in a try/except KeyError and pass on the error? I think that should solve the problem.

Checked that but the problem persist

Also checked running on debug mode and this is what I'm getting when trying to restart the kernel in the log:

```python
2021-12-17 10:50:58,728 [DEBUG] [traitlets] -> handle_close[f8e51e825f5011ecae9bf8a2d65bae71]({'header': {'msg_id': 'a9c1647a-693217bd498333ac1f3ed0a4_15000_414', 'msg_type': 'comm_close', 'username': 'username', 'session': 'a9c1647a-693217bd498333ac1f3ed0a4', 'date': datetime.datetime(2021, 12, 17, 15, 50, 58, 669705, tzinfo=tzutc()), 'version': '5.3'}, 'msg_id': 'a9c1647a-693217bd498333ac1f3ed0a4_15000_414', 'msg_type': 'comm_close', 'parent_header': {'msg_id': '6de7f0bb-83b363b4ca60eb0bd2794439_2320_155', 'msg_type': 'comm_msg', 'username': 'username', 'session': '6de7f0bb-83b363b4ca60eb0bd2794439', 'date': datetime.datetime(2021, 12, 17, 15, 50, 57, 899579, tzinfo=tzutc()), 'version': '5.3'}, 'metadata': {}, 'content': {'data': {}, 'comm_id': 'f8e51e825f5011ecae9bf8a2d65bae71'}, 'buffers': []})
2021-12-17 10:50:58,911 [DEBUG] [spyder_kernels.comms.commbase] -> Call to unconnected comm: flush_std
2021-12-17 10:50:59,922 [DEBUG] [spyder_kernels.comms.commbase] -> Call to unconnected comm: flush_std
2021-12-17 10:51:00,934 [DEBUG] [spyder_kernels.comms.commbase] -> Call to unconnected comm: flush_std
2021-12-17 10:51:01,939 [DEBUG] [spyder_kernels.comms.commbase] -> Call to unconnected comm: flush_std
2021-12-17 10:51:02,945 [DEBUG] [spyder_kernels.comms.commbase] -> Call to unconnected comm: flush_std
2021-12-17 10:51:03,945 [DEBUG] [spyder_kernels.comms.commbase] -> Call to unconnected comm: flush_std
2021-12-17 10:51:04,940 [DEBUG] [spyder_kernels.comms.commbase] -> Call to unconnected comm: flush_std
2021-12-17 10:51:05,954 [DEBUG] [spyder_kernels.comms.commbase] -> Call to unconnected comm: flush_std
2021-12-17 10:51:06,970 [DEBUG] [spyder_kernels.comms.commbase] -> Call to unconnected comm: flush_std
```
This would be solved by https://github.com/spyder-ide/spyder/pull/16890 I think

Resolving patch:
diff --git a/spyder/plugins/ipythonconsole/widgets/client.py b/spyder/plugins/ipythonconsole/widgets/client.py
--- a/spyder/plugins/ipythonconsole/widgets/client.py
+++ b/spyder/plugins/ipythonconsole/widgets/client.py
@@ -327,9 +327,9 @@ def _abort_kernel_restart(self):
         stderr = self.stderr_obj.get_contents()
         if not stderr:
             return False
-        # There is an error. If it is only about comms, ignore.
+        # There is an error. If it is benign, ignore.
         for line in stderr.splitlines():
-            if line and 'No such comm' not in line:
+            if line and not self.is_benign_error(line):
                 return True
         return False
 
@@ -394,7 +394,7 @@ def poll_std_file_change(self):
         stderr = self.stderr_obj.poll_file_change()
         starting = self.shellwidget._starting
         if stderr:
-            if self.is_bening_error(stderr):
+            if self.is_benign_error(stderr):
                 return
             if self.shellwidget.isHidden():
                 # Avoid printing the same thing again
@@ -497,7 +497,7 @@ def show_kernel_error(self, error):
         """Show kernel initialization errors in infowidget."""
         self.error_text = error
 
-        if self.is_bening_error(error):
+        if self.is_benign_error(error):
             return
 
         InstallerIPythonKernelError(error)
@@ -527,17 +527,22 @@ def show_kernel_error(self, error):
         # Tell the client we're in error mode
         self.is_error_shown = True
 
-    def is_bening_error(self, error):
+    def is_benign_error(self, error):
         """Decide if an error is benign in order to filter it."""
-        if "http://bugs.python.org/issue1666807" in error:
+        benign_errors = [
             # See spyder-ide/spyder#16828
-            return True
-
-        if "https://bugs.python.org/issue1180193" in error:
+            "This version of python seems to be incorrectly compiled",
+            "internal generated filenames are not absolute",
+            "This may make the debugger miss breakpoints",
+            "http://bugs.python.org/issue1666807",
             # See spyder-ide/spyder#16927
-            return True
+            "It seems the debugger cannot resolve",
+            "https://bugs.python.org/issue1180193",
+            # Old error
+            "No such comm"
+        ]
 
-        return False
+        return any([err in error for err in benign_errors])
 
     def get_name(self):
         """Return client name"""


Test patch: