Skip to content

Fix timer issues#655

Open
danyabdo94 wants to merge 1 commit intocsdcorp:mainfrom
danyabdo94:fix/timer-issues
Open

Fix timer issues#655
danyabdo94 wants to merge 1 commit intocsdcorp:mainfrom
danyabdo94:fix/timer-issues

Conversation

@danyabdo94
Copy link
Copy Markdown

@danyabdo94 danyabdo94 commented Feb 27, 2026

Fix timer race conditions, double elapsed deduction, and pauseFor behaviour

Summary

This PR fixes 6 bugs across _setupListenAndPause, _notifyResults, _stop, _shutdownListener, and initialize. The issues range from a silent misconfiguration to a critical double-subtraction that caused the listen/pause timer to fire far too early.


Changes

_setupListenAndPause — double subtraction of _elapsedListenMillis (critical)
In the else branch (both pauseFor and listenFor non-null), listenFor had already had _elapsedListenMillis subtracted in the adjustment block above. The minMillis calculation then subtracted it a second time, producing a value far smaller than intended — potentially negative. The redundant subtraction has been removed and the result is now clamped to 0 to prevent a negative Duration being passed to Timer.

_setupListenAndPause — orphaned timer on re-entry
If _setupListenAndPause was called while _listenTimer was still active (e.g. via _stopOnPauseOrListen_setupListenAndPause, or changePauseFor), a second timer was created without cancelling the first. Both would eventually fire _stopOnPauseOrListen. The existing timer is now cancelled at the top of the method before any new one is created.

_notifyResultspauseFor timer reset when _partialResults is false (critical)
The _lastSpeechEventAt timestamp (which drives the pauseFor countdown) was being updated before the early-return check for _partialResults. This meant every interim result silently reset the pause timer even when the caller had opted out of partial results, making pauseFor effectively non-functional in that mode. The early-return has been moved above the timestamp update.

_stop_notifyFinalTimer not cancelled before reassignment
If _stop was called while a _notifyFinalTimer was already pending, a second timer was created alongside the first. Both would fire _onFinalTimeout. A defensive _notifyFinalTimer?.cancel() has been added immediately before the reassignment.

initialize — empty if block silently ignored invalid finalTimeout
The block if (finalTimeout <= _minFinalTimeout) {} was a no-op, so passing a sub-threshold finalTimeout would set _finalTimeout to the invalid value and silently disable the final timeout logic. The block now clamps _finalTimeout to _minFinalTimeout.

_shutdownListener — duplicate _listenTimer = null
A copy-paste leftover left _listenTimer = null written twice in sequence. The duplicate line has been removed.

- cancel any existing timer before creating a new one to prevent two timers from firing _stopOnPauseOrListen simultaneously

- was an empty if block, invalid finalTimeout was silently ignored

- cancel before reassigning to prevent two timers firing if _stop() is called while a _notifyFinalTimer is already pending

- listenFor already had _elapsedListenMillis subtracted above, subtracting it again caused double-deduction and a potentially negative duration. Also clamp to 0 to guard against negative Timer.

- removed duplicate _listenTimer = null that was here
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.

1 participant