Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several problems with _lsprof_Profiler_enable #126425

Closed
sobolevn opened this issue Nov 5, 2024 · 0 comments
Closed

Several problems with _lsprof_Profiler_enable #126425

sobolevn opened this issue Nov 5, 2024 · 0 comments
Assignees
Labels
extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

sobolevn commented Nov 5, 2024

Bug report

There are several problems that I see with _lsprof.Profiler.enable() method:

  • cpython/Modules/_lsprof.c

    Lines 783 to 787 in d384050

    if (PyObject_CallMethod(monitoring, "use_tool_id", "is", self->tool_id, "cProfile") == NULL) {
    PyErr_Format(PyExc_ValueError, "Another profiling tool is already active");
    Py_DECREF(monitoring);
    return NULL;
    }
    This code implictly overrides the set exception, I would prefer to have an explicit PyErr_Clear() call to show that this is intentional
  • Also, the same code does not decrement the refcount of the result, it is None right now (which is immortal), but we still prefer to do the refcount for such objects, where possible
  • This code ignores possible exceptions:

    cpython/Modules/_lsprof.c

    Lines 795 to 799 in d384050

    Py_XDECREF(PyObject_CallMethod(monitoring, "register_callback", "iiO", self->tool_id,
    (1 << callback_table[i].event),
    callback));
    Py_DECREF(callback);
    all_events |= (1 << callback_table[i].event);

I will send a PR.

Linked PRs

@sobolevn sobolevn added type-bug An unexpected behavior, bug, or error extension-modules C modules in the Modules dir labels Nov 5, 2024
@sobolevn sobolevn self-assigned this Nov 5, 2024
sobolevn added a commit to sobolevn/cpython that referenced this issue Nov 5, 2024
sobolevn added a commit that referenced this issue Nov 5, 2024
- Explicit memory management for `None` objects (since we still try to treat immortal objects as regular objects)
- Respect possible errors of `sys.monitoring.register_callback` call
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 5, 2024
- Explicit memory management for `None` objects (since we still try to treat immortal objects as regular objects)
- Respect possible errors of `sys.monitoring.register_callback` call
(cherry picked from commit 7587260)

Co-authored-by: sobolevn <mail@sobolevn.me>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 5, 2024
- Explicit memory management for `None` objects (since we still try to treat immortal objects as regular objects)
- Respect possible errors of `sys.monitoring.register_callback` call
(cherry picked from commit 7587260)

Co-authored-by: sobolevn <mail@sobolevn.me>
sobolevn added a commit that referenced this issue Nov 5, 2024
…6443)

gh-126425: Refactor `_lsprof_Profiler_enable` (GH-126426)

- Explicit memory management for `None` objects (since we still try to treat immortal objects as regular objects)
- Respect possible errors of `sys.monitoring.register_callback` call
(cherry picked from commit 7587260)

Co-authored-by: sobolevn <mail@sobolevn.me>
sobolevn added a commit that referenced this issue Nov 5, 2024
…6442)

gh-126425: Refactor `_lsprof_Profiler_enable` (GH-126426)

- Explicit memory management for `None` objects (since we still try to treat immortal objects as regular objects)
- Respect possible errors of `sys.monitoring.register_callback` call
(cherry picked from commit 7587260)

Co-authored-by: sobolevn <mail@sobolevn.me>
@sobolevn sobolevn closed this as completed Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant