{"id":1983,"date":"2022-08-30T15:21:04","date_gmt":"2022-08-30T15:21:04","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/08\/class-psycopg2-interfaceerror-connection-already-closed-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:21:04","modified_gmt":"2022-08-30T15:21:04","slug":"class-psycopg2-interfaceerror-connection-already-closed-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/class-psycopg2-interfaceerror-connection-already-closed-collection-of-common-programming-errors\/","title":{"rendered":"class &#39;psycopg2.InterfaceError&#39;: connection already closed-Collection of common programming errors"},"content":{"rendered":"<p>I am pulling my hair over this and am about ready to do something dirty.<\/p>\n<p>I am running nginx + django + postgresql. Half the time I am trying to test my site and open a page, I get the following:<\/p>\n<pre><code>   Python 2.7.2: \/home\/webapp\/newavenue\/bin\/python\nFri Feb 24 08:28:58 2012\nA problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.\n\n \/home\/webapp\/newavenue\/lib\/python2.7\/site-packages\/flup\/server\/fcgi_base.py in run(self=)\n    572             \n    573         try:\n=&gt;  574             protocolStatus, appStatus = self.server.handler(self)\n    575         except:\n    576             traceback.print_exc(file=self.stderr)\nprotocolStatus undefined, appStatus undefined, self = , self.server = , self.server.handler = \n \/home\/webapp\/newavenue\/lib\/python2.7\/site-packages\/flup\/server\/fcgi_base.py in handler(self=, req=)\n   1157         try:\n   1158             try:\n=&gt; 1159                 result = self.application(environ, start_response)\n   1160                 try:\n   1161                     for data in result:\nresult = 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 = \n \/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=)\n    272                 response = self.get_response(request)\n    273         finally:\n=&gt;  274             signals.request_finished.send(sender=self.__class__)\n    275 \n    276         try:\nglobal signals = , signals.request_finished = , signals.request_finished.send = , sender undefined, self = , self.__class__ = \n \/home\/webapp\/newavenue\/lib\/python2.7\/site-packages\/django\/dispatch\/dispatcher.py in send(self=, sender=, **named={})\n    170 \n    171         for receiver in self._live_receivers(_make_id(sender)):\n=&gt;  172             response = receiver(signal=self, sender=sender, **named)\n    173             responses.append((receiver, response))\n    174         return responses\nresponse undefined, receiver = , signal undefined, self = , sender = , named = {}\n \/home\/webapp\/newavenue\/lib\/python2.7\/site-packages\/django\/db\/__init__.py in close_connection(**kwargs={'sender': , 'signal': })\n     83 def close_connection(**kwargs):\n     84     for conn in connections.all():\n=&gt;   85         conn.close()\n     86 signals.request_finished.connect(close_connection)\n     87 \nconn = , conn.close = \n \/home\/webapp\/newavenue\/lib\/python2.7\/site-packages\/django\/db\/backends\/__init__.py in close(self=)\n    242     def close(self):\n    243         if self.connection is not None:\n=&gt;  244             self.connection.close()\n    245             self.connection = None\n    246 \nself = , self.connection = , self.connection.close = \n: connection already closed \n      args = ('connection already closed',) \n      cursor = None \n      message = 'connection already closed' \n      pgcode = None \n      pgerror = None\n<\/code><\/pre>\n<p>The server is run with the following to generate the preceding flup traceback report:<\/p>\n<pre><code>python manage.py runfcgi host=127.0.0.1 port=8000 debug=False\n<\/code><\/pre>\n<p>Could I just get around this by patching in something like: &#8220;try: self.connection.close(), except: pass&#8221; 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.<\/p>\n<p>Edit: Considering this guy&#8217;s approach too: commenting it out: http:\/\/osdir.com\/ml\/DjangoUsers\/2009-04\/msg01647.html<\/p>\n<ol>\n<li>\n<p>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 &#8220;try: self.connection.close(), except: pass&#8221;. This also solved a 502 Bad Gateway problem I was having with nginx.<\/p>\n<p>Also, it seems my site runs a little faster, which is always a sweet side effect.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-08 05:35:45. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1983","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1983"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1983\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}