Skip to content

mgr: fix assertion failure in get_perf_schema_python for empty perf counters - #67156

Closed
zenjabba wants to merge 1 commit into
ceph:mainfrom
zenjabba:fix/mgr-telemetry-crash-empty-perf-counters
Closed

zenjabba wants to merge 1 commit into
ceph:mainfrom
zenjabba:fix/mgr-telemetry-crash-empty-perf-counters

Conversation

@zenjabba

@zenjabba zenjabba commented Feb 1, 2026

Copy link
Copy Markdown

Problem

When a daemon exists in daemon_state but has no perf counter instances (e.g., a destroyed OSD with null FSID 00000000-0000-0000-0000-000000000000), the get_perf_schema_python() function crashes with an assertion failure:

/ceph/rpmbuild/BUILD/ceph-20.2.0/src/mgr/PyFormatter.h: 84: FAILED ceph_assert(cursor != root)

Call stack:

ceph::__ceph_assert_fail()
  -> PyFormatter::close_section()
    -> ActivePyModules::get_perf_schema_python()

This causes MGR daemons to crash every 60-90 seconds when the telemetry module iterates through all OSDs and encounters a destroyed one.

Root Cause

In get_perf_schema_python(), when iterating through daemons:

  1. The function enters the daemon loop since daemons is not empty
  2. It creates a Formatter::ObjectSection for the daemon key (RAII-managed)
  3. The inner loop over state->perf_counters.instances never executes because the collection is empty
  4. Lines 1253-1254 unconditionally call f.close_section() twice
  5. These calls try to close 'counters' and 'counter object' sections that were never opened
  6. This violates the assertion cursor != root in PyFormatter::close_section()

Solution

Add an early continue to skip daemons with empty perf_counters.instances before entering the with_gil block. This prevents the crash while still properly handling daemons that do have perf counters.

Reproduction

  1. Have a Ceph cluster with telemetry module enabled
  2. Destroy an OSD without purging it:
    ceph osd destroy 123 --yes-i-really-mean-it
  3. Wait 60-90 seconds for telemetry to query perf schema
  4. MGR crashes with assertion failure

Testing

  • Verified that destroyed OSDs (with null FSID) are skipped
  • Normal OSDs with perf counters continue to work as expected
  • No assertion failures after the fix

Comparison with Similar Code

get_unlabeled_perf_schema_python() handles this case correctly because it opens a section before the loop and closes it after - properly balanced even if the loop is empty. This fix aligns get_perf_schema_python() with that pattern by avoiding the problematic code path entirely.

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

…ounters

When a daemon exists in daemon_state but has no perf counter instances
(e.g., a destroyed OSD with null FSID), the get_perf_schema_python()
function would crash with an assertion failure:

  /src/mgr/PyFormatter.h: 84: FAILED ceph_assert(cursor != root)

This occurred because the function unconditionally called close_section()
after iterating through perf_counters.instances, even when the collection
was empty and no sections had been opened.

The fix adds an early continue to skip daemons with empty perf counter
instances, preventing the crash while still properly handling daemons
that do have counters.

Reproduction scenario:
1. Destroy an OSD: ceph osd destroy <id> --yes-i-really-mean-it
2. Wait for telemetry module to query perf schema
3. MGR crashes with assertion failure

Fixes: https://tracker.ceph.com/issues/74693
Signed-off-by: Stephen Thompson <[email protected]>
@zenjabba
zenjabba requested a review from a team as a code owner February 1, 2026 22:28
@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

13 similar comments
@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@RXWatcher

Copy link
Copy Markdown

Can someone please review this PR?

@github-actions

github-actions Bot commented May 5, 2026

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had any activity for 60 days. It will be closed if no further activity occurs for another 30 days.
If you are a maintainer or core committer, please follow-up on this pull request to identify what steps should be taken by the author to move this proposed change forward.
If you are the author of this pull request, thank you for your proposed contribution. If you believe this change is still appropriate, please ensure that any feedback has been addressed and ask for a code review.

@github-actions github-actions Bot added the stale label May 5, 2026
@tchaikov

tchaikov commented May 6, 2026

Copy link
Copy Markdown
Contributor

ok to test

