class 'psycopg2.InterfaceError': connection already closed

Justin M picture Justin M · Feb 24, 2012 · Viewed 9.4k times · Source

I am pulling my hair over this and am about ready to do something dirty.

I am running nginx + django + postgresql. Half the time I am trying to test my site and open a page, I get the following:

<class 'psycopg2.InterfaceError'>   Python 2.7.2: /home/webapp/newavenue/bin/python
Fri Feb 24 08:28:58 2012
A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.

 /home/webapp/newavenue/lib/python2.7/site-packages/flup/server/fcgi_base.py in run(self=<flup.server.fcgi_base.Request object>)
    572             
    573         try:
=>  574             protocolStatus, appStatus = self.server.handler(self)
    575         except:
    576             traceback.print_exc(file=self.stderr)
protocolStatus undefined, appStatus undefined, self = <flup.server.fcgi_base.Request object>, self.server = <flup.server.fcgi_fork.WSGIServer object>, self.server.handler = <bound method WSGIServer.handler of <flup.server.fcgi_fork.WSGIServer object>>
 /home/webapp/newavenue/lib/python2.7/site-packages/flup/server/fcgi_base.py in handler(self=<flup.server.fcgi_fork.WSGIServer object>, req=<flup.server.fcgi_base.Request object>)
   1157         try:
   1158             try:
=> 1159                 result = self.application(environ, start_response)
   1160                 try:
   1161                     for data in result:
result = None, self = <flup.server.fcgi_fork.WSGIServer object>, self.application = <django.core.handlers.wsgi.WSGIHandler object>, environ = {'CONTENT_LENGTH': '', 'CONTENT_TYPE': '', 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'HTTP_ACCEPT_CHARSET': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'HTTP_ACCEPT_ENCODING': 'gzip,deflate,sdch', 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8', 'HTTP_CACHE_CONTROL': 'max-age=0', 'HTTP_CONNECTION': 'keep-alive', 'HTTP_COOKIE': 'csrftoken=8a0176b368b17af00a8563b5f95b134b; sess...e5bdd0d0a4e3092e7a9abd0029c02; django_language=en', 'HTTP_HOST': 'newavedev.zapto.org', ...}, start_response = <function start_response>
 /home/webapp/newavenue/lib/python2.7/site-packages/django/core/handlers/wsgi.py in __call__(self=<django.core.handlers.wsgi.WSGIHandler object>, environ={'CONTENT_LENGTH': '', 'CONTENT_TYPE': '', 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'HTTP_ACCEPT_CHARSET': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'HTTP_ACCEPT_ENCODING': 'gzip,deflate,sdch', 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8', 'HTTP_CACHE_CONTROL': 'max-age=0', 'HTTP_CONNECTION': 'keep-alive', 'HTTP_COOKIE': 'csrftoken=8a0176b368b17af00a8563b5f95b134b; sess...e5bdd0d0a4e3092e7a9abd0029c02; django_language=en', 'HTTP_HOST': 'newavedev.zapto.org', ...}, start_response=<function start_response>)
    272                 response = self.get_response(request)
    273         finally:
=>  274             signals.request_finished.send(sender=self.__class__)
    275 
    276         try:
global signals = <module 'django.core.signals' from '/home/webapp...python2.7/site-packages/django/core/signals.pyc'>, signals.request_finished = <django.dispatch.dispatcher.Signal object>, signals.request_finished.send = <bound method Signal.send of <django.dispatch.dispatcher.Signal object>>, sender undefined, self = <django.core.handlers.wsgi.WSGIHandler object>, self.__class__ = <class 'django.core.handlers.wsgi.WSGIHandler'>
 /home/webapp/newavenue/lib/python2.7/site-packages/django/dispatch/dispatcher.py in send(self=<django.dispatch.dispatcher.Signal object>, sender=<class 'django.core.handlers.wsgi.WSGIHandler'>, **named={})
    170 
    171         for receiver in self._live_receivers(_make_id(sender)):
=>  172             response = receiver(signal=self, sender=sender, **named)
    173             responses.append((receiver, response))
    174         return responses
response undefined, receiver = <function close_connection>, signal undefined, self = <django.dispatch.dispatcher.Signal object>, sender = <class 'django.core.handlers.wsgi.WSGIHandler'>, named = {}
 /home/webapp/newavenue/lib/python2.7/site-packages/django/db/__init__.py in close_connection(**kwargs={'sender': <class 'django.core.handlers.wsgi.WSGIHandler'>, 'signal': <django.dispatch.dispatcher.Signal object>})
     83 def close_connection(**kwargs):
     84     for conn in connections.all():
=>   85         conn.close()
     86 signals.request_finished.connect(close_connection)
     87 
conn = <django.db.backends.postgresql_psycopg2.base.DatabaseWrapper object>, conn.close = <bound method DatabaseWrapper.close of <django.d...postgresql_psycopg2.base.DatabaseWrapper object>>
 /home/webapp/newavenue/lib/python2.7/site-packages/django/db/backends/__init__.py in close(self=<django.db.backends.postgresql_psycopg2.base.DatabaseWrapper object>)
    242     def close(self):
    243         if self.connection is not None:
=>  244             self.connection.close()
    245             self.connection = None
    246 
self = <django.db.backends.postgresql_psycopg2.base.DatabaseWrapper object>, self.connection = <connection object at 0x1d37130; dsn: 'dbname=ne...er=postgres host=localhost port=5432', closed: 2>, self.connection.close = <built-in method close of psycopg2._psycopg.connection object>
<class 'psycopg2.InterfaceError'>: connection already closed 
      args = ('connection already closed',) 
      cursor = None 
      message = 'connection already closed' 
      pgcode = None 
      pgerror = None

The server is run with the following to generate the preceding flup traceback report:

python manage.py runfcgi host=127.0.0.1 port=8000 debug=False

Could I just get around this by patching in something like: "try: self.connection.close(), except: pass" around that offending line? It would probably work but I think it sounds dirty (messing with core) and I would like to try to find an alternative solution.

Edit: Considering this guy's approach too: commenting it out: http://osdir.com/ml/DjangoUsers/2009-04/msg01647.html

Answer

Justin M picture Justin M · Feb 25, 2012

I think I got around the problem. This is not really a solution to the flup-psycopg2 setup but a sidestepping alternative. I switched from using fcgi to gunicorn and also wrapped the self.connection.close() line with "try: self.connection.close(), except: pass". This also solved a 502 Bad Gateway problem I was having with nginx.

Also, it seems my site runs a little faster, which is always a sweet side effect.