Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Unhandled exception in event loop' (WinError 995) #12049

Open
chandagnac opened this issue Dec 16, 2019 · 49 comments
Open

'Unhandled exception in event loop' (WinError 995) #12049

chandagnac opened this issue Dec 16, 2019 · 49 comments

Comments

@chandagnac
Copy link

chandagnac commented Dec 16, 2019

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.

@Carreau
Copy link
Member

Carreau commented Dec 16, 2019

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

@chandagnac
Copy link
Author

I did downgrade to prompt toolkit 2.0.10 and it worked. Thank you so much.
Could you please say, as I'm completely new to this, what changed?
And could I have problems because of reversing the version?

@Carreau
Copy link
Member

Carreau commented Dec 16, 2019

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.

@superzarzar
Copy link

Any idea when this is going to be solved?

@Carreau
Copy link
Member

Carreau commented Feb 3, 2020

When someone find the time to investigate why this happens and what the fix is; potentially someone with a windows machine as VM are particularly slow.

Unfortunately all of the regular maintainer (aka mostly me), are doing this on their free time – which is rare. I also do not have a windows machine.

If you find any clues, of sequences of actions that always trigger this that would help.

@MrEightFive
Copy link

MrEightFive commented Feb 15, 2020

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

@sscalvo
Copy link

sscalvo commented Feb 16, 2020

MrEightFive Thanks for your fix.. It seems to be working for me

@dioni-tun
Copy link

@MrEightFive Thank you, just implemented the change. It seems to be working just fine.

@ericjam
Copy link

ericjam commented Mar 20, 2020

@MrEightFive Looks like it works for Python 3.8.2 as well.

@yehoshuadimarsky
Copy link

Thanks @MrEightFive and @Carreau for your solutions, much appreciated, I had same issue.

@pl-marasco
Copy link

@MrEightFive Looks like it works for Python 3.8.2 as well.

Same positive experience

@wookiesh
Copy link

Same, thanks !

@segevfiner
Copy link
Contributor

Encountering this too. @MrEightFive if you think you know what the fix is, maybe submit a pull request to CPython? 😉

@MrEightFive
Copy link

@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.

@segevfiner
Copy link
Contributor

segevfiner commented Apr 8, 2020

@MrEightFive It's actually simpler then you think. Just submit a PR and sign a CLA, then wait for code review, etc. Like any other project in GitHub. Though CPython often doesn't seem to have enough people reviewing pull requests, so many are left hanging. Alternatively you can just post your suggested workaround on the Python bug tracker issue. Even if you are not sure, you can just say so in the PR/issue, raising your concerns about it. This often helps getting someone more familiar to look at the issue and verify the fix or offer a better/correct one. But it's just a suggestion, do it only if you really feel like it.

@MrEightFive
Copy link

MrEightFive commented Apr 8, 2020

Connected to here on Python bug tracker Issue39010 - ProactorEventLoop raises unhandled ConnectionResetError - msg366002

@anjalp
Copy link

anjalp commented May 12, 2020

this error raised for also while using the promt_toolkit, but the solution : MrEightyFive works fantastic, saved a lot of time, thanks

@KyryloKaralyus
Copy link

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

@ryu577
Copy link

ryu577 commented May 25, 2020

KyryloKaralyus' solution worked for me. One thing to note - I first accidentally ran (missing the 0 at the end) pip install --upgrade prompt-toolkit==2.0.1 and this caused the error to start occurring non-stop. I had to kill the terminal.

@hearot
Copy link

hearot commented May 28, 2020

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.

@MrEightFive
Copy link

Pull request was finally proposed for CPython: bpo-39010: Ignore error on cancelled future after end of loop. #20525

@imba-tjd
Copy link
Contributor

imba-tjd commented Jun 6, 2020

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".

@1Mark
Copy link

1Mark commented Jun 7, 2020

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

pip install prompt-toolkit==2.0.10

