Unable to log in the user using django channels because "connection is already closed"

33 views
Skip to first unread message

Ross Meredith

unread,
Sep 4, 2021, 5:27:33 PM9/4/21
to Django users
Has anybody had this before?


I'm unable to log a user in with the login function provided by Django Channels.

I have this mutation using graphene django -

class SignIn(DjangoFormMutation):
  profile = graphene.Field(ProfileNode)
 
  @classmethod
  def get_form_kwargs(cls, root, info, **input):
    kwargs = super().get_form_kwargs(root, info, **input)
    kwargs["request"] = info.context
    return kwargs

   class Meta:
     form_class = AuthenticationForm # username is in fact email for user
     fields = ('username', 'password',)

  @classmethod
  def perform_mutate(cls, form, info):
    # form is valid
    user = form.get_user()
    profile = user.profile async_to_sync(channels.auth.login)(info.context.__dict__, user)
    change_session_and_login(info.context, user)
    return cls(errors=[], profile=profile)

This is based on the advice from the package I am using for using websockets with graphene - https://github.com/datadvance/DjangoChannelsGraphqlWs#authentication

I am getting this tracebook though, which, to me, suggests a problem with the async_to_sync.

The only obvious difference I can see is I'm getting the dict by calling dict on the context whereas the example in the link above uses _asdict (not sure why because this method is not an option for me when I try).


This is the full traceback -

Traceback (most recent call last): File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/promise/promise.py", line 489, in _resolve_from_executor executor(resolve, reject) File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/promise/promise.py", line 756, in executor return resolve(f(*args, **kwargs)) File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/graphql/execution/middleware.py", line 75, in make_it_promise return next(*args, **kwargs) File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/graphene/relay/mutation.py", line 70, in mutate result = cls.mutate_and_get_payload(root, info, **input) File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/graphene_django/forms/mutation.py", line 51, in mutate_and_get_payload return cls.perform_mutate(form, info) File "/home/ross/Desktop/web_dev/projects/chat/user/schema.py", line 219, in perform_mutate async_to_sync(channels.auth.login)(info.context.__dict__, user) File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/asgiref/sync.py", line 223, in __call__ return call_result.result() File "/usr/lib/python3.8/concurrent/futures/_base.py", line 437, in result return self.__get_result() File "/usr/lib/python3.8/concurrent/futures/_base.py", line 389, in __get_result raise self._exception File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/asgiref/sync.py", line 292, in main_wrap result = await self.awaitable(*args, **kwargs) File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/asgiref/sync.py", line 444, in __call__ ret = await asyncio.wait_for(future, timeout=None) File "/usr/lib/python3.8/asyncio/tasks.py", line 455, in wait_for return await fut File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/asgiref/current_thread_executor.py", line 22, in run result = self.fn(*self.args, **self.kwargs) File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/channels/db.py", line 13, in thread_handler return super().thread_handler(loop, *args, **kwargs) File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/asgiref/sync.py", line 486, in thread_handler return func(*args, **kwargs) File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/channels/auth.py", line 93, in login session.cycle_key() File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/contrib/sessions/backends/base.py", line 344, in cycle_key self.create() File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/contrib/sessions/backends/db.py", line 51, in create self._session_key = self._get_new_session_key() File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/contrib/sessions/backends/base.py", line 196, in _get_new_session_key if not self.exists(session_key): File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/contrib/sessions/backends/db.py", line 47, in exists return self.model.objects.filter(session_key=session_key).exists() File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/db/models/query.py", line 808, in exists return self.query.has_results(using=self.db) File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/db/models/sql/query.py", line 552, in has_results return compiler.has_results() File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1145, in has_results return bool(self.execute_sql(SINGLE)) File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1173, in execute_sql cursor = self.connection.cursor() File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/graphene_django/debug/sql/tracking.py", line 41, in cursor return state.Wrapper(connection._graphene_cursor(), connection, panel) File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor return self._cursor() File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/db/backends/base/base.py", line 237, in _cursor return self._prepare_cursor(self.create_cursor(name)) File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/db/backends/base/base.py", line 237, in _cursor return self._prepare_cursor(self.create_cursor(name)) File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 236, in create_cursor cursor = self.connection.cursor() graphql.error.located_error.GraphQLLocatedError: connection already closed


Reply all
Reply to author
Forward
0 new messages