Repository: spyder-ide/spyder

Issue:
psutil.NoSuchProcess when restarting the kernel
## Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->


    import turtle as tt

    def draw_square():
        window = tt.Screen()
        window.bgcolor('light blue')
    
        pony = tt.Turtle()
        pony.speed(1)
        pony.forward(100)
        pony.right(90)
    
        window.exitonclick()

    draw_square()
    tt.bye()

This script will have no response while the second time I run it.

### Traceback
```python-traceback
  File "C:\Users\zhour\Anaconda3\lib\site-packages\psutil\_pswindows.py", line 707, in wrapper
    return fun(self, *args, **kwargs)
  File "C:\Users\zhour\Anaconda3\lib\site-packages\psutil\_pswindows.py", line 954, in create_time
    return cext.proc_create_time(self.pid)
ProcessLookupError: [Errno 3] No such process

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\zhour\Anaconda3\lib\site-packages\psutil\__init__.py", line 474, in _init
    self.create_time()
  File "C:\Users\zhour\Anaconda3\lib\site-packages\psutil\__init__.py", line 824, in create_time
    self._create_time = self._proc.create_time()
  File "C:\Users\zhour\Anaconda3\lib\site-packages\psutil\_pswindows.py", line 709, in wrapper
    raise convert_oserror(err, pid=self.pid, name=self._name)
psutil.NoSuchProcess: psutil.NoSuchProcess process no longer exists (pid=16124)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\zhour\Anaconda3\lib\site-packages\qtconsole\manager.py", line 27, in poll
    super(QtKernelRestarter, self).poll()
  File "C:\Users\zhour\Anaconda3\lib\site-packages\jupyter_client\restarter.py", line 113, in poll
    self.kernel_manager.restart_kernel(now=True, newports=newports)
  File "C:\Users\zhour\Anaconda3\lib\site-packages\jupyter_client\manager.py", line 361, in restart_kernel
    self.shutdown_kernel(now=now, restart=True)
  File "C:\Users\zhour\Anaconda3\lib\site-packages\jupyter_client\manager.py", line 321, in shutdown_kernel
    self._kill_kernel()
  File "C:\Users\zhour\Anaconda3\lib\site-packages\spyder\plugins\ipythonconsole\utils\manager.py", line 78, in _kill_kernel
    self.kill_proc_tree(self.kernel.pid)
  File "C:\Users\zhour\Anaconda3\lib\site-packages\spyder\plugins\ipythonconsole\utils\manager.py", line 44, in kill_proc_tree
    parent = psutil.Process(pid)
  File "C:\Users\zhour\Anaconda3\lib\site-packages\psutil\__init__.py", line 447, in __init__
    self._init(pid)
  File "C:\Users\zhour\Anaconda3\lib\site-packages\psutil\__init__.py", line 487, in _init
    raise NoSuchProcess(pid, None, msg)
psutil.NoSuchProcess: psutil.NoSuchProcess no process found with pid 16124
WARNING:traitlets:kernel died: 18.000895500183105
WARNING:traitlets:kernel died: 18.000853061676025
WARNING:traitlets:kernel died: 18.000853538513184
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853061676025
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853061676025
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000852823257446
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853300094604
WARNING:traitlets:kernel died: 18.000853061676025
```

## Versions

* Spyder version: 4.1.0 
* Python version: 3.7.6
* Qt version: 5.9.6
* PyQt5 version: 5.9.2
* Operating System: Windows 10

### Dependencies

```

# Mandatory:
atomicwrites >=1.2.0           :  1.3.0 (OK)
chardet >=2.0.0                :  3.0.4 (OK)
cloudpickle >=0.5.0            :  1.3.0 (OK)
diff_match_patch >=20181111    :  20181111 (OK)
intervaltree                   :  None (OK)
IPython >=4.0                  :  7.11.1 (OK)
jedi =0.14.1                   :  0.14.1 (OK)
nbconvert >=4.0                :  5.6.1 (OK)
numpydoc >=0.6.0               :  0.9.2 (OK)
paramiko >=2.4.0               :  2.7.1 (OK)
parso =0.5.2                   :  0.5.2 (OK)
pexpect >=4.4.0                :  4.8.0 (OK)
pickleshare >=0.4              :  0.7.5 (OK)
psutil >=5.3                   :  5.6.7 (OK)
pygments >=2.0                 :  2.5.2 (OK)
pylint >=0.25                  :  2.4.4 (OK)
pyls >=0.31.2;<0.32.0          :  0.31.8 (OK)
qdarkstyle >=2.8               :  2.8 (OK)
qtawesome >=0.5.7              :  0.7.0 (OK)
qtconsole >=4.6.0              :  4.7.1 (OK)
qtpy >=1.5.0                   :  1.9.0 (OK)
rtree >=0.8.3                  :  0.9.3 (OK)
sphinx >=0.6.6                 :  2.4.0 (OK)
spyder_kernels >=1.9.0;<1.10.0 :  1.9.0 (OK)
watchdog                       :  None (OK)
zmq >=17                       :  18.1.0 (OK)

# Optional:
cython >=0.21                  :  None (OK)
matplotlib >=2.0.0             :  3.1.2 (OK)
numpy >=1.7                    :  1.18.1 (OK)
pandas >=0.13.1                :  1.0.0 (OK)
scipy >=0.17.0                 :  1.3.2 (OK)
sympy >=0.7.3                  :  None (OK)
```



