From e8baf31d243ecd3e0c9a3fa1d9d5716040202af5 Mon Sep 17 00:00:00 2001 From: 4di-hello <4di-hello@users.noreply.github.com> Date: Mon, 30 Nov 2026 19:59:05 +0100 Subject: [PATCH] Threads limited to the cores, audio buffer bounded, isModelReady with known engines only Rest of the report on top of the caller check in 1.0.5: - cpuThreads from the request is limited to the number of cores - At most 10 minutes of audio (the keyboard's longest fixed setting) are buffered per session; the buffer keeps boxed samples (about 20 bytes each), so far more would run out of memory - isModelReady accepts only known engine names, like getModelState and deleteModel Co-Authored-By: Claude Opus 5.5 --- .../leantype/voice/offline/engine/WhisperEngine.kt | 12 ++++++++++-- .../leantype/voice/offline/model/ModelManager.kt | 1 + 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/leanbitlab/leantype/voice/offline/engine/WhisperEngine.kt b/app/src/main/java/com/leanbitlab/leantype/voice/offline/engine/WhisperEngine.kt index 614b3bb..aee029e 100644 --- a/app/src/main/java/com/leanbitlab/leantype/voice/offline/engine/WhisperEngine.kt +++ b/app/src/main/java/com/leanbitlab/leantype/voice/offline/engine/WhisperEngine.kt @@ -115,7 +115,8 @@ class WhisperEngine { isCancelled.set(false) val language = normalizeLanguageTag(config?.languageTag) val numThreads = if (config != null && config.cpuThreads > 0) { - config.cpuThreads + // not more threads than cores + config.cpuThreads.coerceAtMost(maxOf(1, Runtime.getRuntime().availableProcessors())) } else { minOf(4, maxOf(1, Runtime.getRuntime().availableProcessors())) } @@ -147,7 +148,9 @@ class WhisperEngine { .asShortBuffer() .get(shortBuffer, 0, bytesRead / 2) - for (i in 0 until bytesRead / 2) { + // bounded, so an endless stream can't use up the memory + val room = MAX_BUFFERED_SAMPLES - utteranceBuffer.size + for (i in 0 until minOf(bytesRead / 2, room)) { utteranceBuffer.add(shortBuffer[i]) } } @@ -316,5 +319,10 @@ class WhisperEngine { private const val SAMPLE_RATE = 16000 private const val FRAME_SIZE_BYTES = 960 // 30ms @ 16kHz 16-bit mono private const val SILENCE_RMS = 0.001f // Sensitive peak threshold to reliably detect short words + /** + * 10 minutes of audio, the keyboard's longest fixed setting; more is not buffered. The buffer holds boxed + * samples (about 20 bytes each), so this is already about 200 MB + */ + private const val MAX_BUFFERED_SAMPLES = SAMPLE_RATE * 60 * 10 } } diff --git a/app/src/main/java/com/leanbitlab/leantype/voice/offline/model/ModelManager.kt b/app/src/main/java/com/leanbitlab/leantype/voice/offline/model/ModelManager.kt index af5544f..c0615a3 100644 --- a/app/src/main/java/com/leanbitlab/leantype/voice/offline/model/ModelManager.kt +++ b/app/src/main/java/com/leanbitlab/leantype/voice/offline/model/ModelManager.kt @@ -41,6 +41,7 @@ class ModelManager( } fun isModelReady(engineType: String): Boolean { + if (!isKnownEngine(engineType)) return false val targetFile = File(modelsDir, engineType) if (!targetFile.exists()) return false