ipython: 'Unhandled exception in event loop' (WinError 995)

I started using ipython recently. Now I often get this message upon doing any action, even simple ones like defining variables:

Unhandled exception in event loop:
  File "c:\users\USER\appdata\local\programs\python\python38-32\lib\asyncio\proactor_events.py", line 768, in _loop_self_reading
    f.result()  # may raise
  File "c:\users\USER\appdata\local\programs\python\python38-32\lib\asyncio\windows_events.py", line 808, in _poll
    value = callback(transferred, key, ov)
  File "c:\users\USER\appdata\local\programs\python\python38-32\lib\asyncio\windows_events.py", line 457, in finish_recv
    raise ConnectionResetError(*exc.args)

Exception [WinError 995] The I/O operation has been aborted because of either a thread exit or an application request
Press ENTER to continue...

This message sometimes shows up, sometimes not, for the same command (Like xyz = “hello”). Have no idea what I did wrong. The normal interpreter doesn’t have this problem. Using 3.8 and installed ipython 7.10.2 through the cmd.

Thanks.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 54
  • Comments: 49 (10 by maintainers)

Most upvoted comments

After some investigation, I think it is a bug in Windows platform implementation of Python’s asyncio:

Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on Windows 10 Version 1809 (OS Build 17763.1039)

  1. Add to file <path_to_python>\Lib\asyncio\proactor_events.py in function BaseProactorEventLoop._loop_self_reading after line 768 the following code:

766....try: 767........if f is not None: 768............f.result() # may raise ...........if self._stopping: ...............raise exceptions.CancelledError("Event loop is stopping") 769........f = self._proactor.recv(self._ssock, 4096)

  1. Test. I have no more errors…

EDIT: you can just use if self._stopping: return instead in this particular code context (should be a bit faster)

EDIT2: Bug on bugs.python.org and description of what is causing this error (found by someone else): https://bugs.python.org/issue39010#msg362076

adding code to proactor_events.py (as MrEightFive suggested) didn’t help but pip install --upgrade prompt-toolkit==2.0.10 did . Win10 Python38.

Can you try to downgrade prompt toolkit to 2.x to see if this is the cause ?

I did downgrade to prompt toolkit 2.0.10 and it worked. Thank you so much.

pip install prompt-toolkit==2.0.10

Prompt_toolkit 3 (the library that allows IPython to have multi-line editing and syntactic coloration) was released just before IPython 7.10; it tries to play nicer with asyncio eventloop if you do some concurrent programming. We’ve done our best to make IPython compatible with prompt toolkit 3, but there are a couple of edge case we might not have though about.

Everything should work fine with prompt_toolkit 2.x, and we’ll try to figure out why you got the above error.

With your current error it is hard to know where exactly this is coming from, but hopefully in the next few release we’ll narrow down the reason.

pip install --upgrade prompt-toolkit==2.0.10 This worked for me on Windows Anaconda3-2020.11 version.

adding code to proactor_events.py (as MrEightFive suggested) didn’t help but pip install --upgrade prompt-toolkit==2.0.10 did . Win10 Python38.

That worked for me.

@segevfiner I’m not a member of CPython development group, and according to their document there is an (extensive) process of verification before they let you in. Not worth for me to go through for a single pull request, though I would have made it if it was easier.

Second, bug report for this already exists on bugs.python.org (see EDIT2 in my post above), under different title, but the reason for this bug is stated exactly as I have found out. So I guess not necessary to duplicate.

Third, though my workaround works, I have no guarantee that it has no side effects (it has some dependencies as of how shutdown of event loop is implemented in that particular version of asyncio, and that might change). I ran CPython’s asyncio tests (on 3.8.1), and they had passed, but I never investigated beyond that. It is better if someone actually working on asyncio and well versed in CPython code would verify it.

Hi @musm, this is one of the downsides of using the full Anaconda distribution. There are so many modules that it can be difficult to resolve all of their dependencies simultaneously if you stray from the provided distribution. You might try Miniconda instead, which is Conda, but without the pre-installed modules. With it, you create environments to include just the modules you need for your project.

I haven’t tried this, but it’s possible you may also be able to do this without having to uninstall Anaconda and install Miniconda instead. I.E. Create a new environment from scratch that includes only specific modules. Try it with a command like the following (edit the module names to include what you really need).

conda create -n <myenv> python ipython prompt_toolkit=2 jupyter pandas numpy scipy scikit-learn matplotlib

BTW it’s VERY difficult to modify proactor_events.py if python was installed from MS Store.

图片

I changed the owner to me and set me and “everyone” to full controll. But I still can’t edit the file, even in Safe mode and “retry as admin”.

Any idea when this is going to be solved?