Repository navigation
fix: resolve RAM default pid at init time - #1326
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1326 +/- ##
=======================================
Coverage 92.04% 92.04%
=======================================
Files 49 49
Lines 5153 5154 +1
=======================================
+ Hits 4743 4744 +1
Misses 410 410 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
eb4eada to
19fc243
Compare
Verdict: ✅ Approve with nitsThe bug is real. Nits:
|
|
Made the changes in bb22b62: swapped the fork test for a deterministic os.getpid mock (which also removes the int('') issue), and merged master in. |
The default `pid=psutil.Process().pid` was evaluated at import, so a forked child constructing `RAM()` recorded the parent's pid. In process tracking mode that meant measuring the parent and all its descendants, or reporting 0 W once the parent exited. Resolve the pid in `__init__` instead, so each instance measures the process that created it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
bb22b62 to
7cdf451
Compare
Description
RAM.__init__tookpid: int = psutil.Process().pidas a default argument. Python evaluates default arguments once at import time, so the value was frozen to whichever process first importedcodecarbon.external.ramand that stale value survivedfork(). The default is nowNone, resolved toos.getpid()inside__init__, matching howCPUalready does it inexternal/hardware.py.Related Issue
Fixes #1318
Motivation and Context
Neither construction site passes a pid (
core/resource_tracker.py:37,core/hardware_cache.py:164), so the stale default was always in use. In a forked child withtracking_mode="process"this meant measuring the parent's RSS plus every sibling worker, or, if the parent had exited, aNoSuchProcesserror thattotal_powerswallows into a silent 0 W for the rest of the run.How Has This Been Tested?
New test
tests/test_ram.py::TestRAM::test_default_pid_is_resolved_at_initmocksos.getpidand assertsRAM(tracking_mode="process")._pidmatches the mocked value, i.e. that the pid is resolved at construction time rather than frozen at import time.uv run pytest tests/test_ram.pyis green (21 passed).Screenshots (if appropriate):
N/A
Types of changes
AI Usage Disclosure
Checklist: