fix: stop PeriodicScheduler from re-arming itself after stop() - #1324
Closed
davidberenstein1957 wants to merge 3 commits into
Closed
fix: stop PeriodicScheduler from re-arming itself after stop()#1324davidberenstein1957 wants to merge 3 commits into
davidberenstein1957 wants to merge 3 commits into
Conversation
_run() rescheduled unconditionally via start(from_run=True), so a stop() landing between the timer firing and the reschedule was silently undone and the scheduler became unstoppable. The payload also ran to completion after stop() returned, letting a measurement mutate tracker state whose output handlers were already exited. Serialize both paths over _stopped under the lock, re-check the flag in _run() before invoking the payload, and read it under the lock in stop(). Closes #1308 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 #1324 +/- ##
==========================================
+ Coverage 91.43% 91.44% +0.01%
==========================================
Files 49 49
Lines 5057 5063 +6
==========================================
+ Hits 4624 4630 +6
Misses 433 433 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
davidberenstein1957
marked this pull request as ready for review
August 12, 2026 17:35
This was referenced Aug 16, 2026
Collaborator
Author
|
Closing in favour of #1339, which fixes this same re-arm bug plus the re-entrancy problem in the same file. Two open PRs rewriting scheduler.py is review burden for no extra benefit, so it's better to have one. |
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 changed
PeriodicScheduler.start()now checks_stoppedon the reschedule path too (inside the lock),_run()re-checks the flag before invoking the payload, andstop()reads the flag under the lock.Why
_run()calledstart(from_run=True), which deliberately bypassed the_stoppedcheck, so a fired timer always armed a successor. Astop()landing between the timer firing and_run()reachingstart()cancelled an already-fired (uncancellable) timer and was then silently undone — the scheduler kept firing forever. The payload also ran to completion afterstop()returned, so_measure_power_and_energycould mutate tracker state and calllive_outafterEmissionsTracker.stop()had written the final row and exited the handlers.The
elif not self._stopped: returnbranch preserves today's "a secondstart()on a running scheduler is a no-op" semantics. The payload deliberately stays outside the lock, sostop()never blocks for a whole measurement (and must not be called from the payload itself).Verification
New
tests/test_scheduler.py.test_stop_during_timer_firing_does_not_rearmfails on master (scheduler keeps firing,_stoppedflips back toFalse) and passes here; the other three cover stop-mid-payload, doublestart()arming one timer, andstop()beforestart()/ twice.tests/test_emissions_tracker.pypasses unchanged.Closes #1308
🤖 Generated with Claude Code