MagicStack / MagicStack/uvloop

set_exception_handler is not handling exception as expected.

Open
#360 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

need more info
Dominant language
Cython
Stars
11.9k
Forks
616
PR merge metrics
No merged PRs in 30d

Description

* **uvloop version**: 0.14.0
* **Python version**: 3.6.9
* **Platform**: Linux c965e3d4adf2 5.3.0-1032-gcp #34~18.04.1-Ubuntu SMP
* **Can you reproduce the bug with `PYTHONASYNCIODEBUG` in env?**: yes
* **Does uvloop behave differently from vanilla asyncio? How?**:

Yes asyncio default loop catches `TimeoutError: [Errno 110] Connection timed out` whereas uvloop closes the loop and throws `RuntimeError: Event loop stopped before Future completed.`

I ran across this bug when dealing with websockets (you could see the issue - https://github.com/aaugustin/websockets/issues/823)

To log this error :
```
def handle_async_exception(loop, ctx):
logger.error("Loop error occured", ctx=ctx)
loop = asyncio.get_event_loop()
loop.set_exception_handler(handle_async_exception)
```

Python websockets throw a `TimeoutError` when a client is not found in a certain time limit (for ping/ pongs). In asyncio, the error is bubbled to `set_exception_handler`, and I could log it there as expected. However in uvloop, instead of having the exception being bubbled to exception_handler, it shuts down the loop. I get the following error:

```
Process Process-9:
Traceback (most recent call last):
File "/usr/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/lib/python3.6/multiprocessing/process.py", line 93, in run
self._target(*self._args, **self._kwargs)
File "/home/streaming/app/emitter_app/emitter.py", line 60, in start_emitter
loop.run_until_complete(asyncio.gather(server.serve(), push_payload(queue)))
File "uvloop/loop.pyx", line 1454, in uvloop.loop.Loop.run_until_complete
RuntimeError: Event loop stopped before Future completed.
```

Thanks for your time! Appreciate such a neat package to make asyncio faster in python.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing uvloop/loop.pyx around run_until_complete (line 1454) with vanilla asyncio's handling of set_exception_handler. Reproduce the websockets timeout scenario under Python 3.6.9 with PYTHONASYNCIODEBUG enabled; done means the TimeoutError reaches the configured exception handler without unexpectedly stopping the loop.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.