Crash when exiting application.
This library is working fine, however if it make at least one SynthisizeAsync call then exit the app, the app crashes. If I don't make the call, it closes fine.
This is the crash result:
(lldb) process attach --pid 30135
Process 30135 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
frame #0: 0x00000001877fb478 libsystem_kernel.dylib`__wait4 + 8
libsystem_kernel.dylib`__wait4:
-> 0x1877fb478 <+8>: b.lo 0x1877fb498 ; <+40>
0x1877fb47c <+12>: pacibsp
0x1877fb480 <+16>: stp x29, x30, [sp, #-0x10]!
0x1877fb484 <+20>: mov x29, sp
Executable binary set to "/Users/steven/Programming/src/WingmanSharp/WingmanApp/bin/Debug/net10.0/WingmanApp".
Architecture set to: arm64-apple-macosx-.
(lldb) btp │ signal SIGSTOP
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
* frame #0: 0x00000001877fb478 libsystem_kernel.dylib`__wait4 + 8
frame #1: 0x0000000171b2bee4 libggml-base-whisper.dylib`ggml_uncaught_exception() + 12
frame #2: 0x00000001877eb75c libc++abi.dylib`std::__terminate(void (*)()) + 16
frame #3: 0x00000001877eb710 libc++abi.dylib`std::terminate() + 108
frame #4: 0x0000000303b1af34 libonnxruntime.dylib`__clang_call_terminate + 12
libc++abi: terminating due to uncaught exception of type std::__1::system_error: mutex lock failed: Invalid argument::~unique_ptr[abi:ue170006]() + 84
frame #6: 0x00000001876ede48 libsystem_c.dylib`__cxa_finalize_ranges + 416
frame #7: 0x00000001876edc48 libsystem_c.dylib`exit + 44
frame #8: 0x00000001874442e4 libdyld.dylib`dyld4::LibSystemHelpers::exit(int) const + 20
frame #9: 0x000000018747beb0 dyld`dyld4::LibSystemHelpersWrapper::exit(int) const + 164
frame #10: 0x000000018747bdd4 dyld`start + 7040
(lldb) quit
To be clear, I have called this several times in testing and it worked fine. But as soon as I exit the app, bang. If I comment out the lines below, no bang.
This is on an M4 Macook Pro using Kokoro.Cpu v0.6.7.
var modelPath = await LoadModelAsync(_ => _outputService.AppendInfo(".")).ConfigureAwait(false); // <- my own loader
using var wavSynth = new KokoroWavSynthesizer(modelPath);
var voice = KokoroVoiceManager.GetVoice(_configuration.Voice.Name);
var bytes = await _wavSynth.SynthesizeAsync(text, voice).ConfigureAwait(false);
Thanks for the detailed stack trace. This appears to be a known ONNX Runtime shutdown bug on macOS rather than an exception thrown by SynthesizeAsync.
KokoroSharp.CPU 0.6.7 uses ONNX Runtime 1.22.0, and Microsoft has an issue with an almost identical stack trace: the process crashes during destruction of the process-global OrtEnv because its logging mutex has already been destroyed:
https://github.com/microsoft/onnxruntime/issues/25038
https://github.com/microsoft/onnxruntime/issues/24579
As a workaround, dispose the synthesizer first and then explicitly dispose the global ONNX environment during your application’s normal shutdown:
using Microsoft.ML.OnnxRuntime;
_wavSynth?.Dispose();
_wavSynth = null;
// Only do this after every ONNX Runtime session in the process
// has been disposed, and do not use ONNX Runtime afterwards.
if (OrtEnv.IsCreated)
{
OrtEnv.Instance().Dispose();
}
Please ensure any running synthesis operation has completed before executing that shutdown code.
I also noticed that the sample creates wavSynth but calls _wavSynth.SynthesizeAsync(...). If that is not just a transcription error, the instance being used is different from the instance managed by using. It should either be:
using var wavSynth = new KokoroWavSynthesizer(modelPath);
var bytes = await wavSynth.SynthesizeAsync(text, voice);
or _wavSynth should be explicitly disposed in the application shutdown handler.
Could you try the explicit OrtEnv cleanup and confirm whether it prevents the crash? Since OrtEnv is process-global, KokoroSharp should not automatically dispose it while other ONNX consumers may still be active.