fix: make tracker.stop() idempotent - #1336
Open
davidberenstein1957 wants to merge 3 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 #1336 +/- ##
==========================================
+ Coverage 91.39% 91.54% +0.15%
==========================================
Files 49 49
Lines 5056 5063 +7
==========================================
+ Hits 4621 4635 +14
+ 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>
davidberenstein1957
marked this pull request as ready for review
August 12, 2026 19:14
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.
Calling
stop()twice wrote a second complete emissions row (two CSV rows, two API POSTs, twohandler.exit()calls) for a single run, because nothing recorded that the tracker had stopped —_start_timestays set and both schedulers beingNonewas only used to emit an advisory warning.What changed
_initialize_runtime_state()now initializes_is_stopped,final_emissionsandfinal_emissions_data.stop()returns early on_is_stopped, returning the cachedfinal_emissions.stop()no longer retriesos.removeon an already-removed lock file.else: logger.warning("Tracker already stopped !")(bound to the_scheduler_monitor_powercheck) is replaced by the real terminal-state guard.Why
__exit__callsstop()unconditionally, so awithblock plus an explicitstop()— the usual way to get the return value — double counts the whole run.Verification
tests/test_emissions_tracker.py::TestCarbonTracker::test_offline_tracker_stop_is_idempotentasserts one CSV row and an identical return value after a double stop. It fails on master (2 rows) and passes with this change. Full file: 32 passed.Closes #1307
🤖 Generated with Claude Code