fanout / django-eventstream

Server-Sent Events for Django
MIT License
638 stars 84 forks source link

RuntimeError: Non-thread-safe operation invoked on an event loop other than the current one #80

Closed salberin closed 2 years ago

salberin commented 2 years ago

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 commented 2 years ago

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

salberin commented 2 years ago

I've tried it and so far it seems to hold up quite well in local testing. I'll try remote deployment next!

Many thanks for this, I've been wrecking my head over this for several days now....

On Tue, Oct 26, 2021 at 2:29 AM Justin Karneges @.***> wrote:

I've attempted a fix (#81 https://github.com/fanout/django-eventstream/pull/81). Can you try it?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/fanout/django-eventstream/issues/80#issuecomment-951445576, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOL4T5SUEQMIIGZSFBZBVDUIXY5XANCNFSM5GOBRWSQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

salberin commented 2 years ago

Remote deployment seems to work fine as well!

jkarneges commented 2 years ago

Released 4.4.0 with the fix.