Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions src-tauri/src/grpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,15 +915,24 @@ async fn handle_server_message(
});
}
ServerAction::Unload => {
// The backend does not send this yet; freeing the model lands with
// the scheduler's apply step. Logged so it is visible if it does.
// No model the organization permits fits this node (e.g. it
// disabled the one this node served). The backend has already
// stopped routing here; free the model so the node matches it.
// Previously only logged, which left the model resident and the
// app showing Ready while the backend counted the node as empty.
log_message(
&state.message_log,
app_handle,
"UnloadModel",
"UnloadModel received; not handled by this build",
"UnloadModel received; freeing the model",
)
.await;

let state_clone = state.clone();
let app_handle_clone = app_handle.clone();
tokio::spawn(async move {
handle_unload_model(&state_clone, &app_handle_clone).await;
});
}
ServerAction::Malformed { request_id } => {
// Log empty/malformed message but don't crash the connection
Expand Down Expand Up @@ -1126,6 +1135,29 @@ fn file_name(path: &std::path::Path) -> String {
.to_string()
}

async fn handle_unload_model(state: &AppState, app_handle: &tauri::AppHandle) {
// Serialised with LoadModel, so an unload cannot land mid-switch.
let _switch = state.model_switch.lock().await;
let was_loaded = reload::unload(
&*state.model_manager,
&state.generations,
ReloadTimeouts::default().drain,
)
.await;
set_model_status(state, app_handle, status::ModelStatus::NoModel).await;
log_message(
&state.message_log,
app_handle,
"UnloadModel",
if was_loaded {
"Model unloaded"
} else {
"No model was loaded"
},
)
.await;
}

async fn handle_load_model(
state: &AppState,
app_handle: &tauri::AppHandle,
Expand Down
64 changes: 64 additions & 0 deletions src-tauri/src/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,32 @@ where
result
}

/// Free the resident model: the backend's UnloadModel, sent when no model the
/// organization permits fits this node. Drains first, as a switch does, so a
/// straggling generation cannot hold the model for its whole run. Returns
/// whether a model was resident.
pub async fn unload<M: ModelSlot>(
manager: &Mutex<M>,
generations: &Generations,
drain: Duration,
) -> bool {
let drained = generations.drain(drain).await;
if drained.cancelled > 0 {
println!(
"Drain timed out: cancelled {} generation(s)",
drained.cancelled
);
}
let was_loaded = {
let mut model = manager.lock().await;
let was_loaded = model.loaded_path().is_some();
model.unload();
was_loaded
};
generations.resume().await;
was_loaded
}

/// Load `path`, re-reporting Loading with the elapsed time until it is done.
async fn timed_load<M: ModelSlot, R: FnMut(ReloadEvent)>(
model: &mut M,
Expand Down Expand Up @@ -364,6 +390,44 @@ mod tests {
);
}

#[tokio::test]
async fn unload_frees_the_model_after_in_flight_generations_finish() {
let manager = Arc::new(Mutex::new(FakeModel::serving(OLD)));
let generations = Arc::new(Generations::new());
generations.begin("r1").await.unwrap();

let unloading = tokio::spawn({
let (manager, generations) = (manager.clone(), generations.clone());
async move { unload(&*manager, &generations, Duration::from_secs(5)).await }
});
tokio::time::sleep(Duration::from_millis(200)).await;

assert_eq!(manager.lock().await.resident, Some(PathBuf::from(OLD)));
assert!(
generations.begin("r2").await.is_none(),
"draining stops accepting"
);

generations.finish("r1").await;

assert!(unloading.await.unwrap(), "a model was resident");
assert_eq!(manager.lock().await.resident, None);
assert!(
generations.begin("r3").await.is_some(),
"accepting again once unloaded"
);
}

#[tokio::test]
async fn unload_with_nothing_loaded_is_a_no_op() {
let manager = Mutex::new(FakeModel::default());
let generations = Generations::new();

assert!(!unload(&manager, &generations, Duration::from_secs(5)).await);
assert_eq!(manager.lock().await.resident, None);
assert!(!generations.is_draining().await);
}

#[tokio::test]
async fn the_old_model_is_unloaded_before_the_new_one_loads() {
let manager = Mutex::new(FakeModel::serving(OLD));
Expand Down
Loading