Discussion:
> THIS SCRIPT WILL HAVE NO RESPONSE WHILE THE SECOND TIME I RUN IT.

Please stop using caps in your next messages. That's considered rude and not due to that we're going to help you faster.
@dalthviz could you help me and see if you can reproduce this? (I have not been able to reproduce it)
@goanpeca I was unable to reproduce this with `4.1.0` and `4.1.1`. Just in case this is my env (using Spyder `4.1.1`):

<details>

```

# packages in environment at C:\Users\Daniel\Anaconda3\envs\4v:
#
# Name                    Version                   Build  Channel
alabaster                 0.7.12                   py37_0
argh                      0.26.2                   py37_0
asn1crypto                1.3.0                    py37_0
astroid                   2.3.3                    py37_0
atomicwrites              1.3.0                    py37_1
attrs                     19.3.0                     py_0
autopep8                  1.4.4                      py_0
babel                     2.8.0                      py_0
backcall                  0.1.0                    py37_0
bcrypt                    3.1.7            py37he774522_0
blas                      1.0                         mkl
bleach                    3.1.0                    py37_0
ca-certificates           2020.1.1                      0
certifi                   2019.11.28               py37_0
cffi                      1.13.2           py37h7a1dbc1_0
chardet                   3.0.4                 py37_1003
cloudpickle               1.2.2                      py_0
colorama                  0.4.3                      py_0
cryptography              2.8              py37h7a1dbc1_0
cycler                    0.10.0                   py37_0
decorator                 4.4.1                      py_0
defusedxml                0.6.0                      py_0
diff-match-patch          20181111                   py_0
docutils                  0.15.2                   py37_0
entrypoints               0.3                      py37_0
flake8                    3.7.9                    py37_0
freetype                  2.9.1                ha9979f8_1
future                    0.18.2                   py37_0
icc_rt                    2019.0.0             h0cc432a_1
icu                       58.2                 ha66f8fd_1
idna                      2.8                      py37_0
imagesize                 1.2.0                      py_0
importlib_metadata        1.3.0                    py37_0
intel-openmp              2019.4                      245
intervaltree              3.0.2                      py_0
ipykernel                 5.1.3            py37h39e3cac_0
ipython                   7.11.1           py37h39e3cac_0
ipython_genutils          0.2.0                    py37_0
isort                     4.3.21                   py37_0
jedi                      0.15.2                   py37_0
jinja2                    2.10.3                     py_0
joblib                    0.14.1                     py_0
jpeg                      9b                   hb83a4c4_2
jsonschema                3.2.0                    py37_0
jupyter_client            5.3.4                    py37_0
jupyter_core              4.6.1                    py37_0
keyring                   20.0.0                   py37_0
kiwisolver                1.1.0            py37ha925a31_0
lazy-object-proxy         1.4.3            py37he774522_0
libpng                    1.6.37               h2a8f88b_0
libsodium                 1.0.16               h9d3ae62_0
libspatialindex           1.9.3                h33f27b4_0
markupsafe                1.1.1            py37he774522_0
matplotlib                3.1.1            py37hc8f65d3_0
mccabe                    0.6.1                    py37_1
mistune                   0.8.4            py37he774522_0
mkl                       2019.4                      245
mkl-service               2.3.0            py37hb782905_0
mkl_fft                   1.0.15           py37h14836fe_0
mkl_random                1.1.0            py37h675688f_0
more-itertools            8.0.2                      py_0
nbconvert                 5.6.1                    py37_0
nbformat                  4.4.0                    py37_0
numpy                     1.17.4           py37h4320e6b_0
numpy-base                1.17.4           py37hc3f5095_0
numpydoc                  0.9.2                      py_0
openssl                   1.1.1e               he774522_0
packaging                 20.0                       py_0
pandas                    0.25.3           py37ha925a31_0
pandoc                    2.2.3.2                       0
pandocfilters             1.4.2                    py37_1
paramiko                  2.6.0                    py37_0
parso                     0.5.2                      py_0
pathtools                 0.1.2                      py_1
pexpect                   4.7.0                    py37_0
pickleshare               0.7.5                    py37_0
pip                       19.3.1                   py37_0
pluggy                    0.13.1                   py37_0
prompt_toolkit            3.0.2                      py_0
psutil                    5.6.7            py37he774522_0
pycodestyle               2.5.0                    py37_0
pycparser                 2.19                     py37_0
pydocstyle                4.0.1                      py_0
pyflakes                  2.1.1                    py37_0
pygments                  2.5.2                      py_0
pylint                    2.4.4                    py37_0
pynacl                    1.3.0            py37h62dcd97_0
pyopenssl                 19.1.0                   py37_0
pyparsing                 2.4.6                      py_0
pyqt                      5.9.2            py37h6538335_2
pyrsistent                0.15.6           py37he774522_0
pysocks                   1.7.1                    py37_0
python                    3.7.6                h60c2a47_2
python-dateutil           2.8.1                      py_0
python-jsonrpc-server     0.3.2                      py_0
python-language-server    0.31.9                   py37_0
pytz                      2019.3                     py_0
pywin32                   227              py37he774522_0
pywin32-ctypes            0.2.0                    py37_0
pyyaml                    5.2              py37he774522_0
pyzmq                     18.1.0           py37ha925a31_0
qdarkstyle                2.8                        py_0
qt                        5.9.7            vc14h73c81de_0
qtawesome                 0.6.0                      py_0
qtconsole                 4.6.0                      py_1
qtpy                      1.9.0                      py_0
requests                  2.22.0                   py37_1
rope                      0.14.0                     py_0
rtree                     0.8.3                    py37_0
scikit-learn              0.22.1           py37h6288b17_0
scipy                     1.3.2            py37h29ff71c_0
setuptools                44.0.0                   py37_0
sip                       4.19.8           py37h6538335_0
six                       1.13.0                   py37_0
snowballstemmer           2.0.0                      py_0
sortedcontainers          2.1.0                    py37_0
sphinx                    2.3.1                      py_0
sphinxcontrib-applehelp   1.0.1                      py_0
sphinxcontrib-devhelp     1.0.1                      py_0
sphinxcontrib-htmlhelp    1.0.2                      py_0
sphinxcontrib-jsmath      1.0.1                      py_0
sphinxcontrib-qthelp      1.0.2                      py_0
sphinxcontrib-serializinghtml 1.1.3                      py_0
spyder                    4.1.1                    py37_1
spyder-kernels            1.9.0                    py37_0
sqlite                    3.30.1               he774522_0
testpath                  0.4.4                      py_0
tornado                   6.0.3            py37he774522_0
traitlets                 4.3.3                    py37_0
ujson                     1.35             py37hfa6e2cd_0
urllib3                   1.25.7                   py37_0
vc                        14.1                 h0510ff6_4
vs2015_runtime            14.16.27012          hf0eaf9b_1
watchdog                  0.9.0                    py37_1
wcwidth                   0.1.7                    py37_0
webencodings              0.5.1                    py37_1
wheel                     0.33.6                   py37_0
win_inet_pton             1.1.0                    py37_0
wincertstore              0.2                      py37_0
wrapt                     1.11.2           py37he774522_0
yaml                      0.1.7                hc54c509_2
yapf                      0.28.0                     py_0
zeromq                    4.3.1                h33f27b4_3
zipp                      0.6.0                      py_0
zlib                      1.2.11               h62dcd97_3


```

