Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

memory leak if connection is closed actively by pg server

#148

Submitted by: reno.gan@gmail.com

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/vgpreload_memcheck-amd64-linux.so)
==14690==    by 0x92ACD3A: ??? (in /usr/lib/libpq.so.5.4)
==14690==    by 0x92B1765: PQconnectStart (in /usr/lib/libpq.so.5.4)
==14690==    by 0x92B1805: PQconnectdb (in /usr/lib/libpq.so.5.4)
==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)
    return;

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.

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

State: resolved
Milestone: none
Assigned to: nobody

Activity

  1. Daniele Varrazzo
    Daniele Varrazzo
    • State changed from new to open

    Thank you for the report and the analysis: we'll look into that.

    February 17th, 2013 @ 08:59 AM

  2. Daniele Varrazzo
    Daniele Varrazzo
    • State changed from open to resolved

    Fixed in devel branch

    March 16th, 2013 @ 03:14 PM