Re: [PATCH v6 20/59] perf python: Extend API for stat events in python.c

sashiko-bot
2026-04-25T18:19:16Z

Sashiko AI review found 1 potential issue(s):
- [High] Unhandled Python exception and incompatible callback signature in `pyrf_session_tool__stat_round()`.
--

commit 08c1c5bd9c71b8f41c97d2c5bfb3c2a35ba3f92d
Author: Ian Rogers <irogers@google.com>

perf python: Extend API for stat events in python.c

This commit adds stat information to the session and introduces callbacks
for stat events in the Python API.

> diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
> index b358852b3528..37764a287a71 100644
> --- a/tools/perf/util/python.c
> +++ b/tools/perf/util/python.c
[ ... ]
> @@ -3051,6 +3166,47 @@ static int pyrf_session_tool__sample(const struct perf_tool *tool,
> return 0;
> }
>
> +static int pyrf_session_tool__stat(const struct perf_tool *tool,
> + struct perf_session *session,
> + union perf_event *event)
> +{
> + struct pyrf_session *psession = container_of(tool, struct pyrf_session, tool);
> + struct evsel *evsel = evlist__id2evsel(session->evlist, event->stat.id);
> + PyObject *pyevent = pyrf_event__new(event, evsel, psession->session);
> + const char *name = evsel ? evsel__name(evsel) : "unknown";
> + PyObject *ret;
> +
> + if (pyevent == NULL)
> + return -ENOMEM;
> +
> + ret = PyObject_CallFunction(psession->stat, "Os", pyevent, name);
> + if (!ret) {
> + PyErr_Print();
> + Py_DECREF(pyevent);
> + return -1;
> + }
> + Py_DECREF(ret);
> + Py_DECREF(pyevent);
> + return 0;
> +}
> +
> +static int pyrf_session_tool__stat_round(const struct perf_tool *tool,
> + struct perf_session *session __maybe_unused,
> + union perf_event *event)
> +{
> + struct pyrf_session *psession = container_of(tool, struct pyrf_session, tool);
> + PyObject *pyevent = pyrf_event__new(event, /*evsel=*/NULL, psession->session);
> + PyObject *ret;
> +
> + if (pyevent == NULL)
> + return -ENOMEM;
> +
> + ret = PyObject_CallFunction(psession->stat, "O", pyevent);

Since both the stat and stat_round event handlers use the same user-provided
psession->stat callback, will this mismatch in arguments cause a TypeError?

The pyrf_session_tool__stat() function calls it with two arguments (the event
and a name string), while this function only passes one argument (the event).

> + Py_XDECREF(ret);

If PyObject_CallFunction() fails (for example, due to the argument mismatch
mentioned above), it will return NULL and leave an active Python exception in
the thread state.

Since this function doesn't check for ret == NULL to clear the exception or
return an error (unlike pyrf_session_tool__stat() which calls PyErr_Print()),
will returning 0 here leave a pending exception that could cause the Python
interpreter to crash on subsequent C-API calls?

> + Py_DECREF(pyevent);
> + return 0;
> +}

--
Sashiko AI review · https://sashiko.dev/#/patchset/20260425174858.3922152-1-irogers@google.com?part=20