Repository navigation
Conversation
There was a problem hiding this comment.
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.
| if num_cpus == 0: | ||
| cpu_percent = 0.0 | ||
| else: | ||
| cpu_percent = round(quotient * 100 / num_cpus, 1) |
There was a problem hiding this comment.
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.
| 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) |
|
@rueian PTAL |
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]>
a9777cb to
2c7d1ac
Compare
Signed-off-by: Lawson Darrow <[email protected]>
| assert 50 < k8s_utils.cpu_percent() < 60 | ||
|
|
||
|
|
||
| def test_k8s_cpu_percent_zero_num_cpus(): |
There was a problem hiding this comment.
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 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]>
|
Removed the test, thanks for the review! |
|
Thanks for the contribution |
…-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]>
…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]>
…-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]>
…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]>
…-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]>
…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]>
Why are these changes needed?
ray.dashboard.k8s_utils.cpu_percent()computes: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()returns0, so this line raisesZeroDivisionErroron every metrics poll cycle.The exception is caught (so
0.0is still returned and behavior is functionally correct), butlogger.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()usesif self._cpu_counts[0] > 0: ... else: .... This PR applies the same guard tocpu_percent(): return0.0whenget_num_cpus() == 0.Related issue number
Fixes #63729
Checks
test_k8s_cpu_percent_zero_num_cpustotest_advanced_8.py. The discriminating assertion islogger.exception.assert_not_called(): without the fix theZeroDivisionErroris caught and logged every cycle, so the test would fail; with the fix no exception is raised or logged.0.0with no logged exception).ruff checkpasses 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