Repository: spyder-ide/spyder

Issue:
Error while restarting the kernel
## Description

### What steps will reproduce the problem?

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

I was trying to compile a pyqt5 application. Since it failed i tried to restart the kernel.
Than this tool appeared.

### Traceback
```python-traceback
  File "C:\ProgramData\Anaconda3\lib\site-packages\psutil\_pswindows.py", line 679, in wrapper
    return fun(self, *args, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\psutil\_pswindows.py", line 868, in send_signal
    cext.proc_kill(self.pid)
PermissionError: [WinError 5] Zugriff verweigert

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\jupyter_client\manager.py", line 394, in shutdown_kernel
    self.finish_shutdown()
  File "C:\ProgramData\Anaconda3\lib\site-packages\jupyter_client\manager.py", line 346, in finish_shutdown
    self._kill_kernel()
  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\plugins\ipythonconsole\utils\manager.py", line 86, in _kill_kernel
    self.kill_proc_tree(self.kernel.pid)
  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\plugins\ipythonconsole\utils\manager.py", line 59, in kill_proc_tree
    child_process.send_signal(sig)
  File "C:\ProgramData\Anaconda3\lib\site-packages\psutil\__init__.py", line 289, in wrapper
    return fun(self, *args, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\psutil\__init__.py", line 1218, in send_signal
    self._proc.send_signal(sig)
  File "C:\ProgramData\Anaconda3\lib\site-packages\psutil\_pswindows.py", line 681, in wrapper
    raise convert_oserror(err, pid=self.pid, name=self._name)
psutil.AccessDenied: psutil.AccessDenied (pid=29604)
```

## Versions

* Spyder version: 4.1.5 
* Python version: 3.8.5
* Qt version: 5.15.1
* PyQt5 version: 5.15.1
* Operating System: Windows 10

### Dependencies

```

# Mandatory:
atomicwrites >=1.2.0           :  1.4.0 (OK)
chardet >=2.0.0                :  3.0.4 (OK)
cloudpickle >=0.5.0            :  1.6.0 (OK)
diff_match_patch >=20181111    :  20200713 (OK)
intervaltree                   :  None (OK)
IPython >=4.0                  :  7.18.1 (OK)
jedi =0.17.1                   :  0.17.1 (OK)
nbconvert >=4.0                :  6.0.7 (OK)
numpydoc >=0.6.0               :  1.1.0 (OK)
paramiko >=2.4.0               :  2.7.2 (OK)
parso =0.7.0                   :  0.7.0 (OK)
pexpect >=4.4.0                :  4.8.0 (OK)
pickleshare >=0.4              :  0.7.5 (OK)
psutil >=5.3                   :  5.7.2 (OK)
pygments >=2.0                 :  2.7.1 (OK)
pylint >=1.0                   :  2.6.0 (OK)
pyls >=0.34.0;<1.0.0           :  0.35.1 (OK)
qdarkstyle >=2.8               :  2.8.1 (OK)
qtawesome >=0.5.7              :  1.0.1 (OK)
qtconsole >=4.6.0              :  4.7.7 (OK)
qtpy >=1.5.0                   :  1.9.0 (OK)
rtree >=0.8.3                  :  0.9.4 (OK)
sphinx >=0.6.6                 :  3.2.1 (OK)
spyder_kernels >=1.9.4;<1.10.0 :  1.9.4 (OK)
watchdog                       :  None (OK)
zmq >=17                       :  19.0.2 (OK)

# Optional:
cython >=0.21                  :  0.29.21 (OK)
matplotlib >=2.0.0             :  3.3.1 (OK)
numpy >=1.7                    :  1.19.1 (OK)
pandas >=0.13.1                :  1.1.3 (OK)
scipy >=0.17.0                 :  1.5.0 (OK)
sympy >=0.7.3                  :  1.6.2 (OK)
```



Discussion:
Hey @Tneris, thanks for reporting. This looks like a valid bug.

We'll fix it for our next version (4.2.0), to be released at the beginning of November.

Resolving patch:
diff --git a/spyder/plugins/ipythonconsole/utils/manager.py b/spyder/plugins/ipythonconsole/utils/manager.py
--- a/spyder/plugins/ipythonconsole/utils/manager.py
+++ b/spyder/plugins/ipythonconsole/utils/manager.py
@@ -56,7 +56,13 @@ def kill_proc_tree(pid, sig=signal.SIGTERM, include_parent=True,
             children.append(parent)
 
         for child_process in children:
-            child_process.send_signal(sig)
+            # This is necessary to avoid an error when restarting the
+            # kernel that started a PyQt5 application in the background.
+            # Fixes spyder-ide/spyder#13999
+            try:
+                child_process.send_signal(sig)
+            except psutil.AccessDenied:
+                return ([], [])
 
         gone, alive = psutil.wait_procs(
             children,


Test patch: