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

asyncio error on mac #174

Open
Almenon opened this issue Oct 3, 2021 · 4 comments
Open

asyncio error on mac #174

Almenon opened this issue Oct 3, 2021 · 4 comments

Comments

@Almenon
Copy link
Owner

Almenon commented Oct 3, 2021

Almenon/AREPL-vscode#387

Reproduction Requirements:

  • MacOS Mojave or higher - ex 11.2.3 (Big Sur)
  • python v3.9

The bug can be reproduced with a simple import statement: import math

BrandonNav says that changing the following

asyncio.set_event_loop(asyncio.new_event_loop())
to asyncio.set_event_loop(asyncio.get_event_loop()) fixes it.

@Almenon
Copy link
Owner Author

Almenon commented Oct 3, 2021

set_event_loop(loop): Set loop as a current event loop for the current OS thread.

The documentation for new_event_loop (current code) simply says Create a new event loop object.

The documentation for get_event_loop (proposed change) says:

Get the current event loop.

If there is no current event loop set in the current OS thread, the OS thread is main, and set_event_loop() has not yet been called, asyncio will create a new event loop and set it as the current one.

Because this function has rather complex behavior (especially when custom event loop policies are in use), using the get_running_loop() function is preferred to get_event_loop() in coroutines and callbacks.

Consider also using the asyncio.run() function instead of using lower level functions to manually create and close an event loop.

The problem here is that asyncio.set_event_loop(asyncio.get_event_loop()) is a no-op most of the time. Why set loop to the current event loop if it's already the current event loop? To avoid side-effects between runs I need a fresh loop.

@Almenon
Copy link
Owner Author

Almenon commented Oct 3, 2021

Also some notes on the exception thrown:


Traceback (most recent call last):
  File "/usr/local/Cellar/[email protected]/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/selector_events.py", line 261, in _add_reader
    key = self._selector.get_key(fd)
  File "/usr/local/Cellar/[email protected]/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/selectors.py", line 193, in get_key
    raise KeyError("{!r} is not registered".format(fileobj)) from None
KeyError: '5 is not registered'

Called upon the creation of the event loop, not exactly sure what it's trying to do but regardless it tries a different method _selector.register in the exception handler.

Traceback (most recent call last):
  File "/Users/jb/.vscode/extensions/almenon.arepl-2.0.2/node_modules/arepl-backend/python/arepl_python_evaluator.py", line 298, in main
    return_info = exec_input(execArgs)
  File "/Users/jb/.vscode/extensions/almenon.arepl-2.0.2/node_modules/arepl-backend/python/arepl_python_evaluator.py", line 234, in exec_input
    asyncio.set_event_loop(asyncio.new_event_loop())
  File "/usr/local/Cellar/[email protected]/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/events.py", line 761, in new_event_loop
    return get_event_loop_policy().new_event_loop()
  File "/usr/local/Cellar/[email protected]/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/events.py", line 659, in new_event_loop
    return self._loop_factory()
  File "/usr/local/Cellar/[email protected]/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/unix_events.py", line 54, in __init__
    super().__init__(selector)
  File "/usr/local/Cellar/[email protected]/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/selector_events.py", line 61, in __init__
    self._make_self_pipe()
  File "/usr/local/Cellar/[email protected]/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/selector_events.py", line 112, in _make_self_pipe
    self._add_reader(self._ssock.fileno(), self._read_from_self)
  File "/usr/local/Cellar/[email protected]/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/selector_events.py", line 263, in _add_reader
    self._selector.register(fd, selectors.EVENT_READ,
  File "/usr/local/Cellar/[email protected]/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/selectors.py", line 523, in register
    self._selector.control([kev], 0, 0)
TypeError: changelist must be an iterable of select.kevent objects

This is the main error. The last line is a method of KqueueSelector, which is specific to BSD OS's like mac. kev is from the line above (Lib\selectors.py:522):

kev = select.kevent(key.fd, select.KQ_FILTER_READ,
                                        select.KQ_EV_ADD)

I'm guessing the kevent was not found and kev is null. Really wish I had a mac to investigate this.

Print Output:
Exception ignored in: <function BaseEventLoop.__del__ at 0x11871de50>
Traceback (most recent call last):
  File "/usr/local/Cellar/[email protected]/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 683, in __del__
    self.close()
  File "/usr/local/Cellar/[email protected]/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/unix_events.py", line 60, in close
    for sig in list(self._signal_handlers):
AttributeError: '_UnixSelectorEventLoop' object has no attribute '_signal_handlers'

Not sure what's going on here.

@Almenon
Copy link
Owner Author

Almenon commented Oct 3, 2021

Might be worth reporting this as a python bug - but I'd like to see if I'm able to reproduce it with simplified exec code on a mac first :|

@Almenon Almenon changed the title Error on mac asyncio error on mac Jan 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant