plpython memory leak uppon empty resultsets in all versions

Поиск
Список
Период
Сортировка
От Andres Freund
Тема plpython memory leak uppon empty resultsets in all versions
Дата
Msg-id 201004300114.34247.andres@anarazel.de
обсуждение исходный текст
Ответы Re: plpython memory leak uppon empty resultsets in all versions  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: plpython memory leak uppon empty resultsets in all versions  (Björn Grüning<bjoern.gruening@pharmazie.uni-freiburg.de>)
Список pgsql-bugs
Hi all,

plpython[u] leaks memory in PLy_spi_execute_fetch_result in the following
snippet:

Py_DECREF(result->nrows);
result->nrows = PyInt_FromLong(rows);
PLy_typeinfo_init(&args);

oldcontext = CurrentMemoryContext;
PG_TRY();
{
    if (rows)
    {
        Py_DECREF(result->rows);
        result->rows = PyList_New(rows);

        PLy_input_tuple_funcs(&args, tuptable->tupdesc);
        for (i = 0; i < rows; i++)
        {
            PyObject   *row = PLyDict_FromTuple(&args, tuptable->vals[i],
                                              tuptable->tupdesc);

            PyList_SetItem(result->rows, i, row);
        }
        PLy_typeinfo_dealloc(&args);

        SPI_freetuptable(tuptable);
    }
}
PG_CATCH();
{
    MemoryContextSwitchTo(oldcontext);
    PLy_error_in_progress = CopyErrorData();
    FlushErrorState();
    if (!PyErr_Occurred())
        PyErr_SetString(PLy_exc_error,
                "Unknown error in PLy_spi_execute_fetch_result");
    Py_DECREF(result);
    PLy_typeinfo_dealloc(&args);
    return NULL;
}
PG_END_TRY();

if rows is 0 PLy_typeinfo_dealloc and SPI_freetuptable will not be called. An
easy example where this is easily leading to a fast growing memleak (1G in
5s):

DO LANGUAGE 'plpythonu' $$while True: plpy.execute("SELECT 1 WHERE false")$$;


The fix is simple. Just move those two outside the if block. As a slightly
alternative solution one could also remove the "return NULL" and move those
outside the PG_CATCH.

Found when investigating a problem of 'bag' on irc (bcc'ed, he had to go home
before I though of asking him whether its ok to publish his name/mail).

Andres

В списке pgsql-bugs по дате отправления:

Предыдущее
От: "Kevin Grittner"
Дата:
Сообщение: Re: BUG #5441: Why Not?????
Следующее
От: Craig Ringer
Дата:
Сообщение: Re: BUG #5442: Postgres crash when closing a "/console" mstsc session