fix: unload model on cancel when immediate unload enabled (#498)

* fix: unload model on cancel when immediate unload enabled

adds model unload check to cancel_current_operation() to match
the behavior in transcribe(). previously, cancelling with escape
would leave the model loaded even with "unload immediately" enabled.

fixes #479

* also fix model not unloading if there is empty transcription

* refactor to util function

---------

Co-authored-by: CJ Pais <cj@cjpais.com>
This commit is contained in:
Viren Mohindra 2025-12-29 09:29:54 +05:30 committed by GitHub
parent 5c5e428e06
commit e0b72f5f77
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 8 deletions

View file

@ -170,6 +170,19 @@ impl TranscriptionManager {
Ok(())
}
/// Unloads the model immediately if the setting is enabled and the model is loaded
pub fn maybe_unload_immediately(&self, context: &str) {
let settings = get_settings(&self.app_handle);
if settings.model_unload_timeout == ModelUnloadTimeout::Immediately
&& self.is_model_loaded()
{
info!("Immediately unloading model after {}", context);
if let Err(e) = self.unload_model() {
warn!("Failed to immediately unload model: {}", e);
}
}
}
pub fn load_model(&self, model_id: &str) -> Result<()> {
let load_start = std::time::Instant::now();
debug!("Starting to load model: {}", model_id);
@ -316,8 +329,9 @@ impl TranscriptionManager {
debug!("Audio vector length: {}", audio.len());
if audio.len() == 0 {
if audio.is_empty() {
debug!("Empty audio vector");
self.maybe_unload_immediately("empty audio");
return Ok(String::new());
}
@ -418,13 +432,7 @@ impl TranscriptionManager {
info!("Transcription result: {}", final_result);
}
// Check if we should immediately unload the model after transcription
if settings.model_unload_timeout == ModelUnloadTimeout::Immediately {
info!("Immediately unloading model after transcription");
if let Err(e) = self.unload_model() {
error!("Failed to immediately unload model: {}", e);
}
}
self.maybe_unload_immediately("transcription");
Ok(final_result)
}

View file

@ -1,4 +1,5 @@
use crate::managers::audio::AudioRecordingManager;
use crate::managers::transcription::TranscriptionManager;
use crate::shortcut;
use crate::ManagedToggleState;
use log::{info, warn};
@ -36,6 +37,10 @@ pub fn cancel_current_operation(app: &AppHandle) {
change_tray_icon(app, crate::tray::TrayIconState::Idle);
hide_recording_overlay(app);
// Unload model if immediate unload is enabled
let tm = app.state::<Arc<TranscriptionManager>>();
tm.maybe_unload_immediately("cancellation");
info!("Operation cancellation completed - returned to idle state");
}