Skip to content

[BUG] After using SpeechToText my application has an unhandled exception on close #1894

Closed as not planned

Description

@dylandhall

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

If I use SpeechToText.Default or register a singleton and use it via DI, I get an unhandled exception in C:\Windows\SYSTEM32\ntdll.dll on closing the application.

This doesn't appear when running a release build but I can see it's still logged to the application event log.

Expected Behavior

No error should occur.

Steps To Reproduce

  1. use the StartListeningAsync() method
  2. close the project, with or without calling the StopListeningAsync() method
  3. observe debugger error and error in application event log

Link to public reproduction project repository

https://github.com/dylandhall/MauiToolkitErrorExample

Environment

- .NET MAUI CommunityToolkit: 9.0.0
- OS: Windows
- .NET MAUI: 8.0.21 and 8.0.40

Anything else?

Error in visual studio:

image

Error text in event log:

Faulting application name: ErrorExample.exe, version: 1.0.0.0, time stamp: 0x661f0000
Faulting module name: ntdll.dll, version: 10.0.22621.3527, time stamp: 0x92b2df34
Exception code: 0xc000000d
Fault offset: 0x0000000000126330
Faulting process id: 0x0x2048
Faulting application start time: 0x0x1DAAC01D2FF0BC0
Faulting application path: C:\code\ErrorExample\bin\Debug\net8.0-windows10.0.19041.0\win10-x64\AppX\ErrorExample.exe
Faulting module path: C:\Windows\SYSTEM32\ntdll.dll
Report Id: 1b94b74d-ef97-4b65-89f6-5e2a482c35a3
Faulting package full name: com.companyname.errorexample_1.0.0.1_x64__9zz4h110yvjzm
Faulting package-relative application ID: App

I've attempted a number of methods of stopping/disposing the speech engine before closing, as well as adding via DI. Same error in all instances.

