class 'psycopg2.InterfaceError': connection already closed-Collection of common programming errors
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:
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=)
572
573 try:
=> 574 protocolStatus, appStatus = self.server.handler(self)
575 except:
576 traceback.print_exc(file=self.stderr)
protocolStatus undefined, appStatus undefined, self = , self.server = , self.server.handler =
/home/webapp/newavenue/lib/python2.7/site-packages/flup/server/fcgi_base.py in handler(self=, req=)
1157 try:
1158 try:
=> 1159 result = self.application(environ, start_response)
1160 try:
1161 for data in result:
result = None, self = , self.application = , 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 =
/home/webapp/newavenue/lib/python2.7/site-packages/django/core/handlers/wsgi.py in __call__(self=, 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=)
272 response = self.get_response(request)
273 finally:
=> 274 signals.request_finished.send(sender=self.__class__)
275
276 try:
global signals = , signals.request_finished = , signals.request_finished.send = , sender undefined, self = , self.__class__ =
/home/webapp/newavenue/lib/python2.7/site-packages/django/dispatch/dispatcher.py in send(self=, sender=, **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 = , signal undefined, self = , sender = , named = {}
/home/webapp/newavenue/lib/python2.7/site-packages/django/db/__init__.py in close_connection(**kwargs={'sender': , 'signal': })
83 def close_connection(**kwargs):
84 for conn in connections.all():
=> 85 conn.close()
86 signals.request_finished.connect(close_connection)
87
conn = , conn.close =
/home/webapp/newavenue/lib/python2.7/site-packages/django/db/backends/__init__.py in close(self=)
242 def close(self):
243 if self.connection is not None:
=> 244 self.connection.close()
245 self.connection = None
246
self = , self.connection = , self.connection.close =
: 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
-
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.