fix(timer): prevent Start() from spawning multiple tickers causing accelerated countdown#922
Open
Aliexe-code wants to merge 1 commit intocharmbracelet:mainfrom
Open
Conversation
Calling timer.Start() multiple times would cause multiple tick() calls, resulting in the timer counting down faster than the specified interval. Changes: - Update() now checks if the running state actually changes before calling tick(), preventing duplicate tickers - startStop() now returns nil when trying to start a timed-out timer Fixes charmbracelet#867
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.
Problem
Calling
timer.Start()multiple times would cause the timer to tick faster than the specified interval. Each call toStart()sent aStartStopMsgwhich unconditionally calledtick(), spawning a new ticker goroutine. Multiple concurrent tickers resulted in the timer counting down faster than intended.Fixes #867
Solution
Added guards to prevent redundant ticker spawning:
In
Update(): Check if the running state actually changes before callingtick(). Ifm.running == msg.running, return early without spawning a new ticker.In
startStop(): Returnnilwhen trying to start a timer that has already timed out.Changes
timer/timer.go: Added state change guardstimer/timer_test.go: Added comprehensive tests for the fix