Skip to content

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

@priolap

Description

@priolap

Description:
In hardware.py, the method total_power() appends power readings from CPUs and calculates an average using:

cpu_power = sum(power_history_in_W) / len(power_history_in_W)

However, under certain runtime conditions, no power samples may be collected, resulting in an empty power_history_in_W. This causes a ZeroDivisionError when attempting to divide by zero.

Steps to Reproduce:
Run the system under conditions where no CPU power samples are collected (e.g., sensor failure, initial sampling delay, which is frequent).

Call total_power().

Expected Behavior:
The method should handle the empty sample case gracefully and return a default value (e.g., 0.0 W), optionally logging a warning.

Proposed Fix:
Add a check to handle empty sample history:

if not power_history_in_W: logger.warning("No power samples collected; returning 0.0 W") cpu_power = sum(power_history_in_W) / len(power_history_in_W) if power_history_in_W else 0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions