Title: asyncio crashes when tearing down the proactor event loop
Type: crash Stage:
Components: asyncio Versions: Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, cjrh, cmeyer, jack1142, mikeshardmind, pepoluan, rmawatson, yselivanov
Priority: normal Keywords:

Created on 2020-01-06 18:29 by mikeshardmind, last changed 2021-01-29 15:24 by pepoluan.

File name Uploaded Description Edit mikeshardmind, 2020-01-06 18:29 toy code demonstrating the difference.
Messages (4)
msg359448 - (view) Author: Michael Hall (mikeshardmind) * Date: 2020-01-06 18:29
When using for an asynchronous application utilizing ssl, on windows using the proactor event loop the application crashes when the loop is closed, completely skipping a finally block in the process.

This appears to be due to a __del__ method on transports used.

Manual handling of the event loop close while including a brief sleep appears to work as intended.

Both versions work fine with the selector event loop on linux.

This appears to be a somewhat known issue already, as it's been reported to aiohttp, however both the traceback, and the differing behavior seem to indicate this is an issue with the proactor event loop.

(On linux this still emits a resource warning without the sleep)

While I don't mind handling the loop cleanup, it seems like this case should also emit a resource warning rather than crashing.

If it's decided in which way this should be handled, I'm willing to contribute to or help test whatever direction the resolution for this should go. 

Traceback included below, toy version of the problem attached as code.

Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0000026463039820>
Traceback (most recent call last):
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python38\lib\asyncio\", line 116, in __del__
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python38\lib\asyncio\", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python38\lib\asyncio\", line 715, in call_soon
  File "C:\Users\Michael\AppData\Local\Programs\Python\Python38\lib\asyncio\", line 508, in _check_closed       
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
msg359515 - (view) Author: Michael Hall (mikeshardmind) * Date: 2020-01-07 15:56
I don't know if it would be feasible to add this to asyncio, but having a way to mark a resource as needing to be deterministically cleaned up at loop close could probably solve this as well as the underlying reasons why the transports are leaning on __del__ behavior which is an implementation detail (differing on, for example, pypy) and probably improve the overall usefulness of as well.

An addition like that probably requires more discussion than fixing this crash though.
msg362174 - (view) Author: Michael Hall (mikeshardmind) * Date: 2020-02-18 03:49
Linking out to a real-world example where this still manages to happen after running the event loop for an entire 2 seconds waiting for transports to close themselves after finishing everything else:

As well as what we're currently looking at for a temporary solution for this at this point:

I looked into what would need to change to handle this in CPython, but am not confident in my ability to make such a PR after doing so, at least not without more discussion about it.

The best solution I considered involves making the only public way to make transports be tied to an event loop which hasn't been closed yet, and ensuring the event loop keeps a reference to each of these so that it can deterministically close them at loop finalization. Searching GitHub alone found that this would break way too many things.

If this can't be fully fixed, a solution which at least ensures this can't cause an uncatchable exception would be appreciated.
msg367932 - (view) Author: rmawatson (rmawatson) Date: 2020-05-02 14:30
This is also happening in other libraries besides aiohttp. Specifically xhttp on windows. There is an open issue at
Date User Action Args
2021-01-29 15:24:20pepoluansetnosy: + pepoluan
2020-09-13 02:52:45cjrhsetnosy: + cjrh
2020-05-04 17:11:06cmeyersetnosy: + cmeyer
2020-05-02 14:30:06rmawatsonsetnosy: + rmawatson
messages: + msg367932
2020-03-14 19:21:31jack1142setnosy: + jack1142
2020-02-18 03:49:55mikeshardmindsetmessages: + msg362174
2020-01-07 15:56:48mikeshardmindsetmessages: + msg359515
2020-01-06 18:29:20mikeshardmindcreate