mvantellingen/psycopg2-ctypes — Issue #22: InterfaceError: connection already closed with Django and Gunicorn
Author: bclermont
Created: 2012-03-04 00:38:40+00:00
State: open

I'm using pypy 1.8 on a Django 1.3.1 with psycopg2-ctypes 2.4.4 on PostgreSQL 9.1
This is running from Gunicorn, 2 workers.
I very frequently get the following, like 2/3 of requests:

```
Traceback (most recent call last):
File "/usr/local/src/kontactsport.pypy/env/site-packages/gunicorn/workers/sync.py", line 99, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File "/usr/local/src/kontactsport.pypy/env/site-packages/django/contrib/staticfiles/handlers.py", line 68, in __call__
return self.application(environ, start_response)
File "/usr/local/src/kontactsport.pypy/env/site-packages/django/core/handlers/wsgi.py", line 274, in __call__
signals.request_finished.send(sender=self.__class__)
File "/usr/local/src/kontactsport.pypy/env/site-packages/django/dispatch/dispatcher.py", line 172, in send
response = receiver(signal=self, sender=sender, **named)
File "/usr/local/src/kontactsport.pypy/env/site-packages/django/db/__init__.py", line 85, in close_connection
conn.close()
File "/usr/local/src/kontactsport.pypy/env/site-packages/django/db/backends/__init__.py", line 244, in close
self.connection.close()
File "/usr/local/src/kontactsport.pypy/env/site-packages/psycopg2ct/_impl/connection.py", line 38, in check_closed_
raise exceptions.InterfaceError('connection already closed')
InterfaceError: connection already closed
```

my settings.py section for database is simply:

```
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'dbuser',
'PASSWORD': 'dbpass',
'HOST': '127.0.0.1',
'PORT': '5432',
#'autocommit': True
}
}
```

Nothing shows in PostgreSQL logs.

I went down to a single Gunicorn worker.
and if I run it without gunicorn, and simply runserver 127.0.0.1:$portbehindnginx

It never raise that exception again.

I'm not sure if it's psycopg2-ctypes related.

there is something that look like this here:

https://code.djangoproject.com/ticket/15802

This is my first pypy + django 1.3 + psycopg2-ctypes deployment.

All my previous one were on cpython + django 1.2 + psycopg2.