</details>

![turtle](https://user-images.githubusercontent.com/16781833/77129022-74d78300-6a20-11ea-8d59-4b686bfa593a.gif)

@WhatFishBoat how are you running the script? could you provide more details about the way the issue is showing for you? Any new info is greatly appreciated in order to reproduce this
It's confusing that I cannot reproduce this as well today. I will post new info if I meet any similar situation. Thank you guys
Thanks @WhatFishBoat!

Resolving patch:
diff --git a/spyder/plugins/ipythonconsole/plugin.py b/spyder/plugins/ipythonconsole/plugin.py
--- a/spyder/plugins/ipythonconsole/plugin.py
+++ b/spyder/plugins/ipythonconsole/plugin.py
@@ -1197,16 +1197,10 @@ def create_kernel_manager_and_kernel_client(self, connection_file,
             return (error_msg, None)
         kernel_manager._kernel_spec = kernel_spec
 
-        kwargs = {}
-        if os.name == 'nt':
-            # avoid closing fds on win+Python 3.7
-            # which prevents interrupts
-            # jupyter_client > 5.2.3 will do this by default
-            kwargs['close_fds'] = False
         # Catch any error generated when trying to start the kernel.
         # See spyder-ide/spyder#7302.
         try:
-            kernel_manager.start_kernel(stderr=stderr_handle, **kwargs)
+            kernel_manager.start_kernel(stderr=stderr_handle)
         except Exception:
             error_msg = _("The error is:<br><br>"
                           "<tt>{}</tt>").format(traceback.format_exc())
diff --git a/spyder/plugins/ipythonconsole/utils/kernelspec.py b/spyder/plugins/ipythonconsole/utils/kernelspec.py
--- a/spyder/plugins/ipythonconsole/utils/kernelspec.py
+++ b/spyder/plugins/ipythonconsole/utils/kernelspec.py
@@ -97,13 +97,6 @@ def argv(self):
         # Part of spyder-ide/spyder#11819
         is_different = is_different_interpreter(pyexec)
 
-        # Fixes spyder-ide/spyder#3427.
-        if os.name == 'nt':
-            dir_pyexec = osp.dirname(pyexec)
-            pyexec_w = osp.join(dir_pyexec, 'pythonw.exe')
-            if osp.isfile(pyexec_w):
-                pyexec = pyexec_w
-
         # Command used to start kernels
         if is_different and is_conda_env(pyexec=pyexec):
             # If this is a conda environment we need to call an intermediate
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
@@ -41,7 +41,15 @@ def kill_proc_tree(pid, sig=signal.SIGTERM, include_parent=True,
         This is an new method not present in QtKernelManager.
         """
         assert pid != os.getpid()  # Won't kill myself!
-        parent = psutil.Process(pid)
+
+        # This is necessary to avoid showing an error when restarting the
+        # kernel after it failed to start in the first place.
+        # Fixes spyder-ide/spyder#11872
+        try:
+            parent = psutil.Process(pid)
+        except psutil.NoSuchProcess:
+            return ([], [])
+
         children = parent.children(recursive=True)
 
         if include_parent:
@@ -74,7 +82,7 @@ def _kill_kernel(self):
                     self.signal_kernel(signal.SIGKILL)
                 else:
                     # This is the additional line that was added to properly
-                    # kill the spyder started kernels.
+                    # kill the kernel started by Spyder.
                     self.kill_proc_tree(self.kernel.pid)
 
                     self.kernel.kill()


Test patch:
diff --git a/spyder/plugins/ipythonconsole/tests/test_ipythonconsole.py b/spyder/plugins/ipythonconsole/tests/test_ipythonconsole.py
--- a/spyder/plugins/ipythonconsole/tests/test_ipythonconsole.py
+++ b/spyder/plugins/ipythonconsole/tests/test_ipythonconsole.py
@@ -26,8 +26,8 @@
 
 # Third party imports
 from IPython.core import release as ipy_release
+from IPython.core.application import get_ipython_dir
 from flaky import flaky
-from jupyter_client.kernelspec import KernelSpec
 from pygments.token import Name
 import pytest
 from qtpy import PYQT5
@@ -89,13 +89,6 @@ def get_conda_test_env(test_env_name=u'spytest-ž'):
     return test_env_executable
 
 
-class FaultyKernelSpec(KernelSpec):
-    """Kernelspec that generates a kernel crash"""
-
-    argv = [sys.executable, '-m', 'spyder_kernels.foo', '-f',
-            '{connection_file}']
-
-
 # =============================================================================
 # Qt Test Fixtures
 # =============================================================================
@@ -1306,16 +1299,15 @@ def test_stderr_file_remains_two_kernels(ipyconsole, qtbot, monkeypatch):
 
 
 @flaky(max_runs=3)
-@pytest.mark.skipif(not sys.platform.startswith('linux'),
-                    reason="It only works on Linux")
 def test_kernel_crash(ipyconsole, mocker, qtbot):
-    """Test that we show kernel error messages when a kernel crash occurs."""
-    # Patch create_kernel_spec method to make it return a faulty
-    # kernel spec
-    mocker.patch.object(ipyconsole, 'create_kernel_spec')
-    ipyconsole.create_kernel_spec.return_value = FaultyKernelSpec()
+    """Test that we show an error message when a kernel crash occurs."""
+    # Create an IPython kernel config file with a bad config
+    ipy_kernel_cfg = osp.join(get_ipython_dir(), 'profile_default',
+                              'ipython_kernel_config.py')
+    with open(ipy_kernel_cfg, 'w') as f:
+        # This option must be a string, not an int
+        f.write("c.InteractiveShellApp.extra_extension = 1")
 
-    # Create a new client, which will use FaultyKernelSpec
     ipyconsole.create_new_client()
 
     # Assert that the console is showing an error
@@ -1330,8 +1322,12 @@ def test_kernel_crash(ipyconsole, mocker, qtbot):
         webpage = webview.page()
     else:
         webpage = webview.page().mainFrame()
-    qtbot.waitUntil(lambda: check_text(webpage, "No module named"),
-                    timeout=6000)
+    qtbot.waitUntil(
+        lambda: check_text(webpage, "Bad config encountered"),
+        timeout=6000)
+
+    # Remove bad kernel config file
+    os.remove(ipy_kernel_cfg)
 
 
 @pytest.mark.skipif(not os.name == 'nt', reason="Only works on Windows")