Skip to content

fix: resolve RAM default pid at init time - #1326

Open
davidberenstein1957 wants to merge 1 commit into
masterfrom
fix/ram-default-pid
Open

fix: resolve RAM default pid at init time#1326
davidberenstein1957 wants to merge 1 commit into
masterfrom
fix/ram-default-pid

Conversation

@davidberenstein1957

Copy link
Copy Markdown
Collaborator

What

RAM.__init__ took pid: int = psutil.Process().pid. Python evaluates default arguments once at import, so the value was frozen to whichever process first imported codecarbon.external.ram — and survived fork(). The default is now None, resolved to os.getpid() inside __init__ (matching how CPU already does it in external/hardware.py).

Why

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 with tracking_mode="process" this meant measuring the parent's RSS plus every sibling worker, or — if the parent had exited — NoSuchProcess, which total_power swallows into a silent 0 W for the rest of the run.

Verification

New test tests/test_ram.py::TestRAM::test_default_pid_is_resolved_in_forked_child forks a child, constructs RAM(tracking_mode="process") there and asserts _pid is the child's own pid. It fails on master and passes with this change; uv run pytest tests/test_ram.py is green (21 passed).

Closes #1318

🤖 Generated with Claude Code

@codecov

codecov Bot commented Aug 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.43%. Comparing base (3ec31a0) to head (19fc243).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1326   +/-   ##
=======================================
  Coverage   91.43%   91.43%           
=======================================
  Files          49       49           
  Lines        5057     5058    +1     
=======================================
+ Hits         4624     4625    +1     
  Misses        433      433           

☔ View full report in Codecov by Harness.
📢 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.

@davidberenstein1957
davidberenstein1957 marked this pull request as ready for review August 12, 2026 17:36
@davidberenstein1957
davidberenstein1957 requested a review from a team as a code owner August 12, 2026 17:36
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RAM's default pid is evaluated at import time, so forked workers track the parent process

1 participant