mirror of
https://github.com/postgres/postgres.git
synced 2025-08-18 12:22:09 +03:00
Fix memory leaks in PL/Python.
Previously, plpython was in the habit of allocating a lot of stuff in TopMemoryContext, and it was very slipshod about making sure that stuff got cleaned up; in particular, use of TopMemoryContext as fn_mcxt for function calls represents an unfixable leak, since we generally don't know what the called function might have allocated in fn_mcxt. This results in session-lifespan leakage in certain usage scenarios, as for example in a case reported by Ed Behn back in July. To fix, get rid of all the retail allocations in TopMemoryContext. All long-lived allocations are now made in sub-contexts that are associated with specific objects (either pl/python procedures, or Python-visible objects such as cursors and plans). We can clean these up when the associated object is deleted. I went so far as to get rid of PLy_malloc completely. There were a couple of places where it could still have been used safely, but on the whole it was just an invitation to bad coding. Haribabu Kommi, based on a draft patch by Heikki Linnakangas; some further work by me
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
|
||||
#include "access/xact.h"
|
||||
#include "mb/pg_wchar.h"
|
||||
#include "utils/memutils.h"
|
||||
|
||||
#include "plpython.h"
|
||||
|
||||
@@ -111,7 +112,12 @@ PLy_cursor_query(const char *query)
|
||||
return NULL;
|
||||
cursor->portalname = NULL;
|
||||
cursor->closed = false;
|
||||
PLy_typeinfo_init(&cursor->result);
|
||||
cursor->mcxt = AllocSetContextCreate(TopMemoryContext,
|
||||
"PL/Python cursor context",
|
||||
ALLOCSET_DEFAULT_MINSIZE,
|
||||
ALLOCSET_DEFAULT_INITSIZE,
|
||||
ALLOCSET_DEFAULT_MAXSIZE);
|
||||
PLy_typeinfo_init(&cursor->result, cursor->mcxt);
|
||||
|
||||
oldcontext = CurrentMemoryContext;
|
||||
oldowner = CurrentResourceOwner;
|
||||
@@ -139,7 +145,7 @@ PLy_cursor_query(const char *query)
|
||||
elog(ERROR, "SPI_cursor_open() failed: %s",
|
||||
SPI_result_code_string(SPI_result));
|
||||
|
||||
cursor->portalname = PLy_strdup(portal->name);
|
||||
cursor->portalname = MemoryContextStrdup(cursor->mcxt, portal->name);
|
||||
|
||||
PLy_spi_subtransaction_commit(oldcontext, oldowner);
|
||||
}
|
||||
@@ -200,7 +206,12 @@ PLy_cursor_plan(PyObject *ob, PyObject *args)
|
||||
return NULL;
|
||||
cursor->portalname = NULL;
|
||||
cursor->closed = false;
|
||||
PLy_typeinfo_init(&cursor->result);
|
||||
cursor->mcxt = AllocSetContextCreate(TopMemoryContext,
|
||||
"PL/Python cursor context",
|
||||
ALLOCSET_DEFAULT_MINSIZE,
|
||||
ALLOCSET_DEFAULT_INITSIZE,
|
||||
ALLOCSET_DEFAULT_MAXSIZE);
|
||||
PLy_typeinfo_init(&cursor->result, cursor->mcxt);
|
||||
|
||||
oldcontext = CurrentMemoryContext;
|
||||
oldowner = CurrentResourceOwner;
|
||||
@@ -261,7 +272,7 @@ PLy_cursor_plan(PyObject *ob, PyObject *args)
|
||||
elog(ERROR, "SPI_cursor_open() failed: %s",
|
||||
SPI_result_code_string(SPI_result));
|
||||
|
||||
cursor->portalname = PLy_strdup(portal->name);
|
||||
cursor->portalname = MemoryContextStrdup(cursor->mcxt, portal->name);
|
||||
|
||||
PLy_spi_subtransaction_commit(oldcontext, oldowner);
|
||||
}
|
||||
@@ -315,12 +326,13 @@ PLy_cursor_dealloc(PyObject *arg)
|
||||
|
||||
if (PortalIsValid(portal))
|
||||
SPI_cursor_close(portal);
|
||||
cursor->closed = true;
|
||||
}
|
||||
if (cursor->mcxt)
|
||||
{
|
||||
MemoryContextDelete(cursor->mcxt);
|
||||
cursor->mcxt = NULL;
|
||||
}
|
||||
|
||||
PLy_free(cursor->portalname);
|
||||
cursor->portalname = NULL;
|
||||
|
||||
PLy_typeinfo_dealloc(&cursor->result);
|
||||
arg->ob_type->tp_free(arg);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user