From 8c75ad436f75fc629b61f601ba884c8f9313c9af Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 5 Nov 2015 13:52:30 -0500 Subject: 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 --- src/pl/plpython/plpy_main.c | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) (limited to 'src/pl/plpython/plpy_main.c') diff --git a/src/pl/plpython/plpy_main.c b/src/pl/plpython/plpy_main.c index 63a284e2384..3c2ebfa16af 100644 --- a/src/pl/plpython/plpy_main.c +++ b/src/pl/plpython/plpy_main.c @@ -277,7 +277,12 @@ plpython_inline_handler(PG_FUNCTION_ARGS) flinfo.fn_mcxt = CurrentMemoryContext; MemSet(&proc, 0, sizeof(PLyProcedure)); - proc.pyname = PLy_strdup("__plpython_inline_block"); + proc.mcxt = AllocSetContextCreate(TopMemoryContext, + "__plpython_inline_block", + ALLOCSET_DEFAULT_MINSIZE, + ALLOCSET_DEFAULT_INITSIZE, + ALLOCSET_DEFAULT_MAXSIZE); + proc.pyname = MemoryContextStrdup(proc.mcxt, "__plpython_inline_block"); proc.langid = codeblock->langOid; proc.result.out.d.typoid = VOIDOID; @@ -364,17 +369,32 @@ PLy_current_execution_context(void) return PLy_execution_contexts; } +MemoryContext +PLy_get_scratch_context(PLyExecutionContext *context) +{ + /* + * A scratch context might never be needed in a given plpython procedure, + * so allocate it on first request. + */ + if (context->scratch_ctx == NULL) + context->scratch_ctx = + AllocSetContextCreate(TopTransactionContext, + "PL/Python scratch context", + ALLOCSET_DEFAULT_MINSIZE, + ALLOCSET_DEFAULT_INITSIZE, + ALLOCSET_DEFAULT_MAXSIZE); + return context->scratch_ctx; +} + static PLyExecutionContext * PLy_push_execution_context(void) { - PLyExecutionContext *context = PLy_malloc(sizeof(PLyExecutionContext)); + PLyExecutionContext *context; + context = (PLyExecutionContext *) + MemoryContextAlloc(TopTransactionContext, sizeof(PLyExecutionContext)); context->curr_proc = NULL; - context->scratch_ctx = AllocSetContextCreate(TopTransactionContext, - "PL/Python scratch context", - ALLOCSET_DEFAULT_MINSIZE, - ALLOCSET_DEFAULT_INITSIZE, - ALLOCSET_DEFAULT_MAXSIZE); + context->scratch_ctx = NULL; context->next = PLy_execution_contexts; PLy_execution_contexts = context; return context; @@ -390,6 +410,7 @@ PLy_pop_execution_context(void) PLy_execution_contexts = context->next; - MemoryContextDelete(context->scratch_ctx); - PLy_free(context); + if (context->scratch_ctx) + MemoryContextDelete(context->scratch_ctx); + pfree(context); } -- cgit v1.2.3