plpython memory leak uppon empty resultsets in all versions

From: Andres Freund <andres(at)anarazel(dot)de>
To: pgsql-bugs(at)postgresql(dot)org, Peter Eisentraut <peter_e(at)gmx(dot)net>
Subject: plpython memory leak uppon empty resultsets in all versions
Date: 2010-04-29 23:14:33
Message-ID: 201004300114.34247.andres@anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Craig Ringer 2010-04-29 23:27:56 Re: BUG #5442: Postgres crash when closing a "/console" mstsc session
Previous Message Kevin Grittner 2010-04-29 16:29:05 Re: BUG #5441: Why Not?????