RAM's default pid is evaluated at import time, so forked workers track the parent process
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.9k
- Forks
- 323
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 12
Description
Problem
RAM.__init__ uses pid: int = psutil.Process().pid as a default argument. Python evaluates default arguments once, when the module is first imported, so the default pid is frozen to the importing process for the life of the interpreter — including across fork(). A child process that constructs RAM() therefore records the parent's pid.
Reproduction
import os, multiprocessing as mp
from codecarbon.external.ram import RAM # imported in the parent
def worker():
print(os.getpid(), RAM(tracking_mode="process")._pid) # these differ
mp.set_start_method("fork")
p = mp.Process(target=worker); p.start(); p.join()
Linux/fork only — with spawn the child re-imports and the default is recomputed.
Root cause
codecarbon/external/ram.py:37—pid: int = psutil.Process().pid(evaluated at import).codecarbon/external/ram.py:58—self._pid = pid.- Neither real construction site passes a pid:
codecarbon/core/resource_tracker.py:37andcodecarbon/core/hardware_cache.py:164, so the stale default is always the one in use.
By comparison CPU resolves it correctly inside __init__ (codecarbon/external/hardware.py:216).
Expected vs actual
Expected: with tracking_mode="process", RAM usage is measured for the process that created the tracker.
Actual, in a forked child:
- If the parent is alive,
process_memory_GB(ram.py:285-296) measures the parent's RSS plus all its descendants — i.e. every sibling worker — so N workers each report roughly the whole pool. - If the parent has exited,
psutil.Process(pid)raisesNoSuchProcess;total_powerswallows it (ram.py:337-339) and RAM power silently reads 0 W for the rest of the run.
Machine tracking mode is unaffected (it never reads _pid).
Fix
Default pid to None and resolve os.getpid() in __init__.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in codecarbon/external/ram.py at RAM.init and compare its pid handling with CPU in codecarbon/external/hardware.py:216. Check the construction paths in codecarbon/core/resource_tracker.py:37 and codecarbon/core/hardware_cache.py:164, then verify process-mode RAM in ram.py:285-296 under fork. Done means a forked child records and measures its own pid while machine mode remains unaffected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 78/100