Django stops functioning when the database (PostgreSQL) closes the connection
|Reported by:||Owned by:||Aymeric Augustin|
|Component:||Database layer (models, ORM)||Version:||1.6|
|Severity:||Normal||Keywords:||database, postgres, postgresql, connection, closed|
|Cc:||Jeroen Dekkers, sannies, mike@…, hv@…, anssi.kaariainen@…, reinout@…, depaolim@…, Ilya Antipenko, pdina, rckclmbr@…, AkosLadanyi||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
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.
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:
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.
Change History (42)
comment:10 follow-up: 11 Changed 6 years ago by
|Cc:||Jeroen Dekkers added|
|Status:||closed → reopened|
comment:23 Changed 4 years ago by
|Owner:||changed from Honza Král to Aymeric Augustin|
|Status:||new → assigned|
comment:36 follow-up: 39 Changed 2 years ago by
|Version:||1.3 → 1.6|