Skip to content

[dashboard] Guard against zero num_cpus in k8s_utils.cpu_percent (#63729) - #63733

Merged
edoakes merged 4 commits into
ray-project:masterfrom
Lawson-Darrow:fix/k8s-cpu-percent-zero-num-cpus-63729
Jun 2, 2026
Merged

edoakes merged 4 commits into
ray-project:masterfrom
Lawson-Darrow:fix/k8s-cpu-percent-zero-num-cpus-63729

Conversation

@Lawson-Darrow

Copy link
Copy Markdown
Contributor

Why are these changes needed?

ray.dashboard.k8s_utils.cpu_percent() computes:

cpu_percent = round(quotient * 100 / get_num_cpus(), 1)

A Ray node can legitimately be registered with 0 CPUs — in fact num-cpus: "0" on the head node is the recommended KubeRay configuration (to keep tasks off the head). On any such pod, get_num_cpus() returns 0, so this line raises ZeroDivisionError on every metrics poll cycle.

The exception is caught (so 0.0 is still returned and behavior is functionally correct), but logger.exception(...) writes a full traceback to the dashboard-agent logs every cycle, which is noisy and misleading.

This is the same class of bug already guarded against elsewhere in the dashboard — reporter_agent._get_load_avg() uses if self._cpu_counts[0] > 0: ... else: .... This PR applies the same guard to cpu_percent(): return 0.0 when get_num_cpus() == 0.

Related issue number

Fixes #63729

Checks

  • Added test_k8s_cpu_percent_zero_num_cpus to test_advanced_8.py. The discriminating assertion is logger.exception.assert_not_called(): without the fix the ZeroDivisionError is caught and logged every cycle, so the test would fail; with the fix no exception is raised or logged.
  • The fix logic was verified in isolation (0-CPU path returns 0.0 with no logged exception). ruff check passes on both changed files.

Disclosure: this change was prepared with the assistance of Claude Code; I have reviewed it and take responsibility for it.

🤖 Generated with Claude Code

@gemini-code-assist gemini-code-assist Bot 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 Review

This pull request prevents a potential ZeroDivisionError in cpu_percent() when a node is registered with 0 CPUs (such as KubeRay head nodes) and adds a corresponding regression test. The reviewer suggested a defensive programming improvement to guard against any non-positive CPU count (num_cpus <= 0) instead of strictly checking for zero.

Comment thread python/ray/dashboard/k8s_utils.py Outdated
Comment on lines +53 to +56
if num_cpus == 0:
cpu_percent = 0.0
else:
cpu_percent = round(quotient * 100 / num_cpus, 1)

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.

medium

For defensive programming, it is safer to guard against any non-positive CPU count (i.e., num_cpus <= 0) rather than strictly checking for num_cpus == 0. This prevents potential issues (such as negative CPU percentages or division by zero) if the system configuration or container limits ever report a negative or invalid CPU count.

Suggested change
if num_cpus == 0:
cpu_percent = 0.0
else:
cpu_percent = round(quotient * 100 / num_cpus, 1)
if num_cpus <= 0:
cpu_percent = 0.0
else:
cpu_percent = round(quotient * 100 / num_cpus, 1)

@edoakes

edoakes commented May 29, 2026

Copy link
Copy Markdown
Collaborator

@rueian PTAL

@ray-gardener ray-gardener Bot added dashboard Issues specific to the Ray Dashboard core Issues that should be addressed in Ray Core observability Issues related to the Ray Dashboard, Logging, Metrics, Tracing, and/or Profiling community-contribution Contributed by the community labels May 30, 2026
Lawson-Darrow and others added 2 commits May 31, 2026 14:20
A Ray node can be registered with 0 CPUs (the recommended `num-cpus: "0"`
for KubeRay head nodes). `k8s_utils.cpu_percent()` divides by
`get_num_cpus()`, so on such a node it raised `ZeroDivisionError` on every
metrics poll. The exception was caught (so 0.0 was still returned), but a
full traceback was logged each cycle, spamming the dashboard-agent logs.

Guard the division: return 0.0 when `get_num_cpus()` is 0, mirroring the
existing zero-CPU guard in `reporter_agent._get_load_avg`. Adds a regression
test asserting no exception is logged in that case.

Fixes ray-project#63729

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: Lawson Darrow <[email protected]>
Use `num_cpus <= 0` rather than `== 0` per review feedback, to
defensively handle any non-positive CPU count.

Signed-off-by: Lawson Darrow <[email protected]>
@Lawson-Darrow
Lawson-Darrow force-pushed the fix/k8s-cpu-percent-zero-num-cpus-63729 branch from a9777cb to 2c7d1ac Compare May 31, 2026 18:20
Comment thread python/ray/tests/test_advanced_8.py Outdated
assert 50 < k8s_utils.cpu_percent() < 60


def test_k8s_cpu_percent_zero_num_cpus():

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The level of mocking required for this test adds maintenance burden and there is little utility to it. Let's remove it. Otherwise the change lgtm

@edoakes edoakes added the go add ONLY when ready to merge, run all tests label Jun 1, 2026
edoakes noted the regression test's mocking adds maintenance burden with
little utility; the guard is already covered by the existing k8s_utils test.
Removing it.

Signed-off-by: Lawson Darrow <[email protected]>
@Lawson-Darrow

Copy link
Copy Markdown
Contributor Author

Removed the test, thanks for the review!

@edoakes
edoakes merged commit a3f6235 into ray-project:master Jun 2, 2026
3 checks passed
@edoakes

edoakes commented Jun 2, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the contribution

rueian pushed a commit to rueian/ray that referenced this pull request Jun 4, 2026
…-project#63729) (ray-project#63733)

## Why are these changes needed?

`ray.dashboard.k8s_utils.cpu_percent()` computes:

```python
cpu_percent = round(quotient * 100 / get_num_cpus(), 1)
```

A Ray node can legitimately be registered with **0 CPUs** — in fact
`num-cpus: "0"` on the head node is the *recommended* KubeRay
configuration (to keep tasks off the head). On any such pod,
`get_num_cpus()` returns `0`, so this line raises `ZeroDivisionError` on
**every** metrics poll cycle.

The exception is caught (so `0.0` is still returned and behavior is
functionally correct), but `logger.exception(...)` writes a **full
traceback to the dashboard-agent logs every cycle**, which is noisy and
misleading.

This is the same class of bug already guarded against elsewhere in the
dashboard — `reporter_agent._get_load_avg()` uses `if
self._cpu_counts[0] > 0: ... else: ...`. This PR applies the same guard
to `cpu_percent()`: return `0.0` when `get_num_cpus() == 0`.

## Related issue number

Fixes ray-project#63729

## Checks

- Added `test_k8s_cpu_percent_zero_num_cpus` to `test_advanced_8.py`.
The discriminating assertion is `logger.exception.assert_not_called()`:
without the fix the `ZeroDivisionError` is caught and *logged* every
cycle, so the test would fail; with the fix no exception is raised or
logged.
- The fix logic was verified in isolation (0-CPU path returns `0.0` with
no logged exception). `ruff check` passes on both changed files.

---

*Disclosure: this change was prepared with the assistance of Claude
Code; I have reviewed it and take responsibility for it.*

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Lawson Darrow <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
elliot-barn pushed a commit that referenced this pull request Jun 25, 2026
…dge cases (#64354)

## Description

The `get_contributors` release-notes script extracts PR numbers from
commit subjects to look up contributor logins. The old `_find_pr_number`
helper grabbed all text between `(#` and the first `)`, which produced
wrong results for several real commit titles:

- A truncated revert like `Revert "... hot path (#6... (#64309)` yielded
`6... (#64309` instead of `64309`.
- A title carrying a fixed-issue reference followed by the merging PR,
e.g. `... cpu_percent (#63729) (#63733)`, yielded the issue number
`63729` instead of the PR `63733`.
- **Cherry-picks** such as `... in MiB (#63932) (#64042)` (original PR +
backport PR) credited only one number, silently dropping the original
author.

This PR replaces it with `_find_pr_numbers`, which:

- Matches only well-formed `(#<digits>)` tokens using a module-level
compiled regex (the helper runs up to thousands of times per
invocation).
- Returns **every** candidate in title order. The commit text alone
cannot tell an issue from a PR or an original from a backport, so `run`
queries all candidates via the GitHub API and credits each one that
resolves as a real PR. A cherry-pick now credits both the original
author and the backporter.
- Treats a `404` as expected (the number is an issue, not a PR) and
collects those numbers, printing them at the end. This way, if GitHub's
behavior ever changes and a real PR starts returning `404`, the dropped
numbers are surfaced rather than silently lost.

## Related issues

N/A

## Additional information

Unit tests cover the parsing edge cases (truncated revert, issue+PR,
cherry-pick, non-digit parentheticals) and the CLI behavior (both
cherry-pick authors credited; an issue `404` does not drop the real PR
author and is reported in the output).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: sai.miduthuri <[email protected]>
Co-authored-by: Claude Opus 4.8 <[email protected]>
limarkdcunha pushed a commit to limarkdcunha/ray that referenced this pull request Jun 30, 2026
…-project#63729) (ray-project#63733)

## Why are these changes needed?

`ray.dashboard.k8s_utils.cpu_percent()` computes:

```python
cpu_percent = round(quotient * 100 / get_num_cpus(), 1)
```

A Ray node can legitimately be registered with **0 CPUs** — in fact
`num-cpus: "0"` on the head node is the *recommended* KubeRay
configuration (to keep tasks off the head). On any such pod,
`get_num_cpus()` returns `0`, so this line raises `ZeroDivisionError` on
**every** metrics poll cycle.

The exception is caught (so `0.0` is still returned and behavior is
functionally correct), but `logger.exception(...)` writes a **full
traceback to the dashboard-agent logs every cycle**, which is noisy and
misleading.

This is the same class of bug already guarded against elsewhere in the
dashboard — `reporter_agent._get_load_avg()` uses `if
self._cpu_counts[0] > 0: ... else: ...`. This PR applies the same guard
to `cpu_percent()`: return `0.0` when `get_num_cpus() == 0`.

## Related issue number

Fixes ray-project#63729

## Checks

- Added `test_k8s_cpu_percent_zero_num_cpus` to `test_advanced_8.py`.
The discriminating assertion is `logger.exception.assert_not_called()`:
without the fix the `ZeroDivisionError` is caught and *logged* every
cycle, so the test would fail; with the fix no exception is raised or
logged.
- The fix logic was verified in isolation (0-CPU path returns `0.0` with
no logged exception). `ruff check` passes on both changed files.

---

*Disclosure: this change was prepared with the assistance of Claude
Code; I have reviewed it and take responsibility for it.*

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Lawson Darrow <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
limarkdcunha pushed a commit to limarkdcunha/ray that referenced this pull request Jun 30, 2026
…dge cases (ray-project#64354)

## Description

The `get_contributors` release-notes script extracts PR numbers from
commit subjects to look up contributor logins. The old `_find_pr_number`
helper grabbed all text between `(#` and the first `)`, which produced
wrong results for several real commit titles:

- A truncated revert like `Revert "... hot path (ray-project#6... (ray-project#64309)` yielded
`6... (ray-project#64309` instead of `64309`.
- A title carrying a fixed-issue reference followed by the merging PR,
e.g. `... cpu_percent (ray-project#63729) (ray-project#63733)`, yielded the issue number
`63729` instead of the PR `63733`.
- **Cherry-picks** such as `... in MiB (ray-project#63932) (ray-project#64042)` (original PR +
backport PR) credited only one number, silently dropping the original
author.

This PR replaces it with `_find_pr_numbers`, which:

- Matches only well-formed `(#<digits>)` tokens using a module-level
compiled regex (the helper runs up to thousands of times per
invocation).
- Returns **every** candidate in title order. The commit text alone
cannot tell an issue from a PR or an original from a backport, so `run`
queries all candidates via the GitHub API and credits each one that
resolves as a real PR. A cherry-pick now credits both the original
author and the backporter.
- Treats a `404` as expected (the number is an issue, not a PR) and
collects those numbers, printing them at the end. This way, if GitHub's
behavior ever changes and a real PR starts returning `404`, the dropped
numbers are surfaced rather than silently lost.

## Related issues

N/A

## Additional information

Unit tests cover the parsing edge cases (truncated revert, issue+PR,
cherry-pick, non-digit parentheticals) and the CLI behavior (both
cherry-pick authors credited; an issue `404` does not drop the real PR
author and is reported in the output).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: sai.miduthuri <[email protected]>
Co-authored-by: Claude Opus 4.8 <[email protected]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…-project#63729) (ray-project#63733)

## Why are these changes needed?

`ray.dashboard.k8s_utils.cpu_percent()` computes:

```python
cpu_percent = round(quotient * 100 / get_num_cpus(), 1)
```

A Ray node can legitimately be registered with **0 CPUs** — in fact
`num-cpus: "0"` on the head node is the *recommended* KubeRay
configuration (to keep tasks off the head). On any such pod,
`get_num_cpus()` returns `0`, so this line raises `ZeroDivisionError` on
**every** metrics poll cycle.

The exception is caught (so `0.0` is still returned and behavior is
functionally correct), but `logger.exception(...)` writes a **full
traceback to the dashboard-agent logs every cycle**, which is noisy and
misleading.

This is the same class of bug already guarded against elsewhere in the
dashboard — `reporter_agent._get_load_avg()` uses `if
self._cpu_counts[0] > 0: ... else: ...`. This PR applies the same guard
to `cpu_percent()`: return `0.0` when `get_num_cpus() == 0`.

## Related issue number

Fixes ray-project#63729

## Checks

- Added `test_k8s_cpu_percent_zero_num_cpus` to `test_advanced_8.py`.
The discriminating assertion is `logger.exception.assert_not_called()`:
without the fix the `ZeroDivisionError` is caught and *logged* every
cycle, so the test would fail; with the fix no exception is raised or
logged.
- The fix logic was verified in isolation (0-CPU path returns `0.0` with
no logged exception). `ruff check` passes on both changed files.

---

*Disclosure: this change was prepared with the assistance of Claude
Code; I have reviewed it and take responsibility for it.*

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Lawson Darrow <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: 400Ping <[email protected]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…dge cases (ray-project#64354)

## Description

The `get_contributors` release-notes script extracts PR numbers from
commit subjects to look up contributor logins. The old `_find_pr_number`
helper grabbed all text between `(#` and the first `)`, which produced
wrong results for several real commit titles:

- A truncated revert like `Revert "... hot path (ray-project#6... (ray-project#64309)` yielded
`6... (ray-project#64309` instead of `64309`.
- A title carrying a fixed-issue reference followed by the merging PR,
e.g. `... cpu_percent (ray-project#63729) (ray-project#63733)`, yielded the issue number
`63729` instead of the PR `63733`.
- **Cherry-picks** such as `... in MiB (ray-project#63932) (ray-project#64042)` (original PR +
backport PR) credited only one number, silently dropping the original
author.

This PR replaces it with `_find_pr_numbers`, which:

- Matches only well-formed `(#<digits>)` tokens using a module-level
compiled regex (the helper runs up to thousands of times per
invocation).
- Returns **every** candidate in title order. The commit text alone
cannot tell an issue from a PR or an original from a backport, so `run`
queries all candidates via the GitHub API and credits each one that
resolves as a real PR. A cherry-pick now credits both the original
author and the backporter.
- Treats a `404` as expected (the number is an issue, not a PR) and
collects those numbers, printing them at the end. This way, if GitHub's
behavior ever changes and a real PR starts returning `404`, the dropped
numbers are surfaced rather than silently lost.

## Related issues

N/A

## Additional information

Unit tests cover the parsing edge cases (truncated revert, issue+PR,
cherry-pick, non-digit parentheticals) and the CLI behavior (both
cherry-pick authors credited; an issue `404` does not drop the real PR
author and is reported in the output).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: sai.miduthuri <[email protected]>
Co-authored-by: Claude Opus 4.8 <[email protected]>
Signed-off-by: 400Ping <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community core Issues that should be addressed in Ray Core dashboard Issues specific to the Ray Dashboard go add ONLY when ready to merge, run all tests observability Issues related to the Ray Dashboard, Logging, Metrics, Tracing, and/or Profiling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[dashboard] k8s_utils.cpu_percent() crashes with ZeroDivisionError on nodes registered with num-cpus=0

3 participants