Skip to content

armgit / kokon

Public Imported from GitHub
0

Edit photos by describing the change, on your own GPU. A free, open-source alternative to Nano Banana and ChatGPT image editing: photos stay on your computer, no daily limit. One installer for Windows, Mac and Linux sets up the best open model (Qwen-Image 2.1). MCP server for AI agents.

Closing the desktop app ends the service even with a browser tab open (tabs keep only a server running)

Gabriele Lozuponef92f9eb4f494
diff --git a/crates/qls-service/src/api.rs b/crates/qls-service/src/api.rs
index 34c0e60..1a7d55e 100644
--- a/crates/qls-service/src/api.rs
+++ b/crates/qls-service/src/api.rs
@@ -1211,7 +1211,7 @@ async fn release(State(s): St, Json(b): Json<HeartbeatBody>) -> Json<Value> {
let (removed, remaining, ui_remaining) = {
let mut l = s.leases.lock().unwrap();
let removed = l.remove(&b.client_id);
- (removed, l.len(), l.values().any(|l| l.kind == "ui"))
+ (removed, crate::holding_clients(&s, &l), l.values().any(|l| l.kind == "ui"))
};
if let Some(lease) = removed {
tracing::info!("client {} ({}) released", b.client_id, lease.kind);
diff --git a/crates/qls-service/src/main.rs b/crates/qls-service/src/main.rs
index abc3583..06bc979 100644
--- a/crates/qls-service/src/main.rs
+++ b/crates/qls-service/src/main.rs
@@ -58,6 +58,18 @@ pub struct AppState {
pub const LEASE_TTL: Duration = Duration::from_secs(45);
+/// Whether a client keeps the service running. A browser tab does only on a server (headless
+/// `--persistent` or `--remote`, Docker): when the desktop app started the service, closing the
+/// app ends it, and frees the GPU, even with a phone still on the page.
+pub fn keeps_service(kind: &str, persistent: bool, forced_remote: bool) -> bool {
+ kind != "web" || persistent || forced_remote
+}
+
+/// Clients that keep the service running (see [`keeps_service`]).
+pub fn holding_clients(state: &AppState, leases: &HashMap<String, Lease>) -> usize {
+ leases.values().filter(|l| keeps_service(&l.kind, state.persistent, state.remote.forced)).count()
+}
+
/// Whether a client still holds its lease. Clients that sent their pid (the desktop app, the
/// MCP server) hold it while that process runs: after the computer sleeps their heartbeat is
/// minutes late, and dropping them then cancelled the window's jobs and unloaded the model.
@@ -360,7 +372,7 @@ async fn lifecycle(state: Arc<AppState>) {
alive
});
// A browser tab keeps the model loaded like the desktop window does.
- (leases.len(), leases.values().any(|l| l.kind == "ui"), leases.values().any(|l| l.kind == "ui" || l.kind == "web"), lost)
+ (holding_clients(&state, &leases), leases.values().any(|l| l.kind == "ui"), leases.values().any(|l| l.kind == "ui" || l.kind == "web"), lost)
};
for (id, kind) in &lost {
tracing::info!("client {id} ({kind}) is gone");
@@ -440,7 +452,7 @@ async fn server_watch(state: Arc<AppState>) {
#[cfg(test)]
mod tests {
- use super::{cli_meta, APP_VERSION};
+ use super::{cli_meta, keeps_service, APP_VERSION};
fn args(s: &str) -> Vec<String> {
s.split_whitespace().map(String::from).collect()
@@ -457,4 +469,13 @@ mod tests {
}
assert!(cli_meta(&args("--persistent --port 1234")).is_none());
}
+
+ #[test]
+ fn a_browser_tab_keeps_only_a_server_running() {
+ assert!(keeps_service("ui", false, false));
+ assert!(keeps_service("mcp", false, false));
+ assert!(!keeps_service("web", false, false), "closing the desktop app ends the service");
+ assert!(keeps_service("web", true, false));
+ assert!(keeps_service("web", false, true));
+ }
}