@KamarajuKusumanchi
Copy link

I am getting this exception with python 3.8.3, ipython 7.13.0, prompt_toolkit 3.0.5

@zooba
Copy link

zooba commented Aug 24, 2020

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

The easier way is to copy the entire asyncio package into your project and modify it there, then rely on Python to shadow the entire module. Windows Store apps are very well protected.

But I'm also following up on the CPython patch.

@osiascorrea
Copy link

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

worked for me too!!
thx!!

@cdbyers
Copy link

cdbyers commented Aug 30, 2020

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

pip install prompt-toolkit==2.0.10

Thanks a ton, this worked for me:

conda install prompt_toolkit=2.0.10

@ashishejale
Copy link

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

This helped me as well, thank you!

@NoblySP
Copy link

NoblySP commented Sep 18, 2020

pip install --upgrade prompt-toolkit==2.0.10

Thank you so much, I finally have the above solution working on my device!!

I tried doing the following but I would always get [Errno 13] Permission denied

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

@imba-tjd
Copy link
Contributor

This has been fixed in 3.9rc2

@segevfiner
Copy link
Contributor

@imba-tjd Will it be back ported to older Python versions? (Or has it already?)

@Carreau
Copy link
Member

Carreau commented Sep 21, 2020

from the python bugs, t looks like it has been backported on 3.8.

@ele3131
Copy link

ele3131 commented Sep 28, 2020

I'm receiving the error even with 3.9rc2...

@rkuma18
Copy link

rkuma18 commented Oct 3, 2020

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

This helped, thanks.

@segevfiner
Copy link
Contributor

I wonder if this is now fixed in Python 3.8.6 and 3.9.

@guilhermemazanti
Copy link

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

I got this issue with a clean install of Python 3.8.3 using Anaconda on Windows 10 and the workaround by @MrEightFive worked perfectly fine for me!

@kingofhawks
Copy link

kingofhawks commented Dec 5, 2020

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

@RaInta
Copy link

RaInta commented Dec 8, 2020

This (pretty much) worked for me on Windows 10, Anaconda Individual Edition 2020.11 .

If you're a fellow Anaconda user, you may want to keep this update within the conda environment:
conda install -c conda-forge prompt_toolkit

Which worked for me.

@Klimmbimmel
Copy link

Klimmbimmel commented Dec 30, 2020

It comes up instantly after I execute an try-except-block, in other cases it is unpredictable

Just wanna add the german error text, so this will be found by more users. (Windows 10 Pro, Version 2004)

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

Exception [WinError 995] Der E/A-Vorgang wurde wegen eines Threadendes oder einer Anwendungsanforderung abgebrochen
Press ENTER to continue...

@musm
Copy link

musm commented Jan 6, 2021

Sadly

conda install -c conda-forge prompt_toolkit

did not fix the issue for me.

@dhuntley1023
Copy link

@musm, you need to specify prompt_toolkit v2.

conda install -c conda-forge prompt_toolkit=2

@musm
Copy link

musm commented Jan 7, 2021

I'm on Anaconda 2020.11 doing

conda install -c conda-forge prompt_toolkit=2

Is leading to massive conflicts:

