Skip to content

mgr: free erased MgrMapCache python objects - #72727

Merged
NitzanMordhai merged 2 commits into
ceph:mainfrom
NitzanMordhai:wip-nitzan-mgr-cache-leak-with-prometheus
Nov 1, 2026
Merged

NitzanMordhai merged 2 commits into
ceph:mainfrom
NitzanMordhai:wip-nitzan-mgr-cache-leak-with-prometheus

Conversation

@NitzanMordhai

@NitzanMordhai NitzanMordhai commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

MgrMapCache<PyObject*>::erase() and insert() dropped erased, replaced and evicted entries through Py_AddPendingCall(). That only queues the call for the main thread of the main interpreter, and the ceph-mgr main thread never runs Python, so the queue is never drained; once it is full the call fails and erase() ignored the failure. Either way every erased entry kept its reference. With prometheus scraping a 10K PG cluster, each invalidated pg_dump leaked about 90MB and the mgr grew ~400MB per minute.

erase() runs from notify paths that hold ActivePyModules::lock without the GIL, so queue the objects there and drop them on the next get() or insert(), which already require the GIL. insert() now drops replaced and evicted entries directly.

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

Assisted-by: Claude

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.

@NitzanMordhai
NitzanMordhai requested a review from a team as a code owner October 15, 2026 09:45
@NitzanMordhai
NitzanMordhai force-pushed the wip-nitzan-mgr-cache-leak-with-prometheus branch from 7eb391d to 6005e59 Compare October 15, 2026 09:48

@sseshasa sseshasa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code changes LGTM. The only major comment is around the enhancement of the existing unit test if feasible.

Comment thread src/mgr/MgrMapCache.cc Resolved
Comment thread src/mgr/MgrMapCache.cc Resolved
Comment thread src/mgr/MgrMapCache.cc Resolved
MgrMapCache<PyObject*>::erase() and insert() dropped erased, replaced
and evicted entries through Py_AddPendingCall(). That only queues the
call for the main thread of the main interpreter, and the ceph-mgr main
thread never runs Python, so the queue is never drained; once it is
full the call fails and erase() ignored the failure. Either way every
erased entry kept its reference. With prometheus scraping a 10K PG
cluster, each invalidated pg_dump leaked about 90MB and the mgr grew
~400MB per minute.

erase() runs from notify paths that hold ActivePyModules::lock without
the GIL, so queue the objects there and drop them on the next get() or
insert(), which already require the GIL. insert() now drops replaced
and evicted entries directly.

Fixes: https://tracker.ceph.com/issues/81794
Signed-off-by: Nitzan Mordechai <[email protected]>
Assisted-by: Claude
@NitzanMordhai
NitzanMordhai force-pushed the wip-nitzan-mgr-cache-leak-with-prometheus branch from 6005e59 to 457cf4a Compare October 21, 2026 12:51

@sseshasa sseshasa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM and approving. Please consider adding a couple of additional tests as mentioned in my latest comment.

Py_DECREF(a);
Py_DECREF(b);
}

@sseshasa sseshasa Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • How about a test that perform repeated insert(), get() and erase() pattern in a loop many times mimicking the tracker's Prometheus sequence? This would be a reproducer for the tracker.

  • Also, a small concurrent test to verify that the locking mechanism is sound would help.

The test commit can be squashed with the main commit.

Check the refcount of cached python objects after insert, get,
replace, evict, erase and clear. The erase tests release the GIL
before erase(), as the notify paths do, and check that the object is
only dropped on the next get() or insert(). These fail on the old
Py_AddPendingCall() code.

Give the test its own main() that calls global_init, since the
MgrMapCache constructor reads g_conf().

Fixes: https://tracker.ceph.com/issues/81794
Signed-off-by: Nitzan Mordechai <[email protected]>
Assisted-by: Claude
@SrinivasaBharath

Copy link
Copy Markdown
Contributor

This PR has been added to wip-bharath10-testing-20261027.070130-main.

Tanguille added a commit to Tanguille/cluster that referenced this pull request Oct 29, 2026
The active mgr grew ~230 MiB/h on 21.2.0 and OOMed at its 2Gi limit every ~8.5h. Upstream fix is ceph/ceph#72727.
@NitzanMordhai

Copy link
Copy Markdown
Contributor Author

@NitzanMordhai
NitzanMordhai merged commit 4f19942 into ceph:main Nov 1, 2026
16 checks passed
@NitzanMordhai
NitzanMordhai deleted the wip-nitzan-mgr-cache-leak-with-prometheus branch November 1, 2026 07:48
@tchaikov tchaikov mentioned this pull request Nov 3, 2026
4 of 14 tasks
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.

3 participants