Skip to content

fix: handle empty power samples in total_power() to prevent ZeroDivisionError#1129

Open
wangwangbobo wants to merge 5 commits intomlco2:masterfrom
wangwangbobo:fix-total-power-zero-division
Open

fix: handle empty power samples in total_power() to prevent ZeroDivisionError#1129
wangwangbobo wants to merge 5 commits intomlco2:masterfrom
wangwangbobo:fix-total-power-zero-division

Conversation

@wangwangbobo
Copy link

Description

This PR fixes a ZeroDivisionError that can occur in the total_power() method when no power samples are collected.

Problem

The original code checked if self._power_history was empty after appending to it, which meant the check would never catch an empty list. Additionally, the list comprehension that extracts power values could result in an empty list, causing a division by zero error.

Solution

  • Move the empty check to after extracting power values from history
  • Check if power_history_in_W is empty before dividing
  • Return 0.0 W as a safe default value with a warning log

Changes

  • codecarbon/external/hardware.py: Fixed the empty check logic in CPU.total_power()

Testing

The fix handles the edge case where no power samples are collected, preventing crashes during runtime.

Fixes #832

Closes mlco2#1125

Replaced all mlco2.github.io/codecarbon links with docs.codecarbon.io:
- README.md (6 links)
- CONTRIBUTING.md (2 links)
- .github/copilot-instructions.md (2 links)
- Replace mlco2.github.io/codecarbon with docs.codecarbon.io in:
  - webapp/src/app/(dashboard)/home/page.tsx (2 links)
  - codecarbon/core/powermetrics.py (1 link)
  - codecarbon/cli/main.py (1 link)
  - codecarbon/external/ram.py (1 link)
  - examples/notebooks/codecarbon_workshop.ipynb (1 link)

Fixes: mlco2#1125
- Update mkdocs.yml edit_uri to use main branch
- Update CONTRIBUTING.md rebase instructions
- Update emissions_tracker.py docstring URLs
- Add process_tree tracking mode to CPU module for consistency with RAM

Fixes mlco2#1084 and addresses mlco2#976
- Add 'process_tree' as an alias for 'process' tracking mode
- Provides consistent naming with RAM module
- Tracks main process and all children recursively
- Addresses mlco2#976 (Inconsistent behaviour for RAM and CPU Monitoring)

The process_tree mode uses the same implementation as process mode,
which already includes child processes via psutil.children(recursive=True).
This alias improves API clarity and consistency.
…ionError

- Move empty check after extracting power values from history
- Add warning log when no power samples are collected
- Return 0.0 W as safe default value

Fixes mlco2#832
@wangwangbobo wangwangbobo requested a review from a team as a code owner March 23, 2026 02:51
@benoit-cty
Copy link
Contributor

Thanks for you contribution.

Be careful to start new contribution from a fresh environnement of master.

Here you also push the content of #1126

@codecov
Copy link

codecov bot commented Mar 23, 2026

Codecov Report

❌ Patch coverage is 5.55556% with 34 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.09%. Comparing base (46ac6dd) to head (a1a5172).

Files with missing lines Patch % Lines
codecarbon/external/hardware.py 5.55% 34 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1129      +/-   ##
==========================================
- Coverage   80.74%   80.09%   -0.66%     
==========================================
  Files          41       41              
  Lines        3921     3953      +32     
==========================================
  Hits         3166     3166              
- Misses        755      787      +32     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix crash in total_power() when no power samples are collected

2 participants