@github-actions github-actions Bot removed the stale label May 6, 2026
Comment on lines +1154 to +1156
// Skip daemons with no perf counter instances (e.g., destroyed OSDs)
// to avoid closing sections that were never opened
// Fixes: https://tracker.ceph.com/issues/74693

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.

please drop the "Fixes" line, as this is not ceph's convention. you already include this tag in the commit message.

// Fixes: https://tracker.ceph.com/issues/74693
if (state->perf_counters.instances.empty()) {
continue;
}

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.

instead of skipping the perf counters and changing the schema of the output. probably we can improve the formatting using Formatter::ArraySection to manage the section's lifecycle with RAII, like:

with_gil(no_gil, [&, key = ceph::to_string(key), state = state] {
  std::string key_name, prev_key_name;
  perf_counter_label_pairs prev_key_labels;
  Formatter::ObjectSection counter_section(f, key.c_str());
  std::optional<Formatter::ArraySection>  array_section;
  std::optional<Formatter::ObjectSection> counter_object_section;
  std::optional<Formatter::ObjectSection> counters_section;

  for (const auto &[counter_name_with_labels, _] :
       state->perf_counters.instances) {
    // ... compute key_name, key_labels, type ...

    if (prev_key_name != key_name) {
      // reset closes 'counters' then 'counter object' (declaration order,
      // reversed at destruction) before emplacing the new array section
      counters_section.reset();
      counter_object_section.reset();
      prev_key_name = key_name;
      prev_key_labels = key_labels;
      array_section.emplace(f, key_name);
      counter_object_section.emplace(f, "");
      for (Formatter::ObjectSection ls{f, "labels"};
           const auto &label : key_labels) {
        f.dump_string(label.first, label.second);
      }
      counters_section.emplace(f, "counters");
      dump_sub_counter_information(&f, type);
    } else if (prev_key_name == key_name && prev_key_labels == key_labels) {
      dump_sub_counter_information(&f, type);
    } else {
      counters_section.reset();
      counter_object_section.reset();
      counter_object_section.emplace(f, "");
      for (Formatter::ObjectSection ls{f, "labels"};
           const auto &label : key_labels) {
        f.dump_string(label.first, label.second);
      }
      counters_section.emplace(f, "counters");
      dump_sub_counter_information(&f, type);
    }
  }
  // no close_section() calls needed -- all sections close via RAII
});

what do you think?

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.

+1ing this approach, I was working on a branch to resolve this issue to support unit tests within the Mgr, and have pushed similar changes here, some of the comments are superfluous but I had them in while working through implementation:
main...stzuraski898:ceph:wip-sz-75745-OSD-perf-schema-no-counters

@stzuraski898

Copy link
Copy Markdown
Contributor

It appears that a PR was merged yesterday in response to https://tracker.ceph.com/issues/75745, which was made in response to a similar issue found during unit testing. I believe we'll need to rebase onto main when implementing the RAII approach.

This PR can be found here:

@stzuraski898

Copy link
Copy Markdown
Contributor

I've opened a PR to follow up on your requested changes, to move this solution forward @tchaikov.

This can be found here: #69181

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Thank you for your contribution. Since you, the author, are not a member of the Ceph GitHub Org yet, our CI will not automatically run. Any member of the Ceph Org may comment "ok - to - test" (without the dashes) to allow the Jenkins jobs to run.

@ronen-fr

ronen-fr commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

ok to test

@stzuraski898

Copy link
Copy Markdown
Contributor

I believe this PR is redundant, #69181 has been merged and covers the changes requested by Kefu in this PR's review.

@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had any activity for 60 days. It will be closed if no further activity occurs for another 30 days.
If you are a maintainer or core committer, please follow-up on this pull request to identify what steps should be taken by the author to move this proposed change forward.
If you are the author of this pull request, thank you for your proposed contribution. If you believe this change is still appropriate, please ensure that any feedback has been addressed and ask for a code review.

@github-actions github-actions Bot added the stale label Sep 21, 2026
@github-actions

Copy link
Copy Markdown

This pull request has been automatically closed because there has been no activity for 90 days. Please feel free to reopen this pull request (or open a new one) if the proposed change is still appropriate. Thank you for your contribution!

@github-actions github-actions Bot closed this Oct 21, 2026
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.

6 participants