lazybird/django-solo

Bug in async server?

myclues opened this issue · 1 comments

Hello,
I recently added django-solo for an application settings singleton model, and I'm not sure if that's the root cause, but now I'm seeing an error with a stacktrace that references the solo model. I'm using Uvicorn/ASGI to take advantage of websockets fyi.

2020-05-22T18:43:38.897819907Z   File "./my_app/admin.py", line 20, in <module>
2020-05-22T18:43:38.897824304Z     app_settings = AppSettings.get_solo()
2020-05-22T18:43:38.897828288Z   File "/usr/local/lib/python3.8/site-packages/solo/models.py", line 54, in get_solo
2020-05-22T18:43:38.897832902Z     obj, created = cls.objects.get_or_create(pk=cls.singleton_instance_id)
2020-05-22T18:43:38.897848919Z   File "/usr/local/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method
2020-05-22T18:43:38.897855148Z     return getattr(self.get_queryset(), name)(*args, **kwargs)
2020-05-22T18:43:38.897860070Z   File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 559, in get_or_create
2020-05-22T18:43:38.897865434Z     return self.get(**kwargs), False
2020-05-22T18:43:38.897869775Z   File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 411, in get
2020-05-22T18:43:38.897873522Z     num = len(clone)
2020-05-22T18:43:38.897877129Z   File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 258, in __len__
2020-05-22T18:43:38.897880333Z     self._fetch_all()
2020-05-22T18:43:38.897883208Z   File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 1261, in _fetch_all
2020-05-22T18:43:38.897886267Z     self._result_cache = list(self._iterable_class(self))
2020-05-22T18:43:38.897889212Z   File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 57, in __iter__
2020-05-22T18:43:38.897892246Z     results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
2020-05-22T18:43:38.897895318Z   File "/usr/local/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1149, in execute_sql
2020-05-22T18:43:38.897898445Z     cursor = self.connection.cursor()
2020-05-22T18:43:38.897901317Z   File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 24, in inner
2020-05-22T18:43:38.897904395Z     raise SynchronousOnlyOperation(message)
2020-05-22T18:43:38.898339974Z django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.

@myclues Would you be able to provide a minimal example app that demonstrates this?