Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix: gracefully shutdown process

Vanalitedb487053ff6d
diff --git a/src-tauri/src/core/openclaw/commands.rs b/src-tauri/src/core/openclaw/commands.rs
index 6ad625fc7..7bab79a59 100644
--- a/src-tauri/src/core/openclaw/commands.rs
+++ b/src-tauri/src/core/openclaw/commands.rs
@@ -816,8 +816,9 @@ async fn stop_other_sandbox_instance(stop_docker: bool) {
if !cli_stopped {
#[cfg(any(target_os = "macos", target_os = "linux"))]
{
+ // Match full command line — the actual process is `bun /path/to/openclaw gateway`
let _ = Command::new("pkill")
- .args(["-f", "openclaw-gateway"])
+ .args(["-f", "openclaw"])
.output()
.await;
}
@@ -843,6 +844,32 @@ async fn stop_other_sandbox_instance(stop_docker: bool) {
}
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
}
+
+ // Port-based kill as last resort — kill whatever is holding the port
+ if crate::core::openclaw::lifecycle::is_port_in_use(OPENCLAW_PORT).await {
+ log::warn!("Port {} still in use after stop attempts, trying port-based kill", OPENCLAW_PORT);
+ #[cfg(any(target_os = "macos", target_os = "linux"))]
+ {
+ let _ = Command::new("sh")
+ .args(["-c", &format!("lsof -ti :{} | xargs kill -9", OPENCLAW_PORT)])
+ .output()
+ .await;
+ }
+
+ #[cfg(target_os = "windows")]
+ {
+ use std::os::windows::process::CommandExt;
+ // Find and kill PID by port using netstat
+ if let Ok(output) = Command::new("cmd")
+ .args(["/C", &format!("for /f \"tokens=5\" %a in ('netstat -aon ^| findstr :{} ^| findstr LISTENING') do taskkill /F /PID %a", OPENCLAW_PORT)])
+ .creation_flags(0x08000000)
+ .output()
+ .await
+ {
+ let _ = output;
+ }
+ }
+ }
}
}
@@ -4090,19 +4117,38 @@ pub async fn security_get_status() -> Result<SecurityStatus, String> {
/// Set the authentication mode
///
/// Changes how users authenticate to the OpenClaw gateway.
+/// When set to `None`, also removes the gateway auth token from `openclaw.json`.
#[tauri::command]
pub async fn security_set_auth_mode(mode: AuthMode) -> Result<(), String> {
log::info!("Setting auth mode to {:?}", mode);
let mut config = security::load_security_config().await?;
- config.auth_mode = mode;
- security::save_security_config(&config).await
+ config.auth_mode = mode.clone();
+ security::save_security_config(&config).await?;
+
+ // Sync auth mode to the gateway config
+ if mode == AuthMode::None {
+ if let Ok(mut oc_config) = read_openclaw_config() {
+ if let Some(auth) = oc_config.pointer_mut("/gateway/auth") {
+ if let Some(obj) = auth.as_object_mut() {
+ obj.remove("token");
+ }
+ }
+ if let Err(e) = write_openclaw_config(&oc_config) {
+ log::warn!("Failed to clear gateway auth token: {}", e);
+ }
+ }
+ }
+
+ Ok(())
}
/// Generate and store a new access token
///
/// Returns the plaintext token (only shown once). The token is
/// stored as a hash and cannot be retrieved after this.
+/// Also updates the gateway auth token in `openclaw.json` so the
+/// OpenClaw gateway enforces the same token.
#[tauri::command]
pub async fn security_generate_token() -> Result<String, String> {
log::info!("Generating new access token");
@@ -4115,6 +4161,14 @@ pub async fn security_generate_token() -> Result<String, String> {
config.auth_mode = AuthMode::Token;
security::save_security_config(&config).await?;
+ // Sync the token to the gateway config so OpenClaw actually uses it
+ if let Ok(mut oc_config) = read_openclaw_config() {
+ oc_config["gateway"]["auth"]["token"] = serde_json::json!(token);
+ if let Err(e) = write_openclaw_config(&oc_config) {
+ log::warn!("Failed to sync token to gateway config: {}", e);
+ }
+ }
+
Ok(token)
}
diff --git a/src-tauri/src/core/openclaw/sandbox_direct.rs b/src-tauri/src/core/openclaw/sandbox_direct.rs
index 9737364be..3fe7c385f 100644
--- a/src-tauri/src/core/openclaw/sandbox_direct.rs
+++ b/src-tauri/src/core/openclaw/sandbox_direct.rs
@@ -136,6 +136,7 @@ impl Sandbox for DirectProcessSandbox {
if !stopped_via_cli {
#[cfg(any(target_os = "macos", target_os = "linux"))]
{
+ // Match full command line — the actual process is `bun /path/to/openclaw gateway`
let _ = tokio::process::Command::new("pkill")
.args(["-f", "openclaw"])
.output()
@@ -157,6 +158,36 @@ impl Sandbox for DirectProcessSandbox {
}
}
+ // Port-based kill as last resort — kill whatever is holding the port
+ if tokio::net::TcpStream::connect(format!("127.0.0.1:{}", super::OPENCLAW_PORT))
+ .await
+ .is_ok()
+ {
+ log::warn!(
+ "Port {} still in use after stop attempts, trying port-based kill",
+ super::OPENCLAW_PORT
+ );
+ #[cfg(any(target_os = "macos", target_os = "linux"))]
+ {
+ let _ = tokio::process::Command::new("sh")
+ .args([
+ "-c",
+ &format!("lsof -ti :{} | xargs kill -9", super::OPENCLAW_PORT),
+ ])
+ .output()
+ .await;
+ }
+
+ #[cfg(target_os = "windows")]
+ {
+ use std::os::windows::process::CommandExt;
+ let mut cmd = tokio::process::Command::new("cmd");
+ cmd.args(["/C", &format!("for /f \"tokens=5\" %a in ('netstat -aon ^| findstr :{} ^| findstr LISTENING') do taskkill /F /PID %a", super::OPENCLAW_PORT)]);
+ cmd.creation_flags(0x08000000);
+ let _ = cmd.output().await;
+ }
+ }
+
Ok(())
}
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index ece861362..fa83cde80 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -456,6 +456,49 @@ pub fn run() {
}
}
+ // OpenClaw gateway cleanup
+ {
+ use crate::core::openclaw::sandbox::Sandbox;
+ use crate::core::openclaw::OpenClawState;
+ let openclaw_state = app_handle.state::<OpenClawState>();
+ let openclaw_future = async {
+ let sandbox_guard = openclaw_state.sandbox.lock().await;
+ match sandbox_guard.as_ref() {
+ Some(sandbox) => {
+ crate::core::openclaw::lifecycle::stop_openclaw(
+ sandbox.as_ref(),
+ &openclaw_state,
+ )
+ .await
+ }
+ None => {
+ // Fallback: use DirectProcessSandbox to stop any running gateway
+ let direct =
+ crate::core::openclaw::sandbox_direct::DirectProcessSandbox;
+ let mut handle =
+ crate::core::openclaw::sandbox::SandboxHandle::Named(
+ "exit-cleanup".to_string(),
+ );
+ direct.stop(&mut handle).await
+ }
+ }
+ };
+ match tokio::time::timeout(
+ tokio::time::Duration::from_secs(10),
+ openclaw_future,
+ )
+ .await
+ {
+ Ok(Ok(_)) => {
+ log::info!("OpenClaw cleanup completed successfully")
+ }
+ Ok(Err(e)) => log::warn!("OpenClaw cleanup failed: {}", e),
+ Err(_) => {
+ log::warn!("OpenClaw cleanup timed out after 10 seconds")
+ }
+ }
+ }
+
log::info!("App cleanup completed");
});
});