fix: allow restarting a tracker after stop() - #1337
Open
davidberenstein1957 wants to merge 4 commits into
Open
Conversation
A second stop() ran a full measurement and wrote a duplicate row through every output handler. Guard on an explicit _is_stopped flag and return the cached final_emissions instead. The lock release now happens after the guard, so a repeat stop no longer retries os.remove. Closes #1307 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1337 +/- ##
==========================================
+ Coverage 91.39% 91.56% +0.17%
==========================================
Files 49 49
Lines 5056 5076 +20
==========================================
+ Hits 4621 4648 +27
+ Misses 435 428 -7 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Lock.release() left _has_created_lock True, so a second release() deleted a lock file that may by then belong to another process. Rename the stop guard flag to _stopped_at (a timestamp) so start/stop share one piece of state instead of two. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
start() after a stop() now undoes what stop() tore down, symmetrically: it takes the lock back (and refuses to restart if another instance grabbed it meanwhile) and rebuilds the schedulers. The energy accumulators are kept, so _start_time is shifted by the stopped interval to keep duration on the same clock as the accumulators. The output handlers are deliberately left as they are: exit() runs on every stop() and a later out() re-creates what it cleaned up. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
davidberenstein1957
force-pushed
the
fix/restart-after-stop
branch
from
August 12, 2026 17:44
759ed61 to
3faa49a
Compare
davidberenstein1957
marked this pull request as ready for review
August 13, 2026 05:05
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 free
to 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.
What
stop()tore down both schedulers but never cleared_start_time, so a laterstart()returned early with"Already started tracking". The tracker looked started but was never sampled, and the nextstop()wrote a row whosedurationcovered everything since the originalstart()— including the idle gap — making that row'sdurationandemissions_ratewrong.Changes
stop()records_paused_atafter the final persist, and returns early (with the existing"Tracker already stopped !"warning) if the tracker is already stopped, instead of writing a duplicate row.start()rebuilds the schedulers when they were discarded, and on a restart shifts_start_timeby the paused interval rather than re-stamping it. The energy accumulators are deliberately not reset on restart, so shifting keepsdurationand the accumulators on the same clock: both cover active time only.Note: a restarted run stays cumulative, and row 2's
durationis now the sum of the active phases rather than wall-clock since the firststart().Verification
New test
tests/test_emissions_tracker.py::TestCarbonTracker::test_tracker_can_be_restarted_after_stop— start/2s/stop, 2s pause, start/2s/stop — asserts the scheduler exists again after the secondstart()and that the second row'sdurationis ~4 s, not ~6 s. It fails on master and passes with this change. Full suite: 627 passed, 21 skipped.Closes #1328
🤖 Generated with Claude Code