Repository: spyder-ide/spyder

Issue:
`psutil.NoSuchProcess` is raised when killing kernel
## Description

### What steps will reproduce the problem?

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

When I run Spyder it sometimes crush without a valid reason. 

### Traceback
```python-traceback
Traceback (most recent call last):
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\asyncio\tasks.py", line 234, in __step
    result = coro.throw(exc)
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\jupyter_client\manager.py", line 636, in _async_wait
    await asyncio.sleep(pollinterval)
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\asyncio\tasks.py", line 605, in sleep
    return await future
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\asyncio\futures.py", line 285, in __await__
    yield self  # This tells Task to wait for completion.
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\asyncio\tasks.py", line 304, in __wakeup
    future.result()
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\asyncio\futures.py", line 196, in result
    raise exc
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\asyncio\tasks.py", line 456, in wait_for
    return fut.result()
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\asyncio\futures.py", line 196, in result
    raise exc
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\jupyter_client\manager.py", line 424, in _async_finish_shutdown
    await asyncio.wait_for(
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\asyncio\tasks.py", line 458, in wait_for
    raise exceptions.TimeoutError() from exc
asyncio.exceptions.TimeoutError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\spyder\plugins\ipythonconsole\widgets\client.py", line 768, in _restart_thread_main
    self.shellwidget.kernel_manager.restart_kernel(
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\jupyter_client\utils.py", line 30, in wrapped
    raise e
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\jupyter_client\utils.py", line 27, in wrapped
    return loop.run_until_complete(future)
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\nest_asyncio.py", line 90, in run_until_complete
    return f.result()
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\asyncio\futures.py", line 201, in result
    raise self._exception.with_traceback(self._exception_tb)
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\asyncio\tasks.py", line 232, in __step
    result = coro.send(None)
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\jupyter_client\manager.py", line 535, in _async_restart_kernel
    await ensure_async(self.shutdown_kernel(now=now, restart=True))
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\jupyter_client\utils.py", line 30, in wrapped
    raise e
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\jupyter_client\utils.py", line 27, in wrapped
    return loop.run_until_complete(future)
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\nest_asyncio.py", line 90, in run_until_complete
    return f.result()
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\asyncio\futures.py", line 201, in result
    raise self._exception.with_traceback(self._exception_tb)
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\asyncio\tasks.py", line 232, in __step
    result = coro.send(None)
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\jupyter_client\manager.py", line 83, in wrapper
    raise e
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\jupyter_client\manager.py", line 75, in wrapper
    out = await method(self, *args, **kwargs)
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\jupyter_client\manager.py", line 498, in _async_shutdown_kernel
    await ensure_async(self.finish_shutdown(restart=restart))
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\jupyter_client\utils.py", line 30, in wrapped
    raise e
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\jupyter_client\utils.py", line 27, in wrapped
    return loop.run_until_complete(future)
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\nest_asyncio.py", line 90, in run_until_complete
    return f.result()
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\asyncio\futures.py", line 201, in result
    raise self._exception.with_traceback(self._exception_tb)
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\asyncio\tasks.py", line 232, in __step
    result = coro.send(None)
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\jupyter_client\manager.py", line 430, in _async_finish_shutdown
    await ensure_async(self._send_kernel_sigterm())
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\jupyter_client\utils.py", line 30, in wrapped
    raise e
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\jupyter_client\utils.py", line 27, in wrapped
    return loop.run_until_complete(future)
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\nest_asyncio.py", line 90, in run_until_complete
    return f.result()
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\asyncio\futures.py", line 201, in result
    raise self._exception.with_traceback(self._exception_tb)
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\asyncio\tasks.py", line 232, in __step
    result = coro.send(None)
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\spyder\plugins\ipythonconsole\utils\manager.py", line 118, in _async_send_kernel_sigterm
    await self.kill_proc_tree(self.provisioner.process.pid)
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\spyder\plugins\ipythonconsole\utils\manager.py", line 67, in kill_proc_tree
    child_process.send_signal(sig)
  File "C:\Users\SX578CX\AppData\Local\anaconda3\lib\site-packages\psutil\__init__.py", line 277, in wrapper
    raise NoSuchProcess(self.pid, self._name, msg=msg)
psutil.NoSuchProcess: process no longer exists (pid=30804)
```

## Versions

* Spyder version: 5.4.1  (conda)
* Python version: 3.10.9 64-bit
* Qt version: 5.15.2
* PyQt5 version: 5.15.7
* Operating System: Windows 10

### 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;<9.0.0       :  8.10.0 (OK)
jedi >=0.17.2;<0.19.0         :  0.18.1 (OK)
jellyfish >=0.7               :  0.9.0 (OK)
jsonschema >=3.2.0            :  4.17.3 (OK)
keyring >=17.0.0              :  23.4.0 (OK)
nbconvert >=4.0               :  6.5.4 (OK)
numpydoc >=0.6.0              :  1.5.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.9.0 (OK)
pygments >=2.0                :  2.11.2 (OK)
pylint >=2.5.0;<3.0           :  2.16.2 (OK)
pylint_venv >=2.1.1           :  2.3.0 (OK)
pyls_spyder >=0.4.0           :  0.4.0 (OK)
pylsp >=1.7.0;<1.8.0          :  1.7.1 (OK)
pylsp_black >=1.2.0           :  1.2.1 (OK)
qdarkstyle >=3.0.2;<3.1.0     :  3.0.2 (OK)
qstylizer >=0.2.2             :  0.2.2 (OK)
qtawesome >=1.2.1             :  1.2.2 (OK)
qtconsole >=5.4.0;<5.5.0      :  5.4.0 (OK)
qtpy >=2.1.0                  :  2.2.0 (OK)
rtree >=0.9.7                 :  1.0.1 (OK)
setuptools >=49.6.0           :  65.6.3 (OK)
sphinx >=0.6.6                :  5.0.2 (OK)
spyder_kernels >=2.4.1;<2.5.0 :  2.4.1 (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)
zmq >=22.1.0                  :  23.2.0 (OK)

# Optional:
cython >=0.21                 :  None (NOK)
matplotlib >=3.0.0            :  3.7.0 (OK)
numpy >=1.7                   :  1.23.5 (OK)
pandas >=1.1.1                :  1.5.3 (OK)
scipy >=0.17.0                :  1.10.0 (OK)
sympy >=0.7.3                 :  1.11.1 (OK)
```



Discussion:
Hey @Enrik9294, thanks for reporting. I don't understand very well what happened, but we'll catch the error you reported in our next version (5.4.4), so it's not shown to users. That version will be released in a couple of weeks.

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
@@ -60,12 +60,16 @@ async def kill_proc_tree(pid, sig=signal.SIGTERM, include_parent=True,
             children.append(parent)
 
         for child_process in children:
-            # This is necessary to avoid an error when restarting the
-            # kernel that started a PyQt5 application in the background.
+            # This is necessary to avoid an error when restarting the kernel
+            # that started a PyQt5 application in the background. It also fixes
+            # a problem when some of the kernel children are not available
+            # anymore, probably because they were removed by the OS before this
+            # method is able to run.
+            # Fixes spyder-ide/spyder#21012
             # Fixes spyder-ide/spyder#13999
             try:
                 child_process.send_signal(sig)
-            except psutil.AccessDenied:
+            except (psutil.AccessDenied, psutil.NoSuchProcess):
                 return ([], [])
 
         gone, alive = psutil.wait_procs(


Test patch: