classification
Title: __aexit__ not called when `run_until_complete` is interrupted by SIGINT
Type: Stage:
Components: asyncio Versions: Python 3.7, Python 3.6, Python 3.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: rthr, yselivanov
Priority: normal Keywords:

Created on 2017-06-15 15:27 by rthr, last changed 2017-06-15 16:06 by rthr.

Pull Requests
URL Status Linked Edit
PR 2219 open rthr, 2017-06-15 15:27
Messages (3)
msg296106 - (view) Author: Arthur Darcet (rthr) * Date: 2017-06-15 15:27
Here is the example code I am running:

```
import asyncio

class it:
	async def __aenter__(self):
		return self
	async def __aexit__(self, *_):
		print('EXIT')

async def main():
	async with it():
		await asyncio.sleep(100)

asyncio.get_event_loop().run_until_complete(main())
```


When this gets interrupted by a SIGINT, I would expect this code to display `EXIT` before the `KeyboardInterrupt` stacktrace. But instead the `__aexit__` function is simply not called.
msg296110 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-06-15 16:01
Yes, this is a known limitation of asyncio -- keyboardinterrupt exceptions kills the loop rather abruptly. One way to handle this is to use 'signal.signal' for sigint to raise custom exception instead of keyboardinterrupt.
msg296112 - (view) Author: Arthur Darcet (rthr) * Date: 2017-06-15 16:06
Ok, thank you. I'm guessing the patch I proposed in the PR is not an option, for my curiosity, why is that?
History
Date User Action Args
2017-06-15 16:06:49rthrsetmessages: + msg296112
2017-06-15 16:01:23yselivanovsetmessages: + msg296110
2017-06-15 15:27:28rthrcreate