sandrohanea / whisper.net

Whisper.net. Speech to text made simple using Whisper Models
MIT License
534 stars 82 forks source link

AccessViolation when try to cancel ProcessAsync() #23

Closed kaedei closed 1 year ago

kaedei commented 1 year ago

Whisper.Net version: 1.2.2 Environment: win10-x64 .NET version: Framework 4.7.2 Model: Small.bin wav file language: Japanese

Use Whisper.net.Demo sample code, in Program.cs, pass a 1 minute cancellation token to ProcessAsync():

    var cts = new CancellationTokenSource(TimeSpan.FromMinutes(1));
    await foreach (var segment in processor.ProcessAsync(fileStream, cts.Token))
    {
        Console.WriteLine($"New Segment: {segment.Start} ==> {segment.End} : {segment.Text}");
    }

After 1min, the Demo crashed by AccessViolationException: 0x00007FFB245934AE (whisper.dll)处(位于 Whisper.net.Demo.exe 中)引发的异常: 0xC0000005: 读取位置 0x000001D0863A1D90 时发生访问冲突。

kaedei commented 1 year ago

I tried using new Thread() instead of Task.Run() then call processor.Process() in it. After call thread.Abort(), the thread terminated normally without throwing AccessViolationException. But Thread.Abort() seems only works in .NET Framework + Windows, this may not be a perfect solution.

sandrohanea commented 1 year ago

Yes, the problem is that the underlying whisper.cpp library was not supporting cancellation so even if you cancel whisper.net processor, the processing is still running under the hood. Will check if whisper.cpp have any plans of adding cancellation options. and will keep this updated.

sandrohanea commented 1 year ago

I added a commit to fix it and will release it in 1.3.0 version. However, after cancelling, you'll have to call DisposeAsync() instead of Dispose now on the processor as otherwise Dispose method will throw (as the processing is still happening).

DisposeAsync will wait until all resources can be safely released.

Also, a newer encoder will be cancelled now, so the rest of processing will finish faster, but the decoders cannot be cancelled (as whisper.cpp is not supporting that option yet).