Skip to content

Commit 610e664

Browse files
danscMaxclaude
andauthored
whisper : catch C++ exceptions in whisper_init_with_params_no_state (#3831)
whisper_model_load() can throw instead of returning false: std::runtime_error from this file (failed ggml context / no compatible buffer type), or vk::SystemError / vk::OutOfDeviceMemoryError from the ggml-vulkan backend during device/buffer allocation. whisper_init_* are extern "C", so a C++ exception unwinding across that boundary aborts non-C++ callers (Rust via whisper-rs, Go via cgo) -- on Windows STATUS_STACK_BUFFER_OVERRUN (0xC0000409) -- even though the function already returns NULL on failure. Wrap whisper_model_load() in try/catch and route any throw into the existing NULL-return path. Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
1 parent e5d4412 commit 610e664

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

src/whisper.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3720,7 +3720,21 @@ struct whisper_context * whisper_init_with_params_no_state(struct whisper_model_
37203720
whisper_context * ctx = new whisper_context;
37213721
ctx->params = params;
37223722

3723-
if (!whisper_model_load(loader, *ctx)) {
3723+
// A C++ exception escaping this extern "C" function aborts non-C++ callers
3724+
// (Rust via whisper-rs, Go via cgo, ...). whisper_model_load can throw
3725+
// (std::runtime_error here; vk::SystemError from the Vulkan backend during
3726+
// device/buffer allocation), so funnel any throw into the existing
3727+
// NULL-return failure path instead of letting it cross the C ABI.
3728+
bool model_loaded = false;
3729+
try {
3730+
model_loaded = whisper_model_load(loader, *ctx);
3731+
} catch (const std::exception & e) {
3732+
WHISPER_LOG_ERROR("%s: exception during model load: %s\n", __func__, e.what());
3733+
} catch (...) {
3734+
WHISPER_LOG_ERROR("%s: unknown exception during model load\n", __func__);
3735+
}
3736+
3737+
if (!model_loaded) {
37243738
loader->close(loader->context);
37253739
WHISPER_LOG_ERROR("%s: failed to load model\n", __func__);
37263740
delete ctx;

0 commit comments

Comments
 (0)