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

[PR 8617 - Async implementation] Async exception when returning Response(Serializer(instance).data) #8657

Closed
dongyuzheng opened this issue Sep 18, 2022 · 2 comments

Comments

@dongyuzheng
Copy link

dongyuzheng commented Sep 18, 2022

You cannot call this from an async context - use a thread or sync_to_async.

For PR #8617

  1. Make async function-based api_view return Response(Serializer(instance).data)
  2. Hit API (POST in my example)
SynchronousOnlyOperation at /api/create_user
You cannot call this from an async context - use a thread or sync_to_async.

Request Method: POST
Request URL: http://0.0.0.0:8000/api/create_user
Django Version: 4.1.1
Python Executable: /usr/local/bin/python3.10
Python Version: 3.10.7
Python Path: ['.', '/usr/local/bin', '/code', '/code/bb_backend', '/usr/local/lib/python310.zip', '/usr/local/lib/python3.10', '/usr/local/lib/python3.10/lib-dynload', '/root/.local/lib/python3.10/site-packages', '/usr/local/lib/python3.10/site-packages']
Server time: Sun, 18 Sep 2022 06:16:30 +0000
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 '<my_app>',
 'corsheaders']
Installed Middleware:
['corsheaders.middleware.CorsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/asgiref/sync.py", line 472, in thread_handler
    raise exc_info[1]
  File "/usr/local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 42, in inner
    response = await get_response(request)
  File "/usr/local/lib/python3.10/site-packages/django/core/handlers/base.py", line 253, in _get_response_async
    response = await wrapped_callback(
  File "/usr/local/lib/python3.10/site-packages/rest_framework/views.py", line 539, in async_dispatch
    response = self.handle_exception(exc)
  File "/usr/local/lib/python3.10/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/usr/local/lib/python3.10/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/usr/local/lib/python3.10/site-packages/rest_framework/views.py", line 536, in async_dispatch
    response = await handler(request, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/rest_framework/decorators.py", line 54, in async_handler
    return await func(*args, **kwargs)
  File "/code/bb_backend/./bb_core/api/auth.py", line 55, in create_user
    return Response(UserSerializer(instance=user).data)
  File "/usr/local/lib/python3.10/site-packages/rest_framework/serializers.py", line 555, in data
    ret = super().data
  File "/usr/local/lib/python3.10/site-packages/rest_framework/serializers.py", line 253, in data
    self._data = self.to_representation(self.instance)
  File "/usr/local/lib/python3.10/site-packages/rest_framework/serializers.py", line 522, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "/usr/local/lib/python3.10/site-packages/rest_framework/relations.py", line 565, in to_representation
    return [
  File "/usr/local/lib/python3.10/site-packages/django/db/models/query.py", line 394, in __iter__
    self._fetch_all()
  File "/usr/local/lib/python3.10/site-packages/django/db/models/query.py", line 1866, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/usr/local/lib/python3.10/site-packages/django/db/models/query.py", line 87, in __iter__
    results = compiler.execute_sql(
  File "/usr/local/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1393, in execute_sql
    cursor = self.connection.cursor()
  File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 24, in inner
    raise SynchronousOnlyOperation(message)

Exception Type: SynchronousOnlyOperation at /api/create_user
Exception Value: You cannot call this from an async context - use a thread or sync_to_async.
@ahzamm
Copy link
Contributor

ahzamm commented Sep 25, 2022

@dongyuzheng is this issue still open or resolved?

@tomchristie
Copy link
Member

Closing as per #8655

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

3 participants