Attached is a patch that fixes three Python reference leaks in
PLy_traceback(): the objects returned by PyErr_Fetch() are owned by the
caller, so their reference count should be decremented. The memory leak
can be reproduced as follows:

create function import_fail() returns text as
'import foosocket
return "succeeded, that wasn''t supposed to happen"'
LANGUAGE plpythonu;

create function test_py() returns void as $$
for i in 1 .. 1000 loop
    perform import_fail();
end loop;
end;$$ language plpgsql;

select test_py();

On my system, each invocation of test_py() leaks about 2MB. With the
patch applied, each invocation leaks about 500KB. So obviously there are
some more leaks here -- I searched briefly for additional problems, but
couldn't see anything obvious.

I don't have time at the moment to track down the remaining problems, so
I'd like to apply the patch as-is: I'll come back to the remaining
memory leaks later, unless someone beats me to it.

Barring any objections, I'll apply this patch to HEAD and back branches


# old_revision [734d5f45e00c6d9d6072160562ea351d3e2ef238]
# patch "src/pl/plpython/plpython.c"
#  from [103ec857e125a1ca082966ee70f7a2d7dbc19928]
#    to [97efa8b35c7e6431677f738015f7f5bf86089791]
--- src/pl/plpython/plpython.c	103ec857e125a1ca082966ee70f7a2d7dbc19928
+++ src/pl/plpython/plpython.c	97efa8b35c7e6431677f738015f7f5bf86089791
@@ -2516,6 +2516,7 @@
 	PyErr_NormalizeException(&e, &v, &tb);
+	Py_XDECREF(tb);
 	eob = PyObject_Str(e);
 	if (v && ((vob = PyObject_Str(v)) != NULL))
@@ -2534,9 +2535,10 @@
+	Py_XDECREF(v);
-	 * intuit an appropriate error level for based on the exception type
+	 * intuit an appropriate error level based on the exception type
 	if (PLy_exc_error && PyErr_GivenExceptionMatches(e, PLy_exc_error))
 		*xlevel = ERROR;
@@ -2545,6 +2547,7 @@
 		*xlevel = ERROR;
+	Py_DECREF(e);
 	return xstr;
---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
       subscribe-nomail command to [EMAIL PROTECTED] so that your
       message can get through to the mailing list cleanly

Reply via email to