﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
15802	Django stops functioning when the database (PostgreSQL) closes the connection	Rick.van.Hattem@…	Aymeric Augustin	"In some cases (database restart, network connection lost, hard pgBouncer restart, etc...) the connection to the database is lost without giving Django a notification.

Currently Django doesn't handle that very gracefully... it just keeps trying to close an already closed connection (and gets errors because of that) so after you've somehow lost your database connection for a bit you have to restart all of your mod_wsgi processes (means executing a reload command on 5 servers for me right now) before Django starts working again.

The stacktrace:
{{{#!python
Traceback (most recent call last):
  File ""django/core/handlers/wsgi.py"", line 267, in 
__call__
    signals.request_finished.send(sender=self.__class__)
  File ""django/dispatch/dispatcher.py"", line 162, in send
    response = receiver(signal=self, sender=sender, **named)
  File ""django/db/__init__.py"", line 84, in 
close_connection
    conn.close()
  File ""django/db/backends/__init__.py"", line 72, in 
close
    self.connection.close()
InterfaceError: connection already closed

}}}

Proposed patch, replace the `close()` method in `django/db/backends/__init__.py` so it becomes this:
{{{#!python
def close(self):
    if self.connection is not None:
        try:
            self.connection.close()
            self.connection = None
        except InterfaceError:
            self.connection = None
            raise
}}}

I see no valid reason for giving a non-recoverable error while closing the database connection. Yes, something is wrong and yes, the exception can be shown. But looping the error is completely futile here."	Cleanup/optimization	closed	Database layer (models, ORM)	1.6	Normal	fixed	database, postgres, postgresql, connection, closed	Jeroen Dekkers sannies mike@… hv@… anssi.kaariainen@… reinout@… depaolim@… Ilya Antipenko pdina rckclmbr@… Akos Ladanyi	Accepted	1	0	0	1	0	0
