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

'RuntimeError: Event loop is closed' when running under Windows #9

Closed
heckler opened this issue Jul 15, 2022 · 13 comments
Closed

'RuntimeError: Event loop is closed' when running under Windows #9

heckler opened this issue Jul 15, 2022 · 13 comments

Comments

@heckler
Copy link

heckler commented Jul 15, 2022

When running dinghy under windows I'm getting an 'Event loop is closed' error from the asyncio lib, even though the output file is created without issues.

The exact same command runs fine under Ubuntu, without exceptions - same repository, github token, dinghy version; error only happens on windows.

  • Windows version: Windows 10 20H2 19042.1766, 64b
  • Python version: 3.9.1 (64b)
  • Dinghy version: 0.12.0

Note: the digest.html file was generated fine, so the exception thrown might be a minor issue related to resource cleanup.

Error log

C:\Users\heckler>python3 -m dinghy https://github.com/heckler/home-assistant-config
Wrote digest: digest.html
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x00000221711D58B0>
Traceback (most recent call last):
  File "C:\Users\heckler\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\heckler\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\heckler\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 746, in call_soon
    self._check_closed()
  File "C:\Users\heckler\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x00000221711D58B0>
Traceback (most recent call last):
  File "C:\Users\heckler\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\heckler\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\heckler\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 746, in call_soon
    self._check_closed()
  File "C:\Users\heckler\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
@nedbat
Copy link
Owner

nedbat commented Jul 19, 2022

Hmm, very odd. Can you try it on a newer version of 3.9? or on 3.10?

@heckler
Copy link
Author

heckler commented Jul 20, 2022

Can you try it on a newer version of 3.9? or on 3.10?

Sure! Tested on 3.10.5 amd64 just now. Otherwise, same environment as before. This time I had a yaml config pointing to GHE (earlier test was github.com), but as before, the digest was written just fine, so this exception happens when the app is terminating.

$ python --version
Python 3.10.5

$ python -m dinghy
Wrote digest: group_digest.html
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x00000132857049D0>
Traceback (most recent call last):
  File "C:\Users\heckler\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\heckler\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\heckler\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 750, in call_soon
    self._check_closed()
  File "C:\Users\heckler\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 515, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

@nedbat
Copy link
Owner

nedbat commented Aug 3, 2022

This seems to be a known problem that is fixed in 3.10.6.

@carltongibson
Copy link

carltongibson commented Aug 3, 2022

Confirming that it doesn’t reproduce with Python 3.10.6.

Adding something like…

if sys.version_info < (3, 10, 6) and sys.platform.startswith('win'):
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

… after the module imports, should silence it on earlier versions.

@nedbat nedbat closed this as completed in c64c247 Aug 3, 2022
@nedbat
Copy link
Owner

nedbat commented Aug 3, 2022

Thanks, I've added that in c64c247. @heckler Can you try it?

@nedbat
Copy link
Owner

nedbat commented Aug 3, 2022

I've released this as 0.13.1.

@graingert
Copy link

graingert commented Aug 3, 2022

This issue still happens on the SelectorEventLoop (enable ResourceWarnings) , it's just hidden by default

@heckler
Copy link
Author

heckler commented Aug 3, 2022

Thanks, I've added that in c64c247. @heckler Can you try it?

Sure thing!

  • Tested without the fix, under 3.10.5 (warning, as expected) and 3.10.6 (no issues observed)
  • Tested [c64c247] under both 3.10.5 and 3.10.6, no warnings or issues on either

This is as good as closed. Thank you! :)

@graingert
Copy link

python -Werror -m dinghy https://github.com/Me/MyProject
error: dinghy error: Unauthorized. You need to create a GITHUB_TOKEN environment variable.
Exception ignored in: <function _SelectorTransport.__del__ at 0x7f264ea12320>
Traceback (most recent call last):
  File "/usr/lib/python3.10/asyncio/selector_events.py", line 701, in __del__
    _warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
ResourceWarning: unclosed transport <_SelectorSocketTransport fd=7>
Exception ignored in: <function _SelectorTransport.__del__ at 0x7f264ea12320>
Traceback (most recent call last):
  File "/usr/lib/python3.10/asyncio/selector_events.py", line 701, in __del__
    _warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
ResourceWarning: unclosed transport <_SelectorSocketTransport fd=6>
Exception ignored in: <socket.socket fd=6, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('192.168.50.135', 49202), raddr=('140.82.121.6', 443)>
ResourceWarning: unclosed <socket.socket fd=6, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('192.168.50.135', 49202), raddr=('140.82.121.6', 443)>
Exception ignored in: <socket.socket fd=7, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('192.168.50.135', 49204), raddr=('140.82.121.6', 443)>
ResourceWarning: unclosed <socket.socket fd=7, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('192.168.50.135', 49204), raddr=('140.82.121.6', 443)>

@graingert
Copy link

graingert commented Aug 3, 2022

you only need to ignore the runtime error rather than switching over to the SelectorEventLoop:

import sys


def unraisablehook(unraisable, /, _old_hook=sys.unraisablehook):
    if unraisable.err_msg == "RuntimeError: Event loop is closed" and isinstance(
        unraisable.object, _ProactorBasePipeTransport
    ):
        return
    return _old_hook(unraisable)

sys.unraisablehook = unraisablehook

(more people should use the ProactorEventLoop!)

@nedbat
Copy link
Owner

nedbat commented Aug 3, 2022

@graingert I appreciate the advice, but am more comfortable with switching the loop than suppressing the message, and this will all be unneeded eventually anyway. Thanks all for your help!

@carltongibson
Copy link

@graingert That's a good comment! 👀

This is dark arts. Is it documented? Might be worth a blog post...

Interesting.

@graingert
Copy link

@graingert That's a good comment! 👀

This is dark arts. Is it documented? Might be worth a blog post...

Interesting.

https://vstinner.github.io/sys-unraisablehook-python38.html

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

4 participants