> conda install -c conda-forge prompt_toolkit=2
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: \
Found conflicts! Looking for incompatible packages.
This can take several minutes.  Press CTRL-C to abort.
Examining qtpy:   1%|▌                                                                 | 3/327 [00:00<00:20, 16.00it/s]\Examining statsmodels:   1%|▌                                                          | 3/327 [00:00<00:20, 16.00it/s]/Examining ipython_genutils:   3%|█▌                                                   | 10/327 [00:07<04:17,  1.23it/s]|Examining pyflakes:   5%|██▉                                                          | 16/327 [00:10<02:19,  2.22it/s]|Examining zope.event:   7%|████▏                                                      | 23/327 [00:13<02:18,  2.20it/s]\Examining vs2015_runtime:  11%|██████                                                 | 36/327 [00:17<02:29,  1.94it/s]\Examining mpir:  15%|█████████▌                                                       | 48/327 [01:32<37:15,  8.01s/it]\Examining krb5:  17%|███████████▎                                                     | 57/327 [01:38<06:35,  1.47s/it]-Examining prometheus_client:  18%|█████████▌                                          | 60/327 [01:42<09:06,  2.05s/it]|Examining cython:  19%|████████████▏                                                  | 63/327 [01:47<08:58,  2.04s/it]-Examining flake8:  21%|█████████████                                                  | 68/327 [02:37<24:45,  5.73s/it]-Examining pytest:  25%|███████████████▊                                               | 82/327 [03:30<30:07,  7.38s/it]-Examining openpyxl:  26%|███████████████▊                                             | 85/327 [03:51<25:16,  6.27s/it]\Examining beautifulsoup4:  32%|█████████████████▎                                    | 105/327 [04:08<03:40,  1.01it/s]|Examining html5lib:  35%|████████████████████▉                                       | 114/327 [04:11<01:34,  2.25it/s]/Examining console_shortcut:  35%|██████████████████▏                                 | 114/327 [04:11<01:34,  2.25it/s]-Examining qt:  37%|████████████████████████▌                                         | 122/327 [04:14<01:09,  2.94it/s]/Examining wheel:  38%|████████████████████████                                       | 125/327 [04:14<00:54,  3.71it/s]|Examining greenlet:  39%|███████████████████████▍                                    | 128/327 [04:18<03:24,  1.03s/it]\Examining bokeh:  40%|█████████████████████████                                      | 130/327 [04:19<02:27,  1.33it/s]|Examining bleach:  40%|████████████████████████▊                                     | 131/327 [04:37<19:29,  5.97s/it]|Examining entrypoints:  40%|███████████████████████                                  | 132/327 [04:39<15:25,  4.75s/it]|Examining et_xmlfile:  40%|███████████████████████▍                                  | 132/327 [04:39<15:25,  4.75s/it]/Examining rtree:  42%|██████████████████████████▏                                    | 136/327 [04:39<07:31,  2.37s/it]-Examining backcall:  42%|████████████████████████▉                                   | 136/327 [04:39<07:31,  2.37s/it]\Examining libssh2:  45%|███████████████████████████▌                                 | 148/327 [04:58<06:13,  2.09s/it]\Examining path:  52%|█████████████████████████████████▍                              | 171/327 [05:54<23:54,  9.19s/it]/Examining pywavelets:  53%|██████████████████████████████▌                           | 172/327 [05:55<16:51,  6.53s/it]-Examining importlib_metadata:  54%|███████████████████████████▏                      | 178/327 [06:06<07:34,  3.05s/it]-Examining prompt_toolkit=2:  55%|████████████████████████████▍                       | 179/327 [06:07<07:56,  3.22s/it]-Examining seaborn:  56%|██████████████████████████████████▏                          | 183/327 [06:17<06:30,  2.71s/it]\Examining jupyterlab_widgets:  57%|████████████████████████████▎                     | 185/327 [06:21<05:57,  2.52s/it]|Examining jdcal:  58%|████████████████████████████████████▍                          | 189/327 [06:21<02:55,  1.27s/it]-Examining widgetsnbextension:  59%|█████████████████████████████▌                    | 193/327 [06:22<01:18,  1.70it/s]-Examining multipledispatch:  64%|█████████████████████████████████▍                  | 210/327 [07:07<03:36,  1.85s/it]-Examining yaml:  68%|███████████████████████████████████████████▍                    | 222/327 [07:19<01:11,  1.46it/s]-Examining libxslt:  70%|██████████████████████████████████████████▌                  | 228/327 [07:31<03:34,  2.17s/it]\Examining python-jsonrpc-server:  70%|████████████████████████████████▉              | 229/327 [07:31<02:30,  1.54s/it]|Examining typed-ast:  71%|█████████████████████████████████████████▊                 | 232/327 [07:32<01:01,  1.54it/s]\Examining @/win-64::__archspec==1=x86_64:  71%|██████████████████████████▉           | 232/327 [07:32<01:01,  1.54it/s]|Examining urllib3:  73%|████████████████████████████████████████████▍                | 238/327 [07:42<03:30,  2.36s/it]/Examining backports:  73%|███████████████████████████████████████████                | 239/327 [07:47<04:20,  2.96s/it]\Examining mccabe:  76%|███████████████████████████████████████████████▏          

