Discussion:
[Python.NET] Several Python.Runtime problems
Alexey Borzenkov
2012-01-23 20:33:32 UTC
Permalink
Hello,

While using Python.NET in one of my projects I found some problems:

1. PythonException uses Runtime.PyErr_Fetch before calling
AcquireLock, which is wrong. Here's the patch:

--- src/runtime/pythonexception.cs (revision 138)
+++ src/runtime/pythonexception.cs (working copy)
@@ -27,11 +27,11 @@

public PythonException() : base()
{
+ IntPtr gs = PythonEngine.AcquireLock();
Runtime.PyErr_Fetch(ref _pyType, ref _pyValue, ref _pyTB);
Runtime.Incref(_pyType);
Runtime.Incref(_pyValue);
Runtime.Incref(_pyTB);
- IntPtr gs = PythonEngine.AcquireLock();
if ((_pyType != IntPtr.Zero) && (_pyValue != IntPtr.Zero))
{
string type = new PyObject(_pyType).GetAttr("__name__").ToString();

2. Every time .NET code throws an exception Python.NET leaks memory,
because exception object is not decref'ed after calling
PyErr_SetObject. In my case there were lots of exceptions, so problem
was very noticable over time. Here's the patch:

-- src/runtime/exceptions.cs (revision 138)
+++ src/runtime/exceptions.cs (working copy)
@@ -473,6 +473,7 @@
IntPtr etype = Runtime.PyObject_GetAttrString(op, "__class__");
Runtime.PyErr_SetObject(etype, op);
Runtime.Decref(etype);
+ Runtime.Decref(op);
}

/// <summary>

Hope this helps others in their fights for thread safety and against
memory leaks. :)

Thanks,
Alexey.
_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org
http://mail.python.org/mailman/listinfo/pythondotnet
Barton
2012-01-26 08:11:14 UTC
Permalink
Received: from localhost (HELO mail.python.org) (127.0.0.1)
by albatross.python.org with SMTP; 26 Jan 2012 09:11:02 +0100
Received: from nm19-vm0.access.bullet.mail.mud.yahoo.com
(nm19-vm0.access.bullet.mail.mud.yahoo.com [66.94.236.25])
by mail.python.org (Postfix) with SMTP
for <pythondotnet-+ZN9ApsXKcEdnm+***@public.gmane.org>; Thu, 26 Jan 2012 09:11:01 +0100 (CET)
Received: from [66.94.237.199] by nm19.access.bullet.mail.mud.yahoo.com with
NNFMP; 26 Jan 2012 08:10:55 -0000
Received: from [98.137.12.179] by tm10.access.bullet.mail.mud.yahoo.com with
NNFMP; 26 Jan 2012 08:10:55 -0000
Received: from [127.0.0.1] by smtp104.biz.mail.gq1.yahoo.com with NNFMP;
26 Jan 2012 08:10:55 -0000
X-Yahoo-Newman-Id: 34966.43378.bm-***@public.gmane.org
X-Yahoo-Newman-Property: ymail-3
X-YMail-OSG: k1WnaJwVM1kwVaAmXZeC0hSJuam2CqFz9XyqZdzzgIEPf4r
lay27VHwmQv4htV7pZ9xxotTEAiQ_8C3J7pkKCPxaxeblUJqX8FDUUsez0qk
1EiO0eeke0Mj5D3b5cScMR.0jFukVvBeHCPl_cpuqUils7ql6xh5xzf74ypS
aj1bj.tqjRXSPbAF_R8sYjz.ic_v_H4XQWCaVZ28E9gAXUfHawvJhxXbCUuO
3PTfQLFfpJQveuHgU4WgyCyh49_UxzoeJmp.3gwpp4AMLUUWWWUkdH1tXPU1
c4ToNBZRcvYoznFPDMUWk2ZxQItilTGZn7tv3dnOLQzHGm9yb9zVH80852t3
tfgBqLWtsxosQc2xGbbrfnzPHv0WOXMF7kJdlTtLt6vRtOJRKwcvmaFybRLv
UOO1oNLAaOAgopa0ywv.xsvtIOxoUgCnUYHZEWGGIP0Fs6.QoSvHVsKrz3O1
_YLK_reWCm6MTv2EIjaQcvHiA.Fjwfi697cUBKoRaHc8nVXwYqPWr11CRlcs
2PjCkLpHw_UQjK9RIGV75xTjpryE81Oi8KK8txD4HF9792Xy8DruKUXrJe8l
XcDUsOF5esZpjk7zSx3aLoL1daogObN_rSF7oGADkZ.b2SpMWL7k6YGiH2Fy EbQ--
X-Yahoo-SMTP: U71VqzGswBDO9FrcuPHuUtnn.9UhQIfe
Received: from [192.168.0.102] (barton-***@public.gmane.org with plain)
by smtp104.biz.mail.gq1.yahoo.com with SMTP;
26 Jan 2012 00:10:54 -0800 PST
User-Agent: Mozilla/5.0 (Windows NT 6.1;
rv:9.0) Gecko/20111222 Thunderbird/9.0.1
In-Reply-To: <CAKwjive4CSdYc0kmQyJQ9hZZKMaUveFz018XXx7RFxxOVQrwGg-JsoAwUIsXosN+***@public.gmane.org>
X-BeenThere: pythondotnet-+ZN9ApsXKcEdnm+***@public.gmane.org
X-Mailman-Version: 2.1.12
Precedence: list
List-Id: "A list for users and developers of Python for .NET"
<pythondotnet.python.org>
List-Unsubscribe: <http://mail.python.org/mailman/options/pythondotnet>,
<mailto:pythondotnet-request-+ZN9ApsXKcEdnm+***@public.gmane.org?subject=unsubscribe>
List-Archive: <http://mail.python.org/pipermail/pythondotnet>
List-Post: <mailto:pythondotnet-+ZN9ApsXKcEdnm+***@public.gmane.org>
List-Help: <mailto:pythondotnet-request-+ZN9ApsXKcEdnm+***@public.gmane.org?subject=help>
List-Subscribe: <http://mail.python.org/mailman/listinfo/pythondotnet>,
<mailto:pythondotnet-request-+ZN9ApsXKcEdnm+***@public.gmane.org?subject=subscribe>
Sender: pythondotnet-bounces+gcpd-pythondotnet=m.gmane.org-+ZN9ApsXKcEdnm+***@public.gmane.org
Errors-To: pythondotnet-bounces+gcpd-pythondotnet=m.gmane.org-+ZN9ApsXKcEdnm+***@public.gmane.org
Archived-At: <http://permalink.gmane.org/gmane.comp.python.dotnet/1166>

Thanks, Alexey,
I've applied your patches to the trunk.
It's great to have on the project!
Post by Alexey Borzenkov
Hello,
1. PythonException uses Runtime.PyErr_Fetch before calling
--- src/runtime/pythonexception.cs (revision 138)
+++ src/runtime/pythonexception.cs (working copy)
@@ -27,11 +27,11 @@
public PythonException() : base()
{
+ IntPtr gs = PythonEngine.AcquireLock();
Runtime.PyErr_Fetch(ref _pyType, ref _pyValue, ref _pyTB);
Runtime.Incref(_pyType);
Runtime.Incref(_pyValue);
Runtime.Incref(_pyTB);
- IntPtr gs = PythonEngine.AcquireLock();
if ((_pyType != IntPtr.Zero)&& (_pyValue != IntPtr.Zero))
{
string type = new PyObject(_pyType).GetAttr("__name__").ToString();
2. Every time .NET code throws an exception Python.NET leaks memory,
because exception object is not decref'ed after calling
PyErr_SetObject. In my case there were lots of exceptions, so problem
-- src/runtime/exceptions.cs (revision 138)
+++ src/runtime/exceptions.cs (working copy)
@@ -473,6 +473,7 @@
IntPtr etype = Runtime.PyObject_GetAttrString(op, "__class__");
Runtime.PyErr_SetObject(etype, op);
Runtime.Decref(etype);
+ Runtime.Decref(op);
}
///<summary>
Hope this helps others in their fights for thread safety and against
memory leaks. :)
Thanks,
Alexey.
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org
http://mail.python.org/mailman/listinfo/pythondotnet

Loading...