Re: [PATCH] perf script python: Fix dict reference counting

From: Janne Huttunen
Date: Mon Jul 09 2018 - 05:26:55 EST


On Sun, 2018-07-08 at 13:17 +0200, Jiri Olsa wrote:
> On Fri, Jul 06, 2018 at 09:53:44AM +0300, Janne Huttunen wrote:
> >
> > The dictionaries are attached to the parameter tuple that steals the
> > references. The code should not decrement the reference counters
> > explicitly. Otherwise the objects might be released while they are
> > still in use which may cause perf crashes, assertions or just plain
> > weird behavior like unexpected data changes in stored objects.
> >
> > Signed-off-by: Janne Huttunen <janne.huttunen@xxxxxxxxx>
> > ---
> > Âtools/perf/util/scripting-engines/trace-event-python.c | 8 ++------
> > Â1 file changed, 2 insertions(+), 6 deletions(-)
> >
> > diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c
> > index 46e9e19..60fce44 100644
> > --- a/tools/perf/util/scripting-engines/trace-event-python.c
> > +++ b/tools/perf/util/scripting-engines/trace-event-python.c
> > @@ -908,14 +908,11 @@ static void python_process_tracepoint(struct perf_sample *sample,
> > Â if (_PyTuple_Resize(&t, n) == -1)
> > Â Py_FatalError("error resizing Python tuple");
> > Â
> > - if (!dict) {
> > + if (!dict)
> > Â call_object(handler, t, handler_name);
> > - } else {
> > + else
> > Â call_object(handler, t, default_handler_name);
> > - Py_DECREF(dict);
> > - }
> > Â
> > - Py_XDECREF(all_entries_dict);
> > Â Py_DECREF(t);
> > Â}
> > Â
> > @@ -1235,7 +1232,6 @@ static void python_process_general_event(struct perf_sample *sample,
> > Â
> > Â call_object(handler, t, handler_name);
> > Â
> > - Py_DECREF(dict);
> > Â Py_DECREF(t);
>
> so the dict is released when the tuple is released?

To the best of my knowledge, yes.

As far as I can see, there is only a single reference to each dict
and according to the Python documentation PyTuple_SetItem() "steals"
the reference passed to it. If so, afterwards the tuple owns the
only reference to the dict(s) and should take care of releasing
them when appropriate.

I even built libpython with reference debugging enabled and when I
run perf without the fix I get this:

Fatal Python error: Objects/tupleobject.c:238 object at 0x7f10f2041b40 has negative ref count -1
Aborted (core dumped)

With the fix I get no errors.