@dhuntley1023
Copy link

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

@czy0538
Copy link

czy0538 commented Feb 14, 2021

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

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

Thanks!It‘s really useful for me ,even in 2021!

@MrEightFive
Copy link

This appears to be fixed for me in Python 3.9.1 (at least) on Windows 10

@rajnish001
Copy link

pip install prompt-toolkit==2.0.10

yes it worked for me as well.

thanks

@nateGeorge
Copy link

I think this is fixed in the latest prompt-tookit (currently 3.0.17).

pip install -U prompt-toolkit

@Xevion
Copy link

Xevion commented Aug 24, 2022

I think this is fixed in the latest prompt-tookit (currently 3.0.17).

pip install -U prompt-toolkit

Tried updating (3.0.29) and no change.

@itamar-dw
Copy link

I get the same error on Ubuntu, when using Jupyter console 6.4.4. It resolves if I downgrade prompt-toolkit to 2.0.10, but then IPython (8.7.0) fails to run with:

Traceback (most recent call last):
	File "/home/mlspeech/itamark/venvs/ssynth/bin/ipython", line 8, in <module>
		sys.exit(start_ipython())
	File "/home/mlspeech/itamark/venvs/ssynth/lib/python3.9/site-packages/IPython/__init__.py", line 123, in start_ipython
		return launch_new_instance(argv=argv, **kwargs)
	File "/home/mlspeech/itamark/venvs/ssynth/lib/python3.9/site-packages/traitlets/config/application.py", line 991, in launch_instance
		app.initialize(argv)
	File "/home/mlspeech/itamark/venvs/ssynth/lib/python3.9/site-packages/traitlets/config/application.py", line 113, in inner
		return method(app, *args, **kwargs)
	File "/home/mlspeech/itamark/venvs/ssynth/lib/python3.9/site-packages/IPython/terminal/ipapp.py", line 279, in initialize
		self.init_shell()
	File "/home/mlspeech/itamark/venvs/ssynth/lib/python3.9/site-packages/IPython/terminal/ipapp.py", line 293, in init_shell
		self.shell = self.interactive_shell_class.instance(parent=self,
	File "/home/mlspeech/itamark/venvs/ssynth/lib/python3.9/site-packages/traitlets/config/configurable.py", line 552, in instance
		inst = cls(*args, **kwargs)
	File "/home/mlspeech/itamark/venvs/ssynth/lib/python3.9/site-packages/IPython/terminal/interactiveshell.py", line 653, in __init__
		self.init_prompt_toolkit_cli()
	File "/home/mlspeech/itamark/venvs/ssynth/lib/python3.9/site-packages/IPython/terminal/interactiveshell.py", line 442, in init_prompt_toolkit_cli
		history = PtkHistoryAdapter(self)
	File "/home/mlspeech/itamark/venvs/ssynth/lib/python3.9/site-packages/IPython/terminal/interactiveshell.py", line 148, in __init__
		self._refresh()
	File "/home/mlspeech/itamark/venvs/ssynth/lib/python3.9/site-packages/IPython/terminal/interactiveshell.py", line 156, in _refresh
		if not self._loaded:
AttributeError: 'PtkHistoryAdapter' object has no attribute '_loaded'

Any idea how to get both IPython and Jupyter console to work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests