fix: allow restarting a tracker after stop() - #1337
Closed
davidberenstein1957 wants to merge 1 commit into
Closed
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1337 +/- ##
==========================================
- Coverage 91.43% 91.40% -0.04%
==========================================
Files 49 49
Lines 5057 5060 +3
==========================================
+ Hits 4624 4625 +1
- Misses 433 435 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
start() after stop() used to log "Already started tracking" and silently do nothing, which is misleading: stop() has dropped the schedulers, released the lock and exited the output handlers, so the tracker is not tracking anything. Say so instead. Restarting is not supported and this does not add it. start() is wrapped in @Suppress(Exception), so raising would be swallowed; the error is logged instead. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
davidberenstein1957
force-pushed
the
fix/restart-after-stop
branch
from
August 19, 2026 13:21
1287bf3 to
c8a38b9
Compare
davidberenstein1957
added a commit
that referenced
this pull request
Aug 19, 2026
stop() used the schedulers as its state flag, so a second call re-ran the final measurement, wrote a second row and released the lock twice -- by then the lock may already belong to another tracker. Add `_stopped_at` as the single state flag: `_start_time is None` means never started, `_stopped_at is not None` means stopped. A second stop() returns the memoised emissions; a start() after stop() is refused with an error instead of half-restarting a tracker whose output handlers, lock and schedulers are already gone. Folds in #1337, which inferred the same state from `self._scheduler`. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Collaborator
Author
|
Closing as folded into #1336. Both PRs were about start() and stop() disagreeing on whether the tracker has run, and they had already converged: #1336 introduces _stopped_at as the canonical lifecycle flag, while this PR was inferring the same state from _scheduler is None. Two flags for one fact. The refusal now reads 'if self._stopped_at is not None' on #1336, so it is one flag and three guards in a single review. The honest-error change and its test came across unchanged. |
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