{"record":{"id":"c18b3e3ea30fbd9e","repo":"moeru-ai/airi","slug":"web-speech-api-failed-to-restart-creating-new-ins","errorCode":null,"errorMessage":"Web Speech API failed to restart, creating new instance:","messagePattern":"Web Speech API failed to restart, creating new instance:","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"packages/stage-ui/src/libs/providers/providers/browser-web-speech-api/provider.ts","lineNumber":303,"sourceCode":"    options?.onSpeechEnd?.(fullText)\n  }\n\n  recognition.onend = () => {\n    console.info('Web Speech API recognition ended. Continuous mode:', options?.continuous !== false, 'Aborted:', options?.abortSignal?.aborted)\n\n    // If continuous mode and not aborted, restart recognition\n    if (options?.continuous !== false && !options?.abortSignal?.aborted) {\n      // Use the current recognitionInstance to ensure we're using the correct instance\n      const currentRecognition = recognitionInstance || recognition\n\n      // Small delay before restarting to avoid rapid restart loops\n      setTimeout(() => {\n        try {\n          currentRecognition.start()\n          console.info('Web Speech API recognition restarted (continuous mode)')\n        }\n        catch (err) {\n          console.warn('Web Speech API failed to restart, creating new instance:', err)\n          // If restart fails, create a new instance\n          try {\n            createAndStartNewRecognitionInstance(recognition)\n            console.info('Web Speech API created new instance and started')\n          }\n          catch (newErr) {\n            console.error('Web Speech API failed to create new instance:', newErr)\n            const error = new Error(`Failed to restart recognition: ${errorMessageFromValue(newErr)}`)\n            fullStreamCtrl?.error(error)\n            textStreamCtrl?.error(error)\n            deferredText.reject(error)\n            deferredText.isRejected = true\n          }\n        }\n      }, 100)\n    }\n    else {\n      // Don't try to enqueue/close if the stream has already been aborted/errored","sourceCodeStart":285,"sourceCodeEnd":321,"githubUrl":"https://github.com/moeru-ai/airi/blob/677329427f32468c74b17f3ec47eeca4e05bec65/packages/stage-ui/src/libs/providers/providers/browser-web-speech-api/provider.ts#L285-L321","documentation":"In continuous mode the provider restarts recognition after onend by calling currentRecognition.start() inside a setTimeout. start() throws synchronously (typically InvalidStateError: 'recognition has already started') when the old instance is still active or in a bad lifecycle state; this warning logs that throw and falls back to createAndStartNewRecognitionInstance().","triggerScenarios":"onend fires while the recognition object is not fully stopped, or a previous restart already succeeded, so the delayed start() hits an already-started instance — common when Chrome fires onend/onstart in quick succession in continuous mode.","commonSituations":"Long-running continuous dictation where the engine auto-restarts; Chrome version changes altering onend timing; rapid toggle of listening on/off; two restart paths (timer + user action) racing.","solutions":["Rely on the built-in fallback: the provider already creates a fresh instance when restart throws; verify the following 'created new instance and started' log appears.","Track a stopped/started flag around recognition lifecycle to avoid calling start() on an active instance.","Debounce rapid start/stop toggles in the UI to avoid restart races.","If creating the new instance also fails, see the follow-up 'failed to create new instance' error path — that indicates a deeper permission/service problem."],"exampleFix":"// before\nsetTimeout(() => {\n  try {\n    currentRecognition.start()\n  }\n  catch (err) {\n    console.warn('Web Speech API failed to restart, creating new instance:', err)\n    createAndStartNewRecognitionInstance(recognition)\n  }\n})\n\n// after — stop the old instance first so start() cannot hit InvalidStateError\nsetTimeout(() => {\n  try {\n    currentRecognition.abort()\n    currentRecognition.start()\n  }\n  catch (err) {\n    console.warn('Web Speech API failed to restart, creating new instance:', err)\n    createAndStartNewRecognitionInstance(recognition)\n  }\n})","handlingStrategy":"fallback","validationCode":"function isRecognitionStopped(rec: SpeechRecognition): boolean {\n  // no readyState in the spec; track via flags set in onend/onstart\n  return !recognitionActive\n}","typeGuard":null,"tryCatchPattern":"try {\n  currentRecognition.start()\n}\ncatch {\n  // fall back to a fresh instance (provider already does this)\n  createAndStartNewRecognitionInstance(recognition)\n}","preventionTips":["Track active/stopped state around onstart/onend instead of blind restarts.","Abort the previous instance before restarting in continuous mode.","Debounce user-driven start/stop toggles to avoid restart races."],"tags":["web-speech-api","invalid-state-error","continuous-mode","restart"],"backgroundTag":"speech-recognition-restart-failure","analyzedSha":"677329427f32468c74b17f3ec47eeca4e05bec65","analyzedAt":"2026-08-18T17:29:58.153Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}