Skip to content

fix(timer): prevent Start() from spawning multiple tickers causing accelerated countdown#922

Open
Aliexe-code wants to merge 1 commit intocharmbracelet:mainfrom
Aliexe-code:fix/timer-start-double-call
Open

fix(timer): prevent Start() from spawning multiple tickers causing accelerated countdown#922
Aliexe-code wants to merge 1 commit intocharmbracelet:mainfrom
Aliexe-code:fix/timer-start-double-call

Conversation

@Aliexe-code
Copy link
Copy Markdown

Problem

Calling timer.Start() multiple times would cause the timer to tick faster than the specified interval. Each call to Start() sent a StartStopMsg which unconditionally called tick(), 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:

  1. In Update(): Check if the running state actually changes before calling tick(). If m.running == msg.running, return early without spawning a new ticker.

  2. In startStop(): Return nil when trying to start a timer that has already timed out.

Changes

  • timer/timer.go: Added state change guards
  • timer/timer_test.go: Added comprehensive tests for the fix

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

timer.Start calls timer.tick() too frequently

1 participant