Skip to content

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

Merged
benoit-cty merged 2 commits into
masterfrom
fix/ram-default-pid
Oct 7, 2026
Merged

benoit-cty merged 2 commits into
masterfrom
fix/ram-default-pid

Conversation

@davidberenstein1957

@davidberenstein1957 davidberenstein1957 commented Aug 12, 2026 •

Copy link
Copy Markdown
Collaborator

Description

RAM.__init__ took pid: int = psutil.Process().pid as a default argument. Python evaluates default arguments once at import time, so the value was frozen to whichever process first imported codecarbon.external.ram and that stale value survived fork(). The default is now None, resolved to os.getpid() inside __init__, matching how CPU already does it in external/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 with tracking_mode="process" this meant measuring the parent's RSS plus every sibling worker, or, if the parent had exited, a NoSuchProcess error that total_power swallows 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_init mocks os.getpid and asserts RAM(tracking_mode="process")._pid matches 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.py is green (21 passed).

Screenshots (if appropriate):

N/A

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

AI Usage Disclosure

  • 🟥 AI-vibecoded: You cannot explain the logic. Car analogy : the car drive by itself, you are outside it and just tell it where to go.
  • 🟠 AI-generated: Car analogy : the car drive by itself, you are inside and give instructions.
  • ⭐ AI-assisted. Car analogy : you drive the car, AI help you find your way.
  • ♻️ No AI used. Car analogy : you drive the car.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the docs/how-to/contributing.md document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@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 92.04%. Comparing base (7a47f7a) to head (7cdf451).
⚠️ Report is 3 commits behind head on master.

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.
📢 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
@benoit-cty

Copy link
Copy Markdown
Contributor

🤖 This review comment was written and posted by Claude Opus 5.5 (AI assistant), at the request of @benoit-cty. Findings were checked by reading the code and running tests locally (merged with current master where relevant), but please double-check before acting on them.

Verdict: ✅ Approve with nits

The bug is real. pid: int = psutil.Process().pid runs once, when the module is imported. So a forked child in process mode measures its parent, and reports 0 W silently on NoSuchProcess. Resolving os.getpid() in __init__ when pid is None is correct. The hardware cache rebuilds RAM from specs, so it can't hold on to a stale instance. 21 tests pass.

Nits:

  1. The test forks inside multi-threaded pytest, which gives a DeprecationWarning on Python 3.12+ and can be flaky. Patching os.getpid (e.g. mock.patch("os.getpid", return_value=12345)) and asserting RAM()._pid == 12345 would be simpler and deterministic.
  2. If RAM() raises in the child, the parent currently fails with a confusing int(''). If you keep the fork, write an explicit sentinel or the exit status instead.
  3. The branch is behind master; please update it before merge.

@davidberenstein1957

Copy link
Copy Markdown
Collaborator Author

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.

davidberenstein1957 and others added 2 commits October 7, 2026 20:03
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>
@benoit-cty
benoit-cty force-pushed the fix/ram-default-pid branch from bb22b62 to 7cdf451 Compare October 7, 2026 18:03

@benoit-cty benoit-cty left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

@benoit-cty
benoit-cty enabled auto-merge October 7, 2026 18:03
@benoit-cty
benoit-cty merged commit b438616 into master Oct 7, 2026
13 checks passed
@benoit-cty
benoit-cty deleted the fix/ram-default-pid branch October 7, 2026 18:04
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

2 participants