Skip to content

mgr: fix PyObject leak in the remote() pickle calls - #69755

Merged
tchaikov merged 1 commit into
ceph:mainfrom
tchaikov:wip-mgr-remote-pyunicode-leak
Jun 29, 2026
Merged

tchaikov merged 1 commit into
ceph:mainfrom
tchaikov:wip-mgr-remote-pyunicode-leak

Conversation

@tchaikov

@tchaikov tchaikov commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

dispatch_remote() and ceph_dispatch_remote() invoke pickle.loads/dumps via PyObject_CallMethodObjArgs(pmodule, PyUnicode_FromString("loads"), ...). The method-name string created by PyUnicode_FromString is a new reference that CallMethodObjArgs does not steal, and it was never released, so every cross -module remote() call leaked three PyUnicode objects on the active mgr.

Use PyObject_CallMethod(pmodule, "loads", "O", arg), which takes the method name as a C string and manages it internally, so there is nothing to leak.

Fixes: https://tracker.ceph.com/issues/77724

Contribution Guidelines

  • To sign and title your commits, please refer to Submitting Patches to Ceph.

  • If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.

  • When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands

You must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.

@tchaikov
tchaikov requested a review from a team as a code owner June 26, 2026 00:55
@tchaikov

Copy link
Copy Markdown
Contributor Author

the reason why valgrind qa run failed to flag it:

ceph/qa/valgrind.supp

Lines 825 to 832 in 96e268b

{
ignore *all* ceph-mgr python crap. this is overkill, but better than nothing
Memcheck:Leak
match-leak-kinds: all
...
fun:*Py*
...
}

dispatch_remote() and ceph_dispatch_remote() invoke pickle.loads/dumps via
PyObject_CallMethodObjArgs(pmodule, PyUnicode_FromString("loads"), ...). The
method-name string created by PyUnicode_FromString is a new reference that
CallMethodObjArgs does not steal, and it was never released, so every cross
-module remote() call leaked three PyUnicode objects on the active mgr.

Use PyObject_CallMethod(pmodule, "loads", "(O)", arg), which takes the method
name as a C string and manages it internally, so there is nothing to leak.
The "(O)" format wraps the argument in a one-tuple so it reaches the callee as
a single object. A bare "O" passes the argument through directly, and when it
is a tuple (as remote()'s *args always is, including the empty tuple from a
no-argument call) PyObject_CallMethod unpacks it and calls dumps() with the
wrong arguments, e.g. dumps() with none.

Fixes: https://tracker.ceph.com/issues/77724
Signed-off-by: Kefu Chai <[email protected]>
@tchaikov
tchaikov force-pushed the wip-mgr-remote-pyunicode-leak branch from a4eaaf0 to 8ae44f6 Compare June 26, 2026 04:30
@mheler

mheler commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

lgtm

@tchaikov

Copy link
Copy Markdown
Contributor Author

lgtm

@mheler hi Matthew, thanks for reviewing and approving this change. in addition to commenting it, could you actually approve this change by "approving" it in github? otherwise we cannot merged this change, even it passes the integration tests.

@NitzanMordhai

Copy link
Copy Markdown
Contributor

@tchaikov
tchaikov merged commit 66547e6 into ceph:main Jun 29, 2026
6 checks passed
@epuertat

Copy link
Copy Markdown
Member

@tchaikov: I'm curious, how are we catching these? Aren't we enabling valgrind on some test runs? This should? be easy to catch even with simple vstart testing, as long as all mgr API calls are invoked.

@tchaikov

tchaikov commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor Author

@epuertat i am afraid that valgrind cannot flag a "leak" like this, because we practically prevent valgrind / ASan from identifying these leaks with suppression rules see

ceph/qa/lsan.supp

Lines 15 to 41 in 40e8ea0

leak:^_PyObject_Alloc
leak:^_PyObject_GC_Resize
leak:^PyBytes_FromStringAndSize
leak:^PyType_GenericAlloc
leak:^set_table_resize
# python3.7
leak:^_PyObject_Realloc
leak:^PyObject_Malloc
# python3.8
leak:^_PyBytes_FromSize
leak:^_PyObject_GC_Alloc
leak:^PyMem_Calloc
leak:^PyUnicode_New
# python3.9, 3.10, 3.11
leak:^PyMem_Malloc
leak:^PyDict_Copy
leak:^_PyObject_GC_NewVar
# Catch unsymbolised interpreter frames in the standalone python binary used by
# the bin/ceph wrapper and in the libpython embedded by C++ unittests such as
# unittest_mgr_pyformatter / unittest_mgr_pyutil. Not all CPython internal
# helpers are exported in .dynsym, so the function-name matches above don't
# cover everything; this substring-matches the module path in the stack frame.
leak:python3.10
leak:python3.12
leak:python3.13
# python3.13
leak:^PyModule_ExecDef

i implemented a tracemalloc and gc based mgr module, in order to trace the leak reported by https://tracker.ceph.com/issues/77633 . unfortunately, this module helped to identify the issue addressed by this pull request, but not the one in the tracker ticket. if you are interested in it, i can send it as a pull request.

@tchaikov
tchaikov deleted the wip-mgr-remote-pyunicode-leak branch July 4, 2026 00:09
@epuertat

epuertat commented Jul 6, 2026

Copy link
Copy Markdown
Member

@tchaikov interesting! And why are we suppressing those?

I did a quick AI chat and got the following ideas:

  • Compiling with --with-pydebug and using _Py_GetRefTotal(), PYTHONDUMPREFS (CLI), or sys.gettotalrefcount (Python side).
  • Write a RAII wrapper, like PyRef with a destructor (I guess that won't work, with object passing between C++ and Python lands, right?):
    ~PyRef()
    {
        Py_XDECREF(ptr);
    }
  • It was also suggesting PyObject_CallOneArg instead of PyObject_CallMethod since loads/dumps() only have 1 method.
  • It also suggested using the pickle.HIGHEST_PROTOCOL on dumps(). In Python 3.9 the default one is 4, but protocol 5 is already available (improved support for zero-copy/out-of-band buffers).
  • A dummy mgr module trigger all ceph-mgr API methods (with inspection/reflection that's kinda trivial).

And I can check that PR too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants