IndexOutOfRangeException in KokoroWavSynthesizer.Synthesize on certain inputs
Description
KokoroSharp v0.6.6 (KokoroSharp.CPU) throws an IndexOutOfRangeException originating from inside KokoroWavSynthesizer.Synthesize (or SynthesizeAsync) on certain text inputs and engine state transitions. The exception occurs on an internal thread within the synthesizer, not in caller code.
Reproduction
The crash is intermittent and appears to be related to:
- Certain text inputs (possibly specific phoneme sequences)
- Engine state transitions (e.g., calling Synthesize while the engine is being disposed or switched)
We've observed it most reliably when switching TTS engines mid-speech in our application (Herald Pro), but it has also occurred during normal synthesis of certain text lines.
Stack Trace
The exception is an IndexOutOfRangeException thrown from within KokoroWavSynthesizer's internal synthesis pipeline. Unfortunately the internal stack frames are not visible since KokoroSharp is consumed as a NuGet package, but the exception type and origin are consistent.
Environment
- KokoroSharp.CPU version: 0.6.6
- Runtime: .NET 8.0 (net8.0-windows10.0.17763.0)
- OS: Windows 10/11 x64
- ONNX Runtime: 1.22.0 (via KokoroSharp dependency)
- Model: kokoro.onnx (float16, CPU provider)
Workaround
We wrap the Synthesize call in a try/catch for IndexOutOfRangeException and skip the affected line:
try
{
wavBytes = _synth.Synthesize(text, _voice, config);
}
catch (IndexOutOfRangeException ex)
{
// KokoroSharp v0.6.x has an internal array bounds bug
Log.Error(ex, "KokoroSharp synthesis array bounds error -- skipping line");
return null;
}
This allows our app to continue functioning (the affected line is skipped), but the root cause appears to be an array bounds issue inside the synthesizer.
Additional Context
This bug has been observed in production by Microsoft Store users (Herald Pro, Store ID: 9PBB7S63P441). Our global exception handlers catch it when it escapes the synthesis thread.
Thank you for the excellent library -- Kokoro TTS quality is outstanding. Just wanted to flag this edge case.
Hi, thanks for reporting this!
I'll keep an eye out for this exception, but if you have a specific text that reproduces this, please let me know!
Finally have a deterministic reproducer for this. It is not a race - it is an empty token segment.
Repro: synthesize any text that tokenizes to zero tokens. Common single-line examples that do this with the default tokenizer:
- | > : ; , [] "" ... .
(bullet characters, table pipes, blockquote markers, bare punctuation - anything with no phonemizable content. In a document reader these show up constantly, so for us this was killing the host process several times a day in the field.)
Faulting path in 0.6.6 (byte-identical through 0.8.0 as far as I can tell):
- Tokenizer.Tokenize returns a zero-length token array for those inputs.
- KokoroModel.Infer has an early return for tokens.Length == 0, so the empty step survives to the callback instead of failing earlier.
- KokoroWavSynthesizer.SynthesizeAsync's OnStepComplete does step.Tokens[^1] - on an empty array that is index -1 and throws IndexOutOfRangeException.
- KokoroJob.Progress invokes the callback with no try/catch, on the engine's own background thread, so the exception is unhandled and terminates the entire host process.
Suggested fix (either or both):
- Guard the callback: if (step.Tokens.Length == 0) return; before the Tokens[^1] read - or filter zero-length segments when the job is created.
- Wrap the OnStepComplete invocation in KokoroJob.Progress in try/catch so a consumer callback can never take down the host process on the engine thread.
Workaround for anyone else hitting this, no fork needed - filter empty segments in the pipeline config:
config.SegmentationFunc = toks =>
SegmentationSystem.SplitToSegments(toks, new DefaultSegmentationConfig())
.Where(s => s.Length > 0).ToList();
With that in place our killing inputs went from 10/12 to 0/12 and normal text is unaffected. Happy to open a PR for the callback guard if useful.
Thanks for the repro and details. This should be fixed as of v0.8.1!