Repository: spyder-ide/spyder

Issue:
Stop button in the IPython console is not working with Python 3.7 on Windows
<!--- **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! --->

### Issue Report Checklist

* [ ] Searched the [issues page](https://github.com/spyder-ide/spyder/issues?q=is%3Aissue) for similar reports
* [ ] Read the relevant sections of the [Spyder Troubleshooting Guide](https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ) and followed its advice
* [ ] Reproduced the issue after updating with ``conda update spyder`` (or ``pip``, if not using Anaconda)
* [ ] Could not reproduce inside ``jupyter qtconsole`` (if console-related)
* [ ] Tried basic troubleshooting (if a bug/error)
    * [ ] Restarted Spyder
    * [ ] Reset preferences with ``spyder --reset``
    * [ ] Reinstalled the latest version of [Anaconda](https://www.anaconda.com/download/)
    * [ ] Tried the other applicable steps from the Troubleshooting Guide
* [ ] Completed the **Problem Description**, **Steps to Reproduce** and **Version** sections below


## Problem Description

I use the anaconda 5.3,and I update all in the conda(conda update --all)
And I found the bug in the spyder,Whether or not I upgrade
My English is Terrible,so....
I don't know if I have clearly expressed my question.

like this code:
for i in range(0,10000):
    print(i)

I click the button of stop the current command（the red one）,but it doesn't work

### What steps reproduce the problem?

1. run .py
2. Run script will run all the time
3. Spyder can't stop until finish

### What is the expected output? What do you see instead?



### Paste Traceback/Error Below (if applicable)
<!--- Copy from error dialog or View > Panes > Internal Console --->

```python-traceback

PASTE TRACEBACK HERE

```

## 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: 3.3.1
* Python version: 3.7.0
* Qt version: QtConsole 4.4.1
* PyQt version: IPython 7.0.1
* Operating System name/version: 

### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->

```
PASTE DEPENDENCIES HERE
```



Discussion:
Win10 64bits
Please downgrade to IPython 6 and ipykernel 4.10 to see what happens.
I downgrade ipykernel from 5.0.0 to 4.10.0,but Ipython can't downgrade
Not working....

but the anaconda 5.2 can work,
update --all,,,
python 3.6.6,
spyder 3.3.1
It can work

but I have obsessive,
I like Latest version......

So....
Sorry...
> but I have obsessive,
I like Latest version

This is a bad idea. You should update only when necessary or perhaps twice per year. Else, you'll find errors like the one you just reported.
@CAM-Gerlach, are you able to reproduce this?
@ccordoba12 Updating to Anaconda 5.3 now to check...
Thanks!
@ccordoba12 I wasn't able to update ``base`` to ``anaconda`` 5.3.0 at the moment since I have several Spyder instances currently open, but I could not reproduce this running the code and hitting the stop button on Windows 8.1 x64 in Spyder 3.3.1 after updating to IPython 7.0.1 and IPyKernel 5.0.0 in a separate Spyder env on Python 3.7.0. a ``KeyboardInterrupt`` was raised and execution stopped as expected.
That was my experience on Linux too.
@ccordoba12 @CAM-Gerlach 
Thanks！
 @ccordoba12 @CAM-Gerlach Is this bug just happening to me?
@Ares8947 Perhaps, but more likely its just another package than the two obvious suspects we tested, ``ipython`` and ``pykernel``. It could be something related, like ``tornado``, ``pyzmq``/``zeromq``, ``jupyter_client``, ``traitlets``, or even a dependency thereof. It could be anything that ``conda update --all`` updated, which amounts to dozens and dozens of packages at least, and given the number of combinations of packages and versions is at the very least in the thousands, it may be very difficult to find simply be trial and error without recreating your exact environment. That's why the ``anaconda`` metapackage exists and why ``conda update anaconda`` or ``conda install anaconda=X.Y.Z`` is recommended update method, and/or just updating individual packages like Spyder that don't have many reverse dependencies and that you specifically need.

What I recommend you do is run ``conda install anaconda=5.3`` and see if the issue persists. If it does, then we can properly confirm if we can reproduce it on our end or not. Thanks.
@CAM-Gerlach  I follow your advice,conda install anaconda=5.3,and reset spyder setting.But the problems still exist.
<img src=http://thyrsi.com/t6/384/1539064833x1822611383.gif />
<img src=http://thyrsi.com/t6/384/1539065022x-1566688371.gif />
<img src=http://thyrsi.com/t6/384/1539065047x-1566688371.gif />

@ccordoba12 @CAM-Gerlach 
> [img]http://thyrsi.com/t6/384/1539064833x1822611383.gif[/img]

This is Github, not a forum, so BBcode won't work here. However, Markdown will, and you can simply copy and paste the image here directly.

EDIT: I see you did, great.
@CAM-Gerlach Thank you for your time and patience.    ：）
Anaconda 5.2 python 3.6.6 spyder 3.3.1,The same problem will not arise.
So....I am very sorry to waste your time.
Thanks!
@Ares8947 Ares8947 Okay, thanks. I'll boot up my Windows 10 test machine, update an untouched ``base`` env to Anaconda 5.3 and see if I can reproduce. For the record, you didn't install any other packages other than those already installed in your ``base`` environment, correct? Can you also try creating a new environment (``conda create -n spyder-test-env python=3.7``), ``activate`` it, ``conda install spyder``, and see if it occurs there if you run ``spyder``? 
> Anaconda 5.2 python 3.6.6 spyder 3.3.1,The same problem will not arise. So....I am very sorry to waste your time. Thanks!

You're not wasting our time; you're helping us isolate this issue this it could happen to others. You've already mentioned that this is the case, so I'm not sure how that would invalidate your issue. It may be an hour or so before my machine downloads and installs everything, since I don't regularly update it.

Once you've tried the above, could you try switching python to 3.7 in your current Anaconda 5.2 setup, see if it reoccurs, then upgrade to Anaconda 3 and try Python 3.6 there? Just trying to isolate another variable. If that doesn't change anything, its likely a specific package that's the issue.
I am trying.....
<img src=http://thyrsi.com/t6/384/1539066375x-1566680853.gif />

<img src=http://thyrsi.com/t6/384/1539066771x-1566681057.gif />
<img src=http://thyrsi.com/t6/384/1539066792x-1566680889.gif />

It is still.....
and I use the Python3.6 in another computer just now...
But maybe I have Obsession.Just like the latest version,Sorry....
@CAM-Gerlach 

Okay, that's what I expected for that test, thanks, but it does rule out any other packages being the issue other than Spyder's direct dependencies. Are you able to test ``anaconda`` 5.2 + python 3.7, and ``anaconda`` 5.3 + python 3.6? That will rule out any differences between Python versions.
@CAM-Gerlach Can you tell me what should I do?
Like how to create anaconda 5.2 + python 3.7 envs？
Sorry I am not a computer major.
> Like how to create anaconda 5.2 + python 3.7 envs？

If you're sufficiently tolerant of risk that you do ``conda update --all`` on your ``base`` environment on a regular basis, then you should have no problem making the changes there without creating a new environment. As you did before, just tell ``conda`` to install a specific version of a package—in this case, Python, with ``conda install python=3.7`` in your environment with ``anaconda`` 5.2. You can then upgrade it to anaconda 5.3 by the same means you downgraded it, and downgrade python by the same means you updated it.

A stupid permissions issue at the very end of the install process means I've got to elevate as admin and redo the entire thing, including all the downloading, so it'll be as much time as its already been to confirm whether I can reproduce your results running a clean ``anaconda`` 5.3. However, I will also test in a clean Python 3.7/Spyder 3.3.1-only environment which should come back faster and cleanly reproduce your results. You can also try downgrading Python in that environment, with ``conda install python=3.6`` after activating it, and see if the problem reoccurs.
@ccordoba12 Okay, so I tested a clean environment with just a fresh install of Spyder 3.3.1 and Python 3.7, and indeed I was able to reproduce the error. However, downgrading to Python 3.6 with the same versions of everything did not result in an error. Therefore, it seems the error is specific to Python 3.7, either due to its interactions with Spyder itself, or one of its dependencies. However, I have no real idea which of the [many changes in Python 3.7](https://docs.python.org/3/whatsnew/3.7.html) it could be.
 @CAM-Gerlach By the way,How to locate variables quickly in the variables Explorer，Like Ctrl + F....

> @CAM-Gerlach By the way,How to locate variables quickly in the variables Explorer，Like Ctrl + F....

Not related to this issue, but currently just by filtering (using the "gear" menu). However, there has been talk of adding one, either one filtering on everything, one per column, or both.
@CAM-Gerlach I know use the filtering,but even filtering still has too many variables.
So,I recommend adding a function to search all variables.

By the way,Is that Bug complicated?
> So,I recommend adding a function to search all variables.

We agree, we're planning on it. Hopefully it will be in the full release of Spyder 4.

> By the way,Is that Bug complicated?

Which bug is this?
@CAM-Gerlach The bug of Stop button in the IPython console is not working with ipykernel 5.0+
Oh, so you mean *this* bug? I don't know, since I'm not sure what's causing it beyond being something with Python 3.7 vs. 3.6, and even that could be related to Spyder's code or one of our many dependencies. If anyone would know, its @ccordoba12 .
I found the v4.0beta2 .
Oh,I am very much looking forward to the arrival of the new version.
Thank you for your developers for us.
I think it should be the cause of python3.7 @CAM-Gerlach @ccordoba12 

> I found the v4.0beta2

The current beta is beta 1, so I'm not sure how you found beta 2 since its doens't exist yet :)

> I think it should be the cause of python3.7

?
@CAM-Gerlach 
I use Google translate...
：(
I have to sleep,Our time is 1:35 am,,,,,ZZZZzzzzzzz
Good night....
Good morning for you?
Good afternoon for you?

..........ZZZZZzzzzz
I look forward to solving this bug....
Thanks!
@ccordoba12 @CAM-Gerlach 
Now I have upgraded to the latest version
anaconda 5.3.1
spyder 3.3.2
The problem remains unresolved
:(
This is not something we can solve in our side, sorry.
Oh,god....
I'm used to using spyder.
I like the variable explorer..
I'm sorry to hear that.
@ccordoba12 

This Bug is a common phenomenon, not just for me.
@ccordoba12 
@ccordoba12 Okay, then should I report it to ``ipykernel``? I was able to repro it in QtConsole (4.4.1 and 4.4.2 tested): Pressing Ctrl-C or Kernel > Interrupt during ``time.sleep(10)`` (eventually) produces a ``KeyboardInterrupt`` traceback and stops the command in Python 3.6, while not doing so in Python 3.7. I tested under several different versions of QtConsole, IPykernel, and IPython for both and identical versions of Jupyter_Client and Jupyter_core.
@CAM-Gerlach, yes, if you were able to reproduce this problem in QtConsole, then it's an ipykernel bug. Please ping me and minrk, and reference this issue when reporting.

Does it help to downgrade that package to 4.10?

Thanks, I'll report it.

> Does it help to downgrade that package to 4.10?

Nope, makes no difference, and neither does 4.8.2 which I tested both previously way back when this was first reported and now. Nor does downgrading IPython to 6.x as well, according to my previous testing.
@ccordoba12 Reported there as ipython/ipykernel#374 . Also, I wasn't able to reproduce it on my Fedora 28 Linux VM under various versions on Python 3.7 (and the interrupt was processed vastly quicker than on Windows either way), so it appears to be a Windows-specific bug.
Thank you for your help to Windows users.
Thanks!!!!
@ccordoba12 @CAM-Gerlach 

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
@@ -1549,10 +1549,16 @@ 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 issue 7302
         try:
-            kernel_manager.start_kernel(stderr=stderr_handle)
+            kernel_manager.start_kernel(stderr=stderr_handle, **kwargs)
         except Exception:
             error_msg = _("The error is:<br><br>"
                           "<tt>{}</tt>").format(traceback.format_exc())


Test patch: