title: Django Rest Framework + psycopg2 : InterfaceError: cursor already closed
body: <p>I have a problem with psycopg2. It fails to perform some queries. I have traced exceptions and it says:  <code>django.db.utils.InterfaceError: cursor already closed</code></p>
  <p>I have already seen some similar cases on the Internet, tried all the recommendations, but no luck.</p>
  <p>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.<br />
  Is there any solutions for this problem?</p>
  <p>Software Versions:<br />
  <em>djangorestframework == 3.12.1<br />
  django == 3.0.5<br />
  Python 3.8</em></p>
  <p>This is <code>uswgi.ini</code> file which I run on the server. Socket is used by NGINX.</p>
  <pre><code>[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
  </code></pre>
  <p>I tried to solve problem by increasing connections to Postgresql. But no change, the error still remains.
  Snippet from <code>postgresql.conf</code> file:</p>
  <pre><code>max_connections = 2000
  shared_buffers = 800MB
  </code></pre>
  <p>Complete stack trace:</p>
  <pre><code>Internal Server Error: /api/users/
  Traceback (most recent call last):
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/django/db/utils.py&quot;, 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 &quot;/opt/django-project/venv/lib/python3.8/site-packages/django/core/handlers/exception.py&quot;, line 34, in inner
      response = get_response(request)
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/django/core/handlers/base.py&quot;, line 115, in _get_response
      response = self.process_exception_by_middleware(e, request)
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/django/core/handlers/base.py&quot;, line 113, in _get_response
      response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/django/views/decorators/csrf.py&quot;, line 54, in wrapped_view
      return view_func(*args, **kwargs)
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/viewsets.py&quot;, line 125, in view
      return self.dispatch(request, *args, **kwargs)
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/views.py&quot;, line 509, in dispatch
      response = self.handle_exception(exc)
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/views.py&quot;, line 469, in handle_exception
      self.raise_uncaught_exception(exc)
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/views.py&quot;, line 480, in raise_uncaught_exception
      raise exc
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/views.py&quot;, line 497, in dispatch
      self.initial(request, *args, **kwargs)
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/views.py&quot;, line 414, in initial
      self.perform_authentication(request)
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/views.py&quot;, line 324, in perform_authentication
      request.user
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/request.py&quot;, line 227, in user
      self._authenticate()
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/request.py&quot;, line 380, in _authenticate
      user_auth_tuple = authenticator.authenticate(self)
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/authentication.py&quot;, line 193, in authenticate
      return self.authenticate_credentials(token)
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/authentication.py&quot;, line 198, in authenticate_credentials
      token = model.objects.select_related('user').get(key=key)
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/query.py&quot;, line 411, in get
      num = len(clone)
  File &quot;/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/query.py&quot;, line 258, in __len__
      self._fetch_all()
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/query.py&quot;, line 1261, in _fetch_all
      self._result_cache = list(self._iterable_class(self))
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/query.py&quot;, line 57, in __iter__
      results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py&quot;, line 1184, in execute_sql
      return list(result)
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py&quot;, line 1583, in cursor_iter
      for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py&quot;, line 1583, in &lt;lambda&gt;
      for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/django/db/utils.py&quot;, line 97, in inner
      return func(*args, **kwargs)
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/django/db/utils.py&quot;, line 90, in __exit__
      raise dj_exc_value.with_traceback(traceback) from exc_value
    File &quot;/opt/django-project/venv/lib/python3.8/site-packages/django/db/utils.py&quot;, line 97, in inner
      return func(*args, **kwargs)
  django.db.utils.InterfaceError: cursor already closed
  
  </code></pre>