Open
Conversation
- 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
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.
Fix timer race conditions, double elapsed deduction, and pauseFor behaviour
Summary
This PR fixes 6 bugs across
_setupListenAndPause,_notifyResults,_stop,_shutdownListener, andinitialize. 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
elsebranch (bothpauseForandlistenFornon-null),listenForhad already had_elapsedListenMillissubtracted in the adjustment block above. TheminMilliscalculation 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 to0to prevent a negativeDurationbeing passed toTimer._setupListenAndPause— orphaned timer on re-entryIf
_setupListenAndPausewas called while_listenTimerwas still active (e.g. via_stopOnPauseOrListen→_setupListenAndPause, orchangePauseFor), 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._notifyResults—pauseFortimer reset when_partialResultsis false (critical)The
_lastSpeechEventAttimestamp (which drives thepauseForcountdown) 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, makingpauseForeffectively non-functional in that mode. The early-return has been moved above the timestamp update._stop—_notifyFinalTimernot cancelled before reassignmentIf
_stopwas called while a_notifyFinalTimerwas 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— emptyifblock silently ignored invalidfinalTimeoutThe block
if (finalTimeout <= _minFinalTimeout) {}was a no-op, so passing a sub-thresholdfinalTimeoutwould set_finalTimeoutto the invalid value and silently disable the final timeout logic. The block now clamps_finalTimeoutto_minFinalTimeout._shutdownListener— duplicate_listenTimer = nullA copy-paste leftover left
_listenTimer = nullwritten twice in sequence. The duplicate line has been removed.