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: Non-thread-safe operation invoked on an event loop other than the current one #80

Closed
salberin opened this issue Oct 21, 2021 · 4 comments

Comments

@salberin
Copy link

I have my application deployed with gunicorn and uvicorn workers and for some views I'm getting the following error
RuntimeError: Non-thread-safe operation invoked on an event loop other than the current one

However, I'm not running any other event loops, I don't even have async code in my application at the moment. I'm running Django 3.2.7 on Python 3.9, full stacktrace is found below

Traceback (most recent call last):
  File "/home/alberink/.local/share/virtualenvs/device-management-7Wif-8Qf/lib/python3.9/site-packages/asgiref/sync.py", line 482, in thread_handler
    raise exc_info[1]
  File "/home/alberink/.local/share/virtualenvs/device-management-7Wif-8Qf/lib/python3.9/site-packages/django/core/handlers/exception.py", line 38, in inner
    response = await get_response(request)
  File "/home/alberink/.local/share/virtualenvs/device-management-7Wif-8Qf/lib/python3.9/site-packages/django/core/handlers/base.py", line 233, in _get_response_async
    response = await wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/alberink/.local/share/virtualenvs/device-management-7Wif-8Qf/lib/python3.9/site-packages/asgiref/sync.py", line 444, in __call__
    ret = await asyncio.wait_for(future, timeout=None)
  File "/usr/lib/python3.9/asyncio/tasks.py", line 442, in wait_for
    return await fut
  File "/home/alberink/.local/share/virtualenvs/device-management-7Wif-8Qf/lib/python3.9/site-packages/asgiref/current_thread_executor.py", line 22, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/alberink/.local/share/virtualenvs/device-management-7Wif-8Qf/lib/python3.9/site-packages/asgiref/sync.py", line 486, in thread_handler
    return func(*args, **kwargs)
  File "/home/alberink/.local/share/virtualenvs/device-management-7Wif-8Qf/lib/python3.9/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/alberink/Code/cloud/device-management/device_admin/device_views.py", line 651, in request_logs_device
    send_event('notifications', 'message', {'message': f'Sent log request for device {managed_device.name}'})
  File "/home/alberink/.local/share/virtualenvs/device-management-7Wif-8Qf/lib/python3.9/site-packages/django_eventstream/eventstream.py", line 35, in send_event
    get_listener_manager().add_to_queues(channel, e)
  File "/home/alberink/.local/share/virtualenvs/device-management-7Wif-8Qf/lib/python3.9/site-packages/django_eventstream/consumers.py", line 66, in add_to_queues
    l.aevent.set()
  File "/usr/lib/python3.9/asyncio/locks.py", line 205, in set
    fut.set_result(True)
  File "/usr/lib/python3.9/asyncio/base_events.py", line 748, in call_soon
    self._check_thread()
  File "/usr/lib/python3.9/asyncio/base_events.py", line 785, in _check_thread
    raise RuntimeError(
RuntimeError: Non-thread-safe operation invoked on an event loop other than the current one
@jkarneges
Copy link
Member

I've attempted a fix (#81). Can you try it?

@salberin
Copy link
Author

salberin commented Oct 26, 2021 via email

@salberin
Copy link
Author

Remote deployment seems to work fine as well!

@jkarneges
Copy link
Member

Released 4.4.0 with the fix.

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

2 participants