Skip to content

fix: allow restarting a tracker after stop() - #1337

Closed
davidberenstein1957 wants to merge 1 commit into
masterfrom
fix/restart-after-stop
Closed

fix: allow restarting a tracker after stop()#1337
davidberenstein1957 wants to merge 1 commit into
masterfrom
fix/restart-after-stop

Conversation

@davidberenstein1957

Copy link
Copy Markdown
Collaborator

What

stop() tore down both schedulers but never cleared _start_time, so a later start() returned early with "Already started tracking". The tracker looked started but was never sampled, and the next stop() wrote a row whose duration covered everything since the original start() — including the idle gap — making that row's duration and emissions_rate wrong.

Changes

  • stop() records _paused_at after 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_time by the paused interval rather than re-stamping it. The energy accumulators are deliberately not reset on restart, so shifting keeps duration and the accumulators on the same clock: both cover active time only.

Note: a restarted run stays cumulative, and row 2's duration is now the sum of the active phases rather than wall-clock since the first start().

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 second start() and that the second row's duration is ~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

@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 91.40%. Comparing base (3ec31a0) to head (c8a38b9).

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.
📢 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 13, 2026 05:05
@davidberenstein1957
davidberenstein1957 requested a review from a team as a code owner 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>
@github-actions github-actions Bot added size/S and removed size/L labels Aug 19, 2026
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>
@davidberenstein1957

Copy link
Copy Markdown
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.

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.

Tracker cannot be restarted: start() after stop() is a no-op and the next row's duration is wrong

1 participant