Activity

  1. JORGEGO commented on Aug 26, 2024

    @JORGEGO

    I think the error is the same, on Android I use SpeechToText.Default.ListenAsync, it recognizes what I say, but then it closes the application with the following information obtained from the app center.
    I would like to add that I have tried the same code on Windows and it is not able to recognize what I say.

    Java.Interop.JniEnvironment.InstanceMethods.CallVoidMethod(JniObjectReference instance, JniMethodInfo method, JniArgumentValue* args)
    Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod(String encodedMember, IJavaPeerable self, JniArgumentValue* parameters)
    Android.Speech.SpeechRecognizer.StopListening()
    CommunityToolkit.Maui.Media.SpeechToTextImplementation.StopRecording()
    CommunityToolkit.Maui.Media.SpeechToTextImplementation.<>c__DisplayClass13_0.b__0()
    System.Threading.CancellationToken.<>c.b__12_0(Object obj)
    System.Threading.CancellationTokenSource.Invoke(Delegate d, Object state, CancellationTokenSource source)
    System.Threading.CancellationTokenSource.CallbackNode.<>c.b__9_0(Object s)
    System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
    System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
    System.Threading.CancellationTokenSource.CallbackNode.ExecuteCallback()
    System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(Boolean throwOnFirstException)
    java.lang.RuntimeException: SpeechRecognizer should be used only from the application's main thread
    android.speech.SpeechRecognizer.checkIsCalledFromMainThread SpeechRecognizer.java:557
    android.speech.SpeechRecognizer.stopListening SpeechRecognizer.java:461

    My code it is very simple:
    bool lIsGranted = await SpeechToText.Default.RequestPermissions(CancellationToken.None);
    if (!lIsGranted) return null;
    CancellationTokenSource lCancellationTokenSource = new();
    lCancellationTokenSource.CancelAfter(5000);
    SpeechToTextResult lSpeechToTextResult = await SpeechToText.Default.ListenAsync(
    new CultureInfo(Settings.Current.CultureInfoValue),
    new Progress(), lCancellationTokenSource.Token);

  2. CameronVetter commented on Aug 28, 2024

    @CameronVetter

    Is there any example of SpeechToText that actually works? There are two examples in the docs here:

    https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/essentials/speech-to-text?tabs=windows

    After learning that the ListenAsync version only works on Android. I've been trying to make use of the StartListeningAsync / StopListeningAsync version. This version always throws an error on Android and Windows as documented here, even using the very basic example from the docs. (This is the easiest way to repro this issue) I was seeing this bug back in the 5.x releases so I was pretty disappointed when I came back to this in 9.x and still see the same issue...

    Has anyone found any workarounds and/or have a fork that has a potential fix for this?

  3. CameronVetter commented on Aug 28, 2024

    @CameronVetter

    This issue, which we are commenting on, is documenting that StartListeningAsync results in an exception being thrown on Android and Windows (I haven't tested IOS yet) when the program closes, so that path is currently broken.

    This issue: #1515 documents that ListenAsync only works on Android, on ios and Windows it runs forever and you have to pass it a cancellation token. If you pass it a cancellation token the result becomes a non success result. I understand this issue was closed, but that doesn't mean it isn't in a broken state.

    So from my perspective as a consumer of SpeechToText, both examples in the documentation don't work, since neither are in a state that could be used in a production app. If they work on all platforms, could you share an example of either that works?

  4. bijington commented on Aug 28, 2024

    @bijington
    Contributor

    @CameronVetter do the samples within our sample application work for you?

  5. CameronVetter commented on Aug 28, 2024

    @CameronVetter

    I just cloned the repo locally and ran the sample application on Windows. The problems are the same there.

    After using StartListeningAsync when I close the application I get this error:

    image

    When using the ListenAsync button, it just keeps listening forever, if I click the cancel token button recognitionResult.IsSuccessful == false, and we end up down the failure path.

  6. bijington commented on Aug 28, 2024

    @bijington
    Contributor

    I am not by my Windows Machine right now to look into this but I will try and remember to take a look tomorrow.

    Some things I would try to look at by having a quick glance over the code:

    Stick a breakpoint in the catch statement (line 108) inside the StopRecording method in the SpeechToTextImplementation.windows.cs class and then try closing the application. Try to see if anything is silently failing in our codebase when tidying things up.

    On the ListenAsync method. I believe it is by design to listen forever or at least a lengthy period of time but I suspect you are not seeing anything being recognised? If so I would look over this post to see if there could be a reason why nothing is being detected: https://learn.microsoft.com/en-us/windows/apps/design/input/manage-issues-with-audio-input

  7. bijington commented on Sep 4, 2024

    @bijington
    Contributor

    Needless to say I didn't manage to get around to testing this 'tomorrow' I will try again over the course of this week/weekend

  8. VladislavAntonyuk commented on Mar 7, 2025

    @VladislavAntonyuk
    Contributor

    A ResultGenerated event can occur after a call to CancelAsync.
    Because of multithreading, a ResultGenerated event might still remain on the stack when CancelAsync is called. If so, the ResultGenerated event still fires.
    If you set any private fields when canceling the recognition session, always confirm their values in the ResultGenerated handler. For example, don't assume a field is initialized in your handler if you set them to null when you cancel the session.

    I don't see the issue in our code or a way how we use SpeechRecognizer. Most likely we'll close the issue

  9. EreborKing commented on Aug 11, 2025

    @EreborKing

    Even with the cancellationtoken the ListenAsync doesn't stop in iOS, am i missing something?

    _cancellationTokenSource = new();
    _cancellationTokenSource.CancelAfter(5000);
    await SpeechToText.StartListenAsync(new SpeechToTextOptions
    {
    Culture = CultureInfo.CurrentCulture,
    ShouldReportPartialResults = true
    }, _cancellationTokenSource.Token);

  10. VladislavAntonyuk commented on Aug 11, 2025

    @VladislavAntonyuk
    Contributor

    Even with the cancellationtoken the ListenAsync doesn't stop in iOS, am i missing something?

    _cancellationTokenSource = new();
    _cancellationTokenSource.CancelAfter(5000);
    await SpeechToText.StartListenAsync(new SpeechToTextOptions
    {
    Culture = CultureInfo.CurrentCulture,
    ShouldReportPartialResults = true
    }, _cancellationTokenSource.Token);

    Yes, this method just start listening. You need to call StopListening.

  11. EreborKing commented on Aug 11, 2025

    @EreborKing

    Even with the cancellationtoken the ListenAsync doesn't stop in iOS, am i missing something?
    _cancellationTokenSource = new();
    _cancellationTokenSource.CancelAfter(5000);
    await SpeechToText.StartListenAsync(new SpeechToTextOptions
    {
    Culture = CultureInfo.CurrentCulture,
    ShouldReportPartialResults = true
    }, _cancellationTokenSource.Token);

    Yes, this method just start listening. You need to call StopListening.

    But if i call the StopListening, the event RecognitionResultCompleted won't trigger.

  12. locked and limited conversation to collaborators on Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/essentialsIssue/Discussion/PR that has to do with EssentialsbugSomething isn't workingunverified

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions