Uh oh!
There was an error while loading. Please reload this page.
fix: lock CPU power history swap - #1335
Open
davidberenstein1957 wants to merge 1 commit into
Open
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@## master #1335 +/- ##
==========================================
+ Coverage 91.43% 91.48% +0.04%
==========================================
Files 49 49 Lines 5057 5060 +3 ==========================================
+ Hits 4624 4629 +5 + Misses 433 431 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
davidberenstein1957
marked this pull request as ready for review
August 12, 2026 19:14
davidberenstein1957force-pushed
the
fix/cpu-power-history-race
branch
from
August 19, 2026 09:18
d70b9ae to
ed2b386Comparedavidberenstein1957force-pushed
the
fix/cpu-power-history-race
branch
2 times, most recently
from
August 19, 2026 14:29
00f2a1e to
b813d76CompareThe 1 Hz monitor scheduler thread appends to `CPU._power_history` while the measurement scheduler thread drains it. The read-then-rebind in `total_power()` is not atomic, so any sample appended between the list comprehension and the rebinding was written to the discarded list and lost, biasing the reported `cpu_power`. Take the swap under a lock, held only for the O(1) rebinding so a slow `_get_power_from_cpus()` backend never blocks the monitor thread. Also drop the unreachable empty-history branch: a sample is always appended before the average. The test asserts the drain invariant rather than the drain shape, so it does not pin the implementation. Closes#1315 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
davidberenstein1957force-pushed
the
fix/cpu-power-history-race
branch
from
August 20, 2026 06:14
b813d76 to
f72f432Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes a lost-update race on
CPU._power_history.What changed
CPU._power_historyis appended to by the 1 Hz monitor scheduler thread (CPU.monitor_power) and drained by themeasure_power_secsscheduler thread (CPU.total_power) — two distinctPeriodicSchedulertimer threads (codecarbon/emissions_tracker.py:345-352). The read-then-rebind intotal_powerwas not atomic, so any sample appended between the list comprehension andself._power_history = []went into the discarded list.threading.LocktoCPU, held only for the O(1) list swap._get_power_from_cpus()is called outside the lock in both paths, so a slow backend (e.g. theIntelPowerGadgetsubprocess) never blocks the monitor thread.if not power_history_in_W:branch — a sample is unconditionally appended before the average, so the list is never empty.Behaviour is otherwise unchanged, including the extra synchronous sample taken at measurement time.
Why
In
cpu_loadmode the reportedcpu_poweris the mean of the buffered samples. The samples dropped in the swap window are exactly those taken while a slow measurement is in flight, so the resulting error is small but systematic and silent.How it was verified
New test
tests/test_cpu_load.py::TestCPULoad::test_cpu_total_power_keeps_samples_added_while_drainingmakes the race deterministic (alistsubclass that firesmonitor_power()at iteration exhaustion, i.e. inside the lost-update window) and asserts the injected sample survives. It fails on master and passes with this change.uv run pytest tests/test_cpu_load.py -q→ 9 passed.Note:
uv run task formatreformats a large number of unrelated files on the current tree, so only the two touched files are included here.Closes#1315
🤖 Generated with Claude Code