Django Rest Framework + psycopg2 : InterfaceError: cursor already closed
Asked Answered
E

1

7

I have a problem with psycopg2. It fails to perform some queries. I have traced exceptions and it says: django.db.utils.InterfaceError: cursor already closed

I have already seen some similar cases on the Internet, tried all the recommendations, but no luck.

I use Django Rest Framework, and while requesting some endpoints it causes error, just in some requests. I monitored error.log file and I see 15-20 requests failed with HTTP 500 error.
Is there any solutions for this problem?

Software Versions:
djangorestframework == 3.12.1
django == 3.0.5
Python 3.8

This is uswgi.ini file which I run on the server. Socket is used by NGINX.

[uwsgi]

project = DjangoProject
base = /opt/django-project

chdir = %(base)
module = %(project).wsgi:application
home = %(base)/venv

gid = www-data
uid = www-data

master = true
processes = 5
socket = /tmp/%(project).sock
chmod-socket = 664
vacuum = true
harakiri = 60
max-requests = 10000

I tried to solve problem by increasing connections to Postgresql. But no change, the error still remains. Snippet from postgresql.conf file:

max_connections = 2000
shared_buffers = 800MB

Complete stack trace:

Internal Server Error: /api/users/
Traceback (most recent call last):
  File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/utils.py", line 97, in inner
    return func(*args, **kwargs)
psycopg2.InterfaceError: cursor already closed

The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/opt/django-project/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/opt/django-project/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/opt/django-project/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/django-project/venv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/viewsets.py", line 125, in view
    return self.dispatch(request, *args, **kwargs)
  File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/views.py", line 497, in dispatch
    self.initial(request, *args, **kwargs)
  File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/views.py", line 414, in initial
    self.perform_authentication(request)
  File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/views.py", line 324, in perform_authentication
    request.user
  File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/request.py", line 227, in user
    self._authenticate()
  File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/request.py", line 380, in _authenticate
    user_auth_tuple = authenticator.authenticate(self)
  File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/authentication.py", line 193, in authenticate
    return self.authenticate_credentials(token)
  File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/authentication.py", line 198, in authenticate_credentials
    token = model.objects.select_related('user').get(key=key)
  File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/query.py", line 411, in get
    num = len(clone)
File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/query.py", line 258, in __len__
    self._fetch_all()
  File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/query.py", line 1261, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/query.py", line 57, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1184, in execute_sql
    return list(result)
  File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1583, in cursor_iter
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1583, in <lambda>
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/utils.py", line 97, in inner
    return func(*args, **kwargs)
  File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/utils.py", line 97, in inner
    return func(*args, **kwargs)
django.db.utils.InterfaceError: cursor already closed

Elin answered 30/1, 2021 at 14:34 Comment(3)
Adding connections is not going to fix this. The issue is a cursor on a connection e.g. : con = psycopg2.connect(dsn) cur = con.cursor() Something in the code is creating a cursor then doing cursor.close() and then trying to use the closed cursor again.Ailina
Did you solve the issue? I face the same and cannot find an answer.Cyclist
I'm facing the same issue with no luck. I don't have this problem in 20+ other Django projects...even matched all the dependencies to known stable apps I have in the wild. Random, but frequent, cursor closed 500 errors.Draughtboard
V
1

Most likely, this is caused by client closing the connection. See https://forum.djangoproject.com/t/db-connection-closed-by-uwsgi-in-the-middle-of-handling-a-request/9875 for detailed explanation.

Vanesavanessa answered 19/11, 2021 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.