OperationalError server closed the connection unexpectedly

1,827 views
Skip to first unread message

Janusz Harkot

unread,
Nov 11, 2009, 8:17:48 AM11/11/09
to Django users
Request Method: GET
Request URL: http://localhost/
Exception Type: OperationalError
Exception Value:
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.

Have you ever get such an exception running django as fastcgi?

First time I've got this exception was on pre-1.0 Django, only when
runfcgi's "method" was set to prefork,
fortunately on threaded code was working fine.

Until recently I was curious to test this on Django 1.1.1. Will this
exception be thrown again... surprise, there it was again.

It took me some time to debug this, helpful hint was that it only
shows when (pre)forking.

So for those who getting randomly those exceptions, I can say... fix
your code :)
Ok.. seriously, there are always few ways of doing this, so let me
firs explain where is a problem first.

If you access database when any of your modules will import as, e.g.
reading configuration from database then you will get this error.

When your fastcgi-prefork application starts, first it imports all
modules, and only after this forks children.
If you have established db connection during import all children
processes will have an exact copy of that object.

This connection is being closed at the end of request phase
(request_finished signal).
So first child which will be called to process your request, will
close this connection.
But what will happen to the rest of the child processes?
They will believe that they have open and presumably working
connection to the db, so any db operation will cause an exception.


Why this is not showing in threaded execution model? I suppose because
threads are using same object and know when any other thread is
closing connection.

How to fix this?
Best way is to fix your code... but this can be difficult sometimes.

Other option, in my opinion quite clean, is to write somewhere in your
application small piece of code:
from django.db import connection
from django.core import signals

def close_connection(**kwargs):
connection.close()

signals.request_started.connect(close_connection)


Is this a best way? Not sure, but is working for me without
performance penalties - django doesn't have connection pooling - so no
harm is done.

Is this a django bug? Not really - this is something on the
application, not framework side - but let me know if you thing
otherwise.
Do you have a better fix?

J.

jame...@gmail.com

unread,
Nov 20, 2009, 2:06:06 PM11/20/09
to Django users


On 11 nov, 14:17, Janusz Harkot <janusz.har...@gmail.com> wrote:
> Request Method: GET
> Request URL:    http://localhost/
> Exception Type: OperationalError
> Exception Value:
> server closed the connection unexpectedly
>         This probably means the server terminated abnormally
>         before or while processing the request.
>
> Have you ever get such an exception running django as fastcgi?
>
> First time I've got this exception was on pre-1.0 Django, only when
> runfcgi's "method" was set to prefork,
> fortunately on threaded code was working fine.
>
> Until recently I was curious to test this on Django 1.1.1. Will this
> exception be thrown again... surprise, there it was again.

Same here.
>
> Other option, in my opinion quite clean, is to write somewhere in your
> application small piece of code:
> from django.db import connection
> from django.core import signals
>
> def close_connection(**kwargs):
>     connection.close()
>
> signals.request_started.connect(close_connection)

Thanks a lot!
Reply all
Reply to author
Forward
0 new messages