This project is archived and is in readonly mode.

#148 ✓resolved
Psycopg website

memory leak if connection is closed actively by pg server

Reported by Psycopg website | February 17th, 2013 @ 02:54 AM

Submitted by:

In our environment, every client connection has a max idle time. If this time expires, pg server will disconnect its connection to psycopg client. However, we found continuous memory leak during this process.

we use valgrind to check and found this:

==14690== 1,958,500 (53,680 direct, 1,904,820 indirect) bytes in 61 blocks are definitely lost in loss record 2,474 of 2,474
==14690==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/
==14690==    by 0x92ACD3A: ??? (in /usr/lib/
==14690==    by 0x92B1765: PQconnectStart (in /usr/lib/
==14690==    by 0x92B1805: PQconnectdb (in /usr/lib/
==14690==    by 0x9085088: conn_connect (connection_int.c:538)
==14690==    by 0x90864F9: connection_init (connection_type.c:1037)
==14690==    by 0x4DB6A5: ??? (in /usr/bin/python2.7)
==14690==    by 0x4E9F35: PyObject_Call (in /usr/bin/python2.7)
==14690==    by 0x4796D7: _PyObject_CallFunction_SizeT (in /usr/bin/python2.7)
==14690==    by 0x9080771: psyco_connect (psycopgmodule.c:110)
==14690==    by 0x49D584: PyEval_EvalFrameEx (in /usr/bin/python2.7)
==14690==    by 0x49F1BF: PyEval_EvalCodeEx (in /usr/bin/python2.7)

I searched the code and noticed that when pg server closed the connection, in "pq_raise()":

conn->closed = 2

But in "conn_close()", psycopg thinks connection is closed only by :

if (self->closed)

That means if "closed == 2", the memory free function "PQfreeCancel" will never be called.

I think the reasonable judgement is:

if (self->closed == 1)

That means if connection has really been closed and memory got freed.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

<b>WARNING:</b> the informations in this tracker are archived. Please submit new tickets or comments to <a href="">the new tracker</a>.
Psycopg is the most used PostgreSQL adapter for the Python programming language. At the core it fully implements the Python DB API 2.0 specifications. Several extensions allow access to many of the features offered by PostgreSQL.

Shared Ticket Bins