Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

fix(service): resolve installation and decompression failures caused by file occupation in Windows

hairyfb919b8816a0c
diff --git a/src-tauri/src/service/download/core.rs b/src-tauri/src/service/download/core.rs
index ad581331..7d03084c 100644
--- a/src-tauri/src/service/download/core.rs
+++ b/src-tauri/src/service/download/core.rs
@@ -115,10 +115,37 @@ pub fn ensure_extract<'a, R: Runtime>(
return Ok(());
}
- // 清理并准备目标目录
+ // 清理并准备目标目录(Windows 上文件可能被短暂占用,重试等待释放)
if dest.exists() {
log::debug!("Destination directory exists, cleaning");
- fs::remove_dir_all(&dest).ok();
+ let mut cleaned = false;
+ for attempt in 1..=5 {
+ match fs::remove_dir_all(&dest) {
+ Ok(()) => {
+ cleaned = true;
+ break;
+ }
+ Err(e) => {
+ if attempt < 5 {
+ log::warn!(
+ "Failed to clean {:?} (attempt {}/5), file may be locked: {}",
+ dest,
+ attempt,
+ e
+ );
+ std::thread::sleep(std::time::Duration::from_millis(300));
+ } else {
+ log::error!("Failed to clean {:?}: {}", dest, e);
+ }
+ }
+ }
+ }
+ if !cleaned {
+ log::error!(
+ "Could not fully clean destination {:?}, some files may still be locked",
+ dest
+ );
+ }
}
fs::create_dir_all(&dest).map_err(|e| {
log::error!("Failed to create destination directory: {}", e);
diff --git a/src-tauri/src/service/download/extractor.rs b/src-tauri/src/service/download/extractor.rs
index ef4f78ed..f83651b3 100644
--- a/src-tauri/src/service/download/extractor.rs
+++ b/src-tauri/src/service/download/extractor.rs
@@ -1,11 +1,41 @@
use std::fs;
use std::io::copy;
use std::io::Cursor;
-use std::path::PathBuf;
+use std::path::{Path, PathBuf};
+use std::time::Duration;
use crate::service::download::ProgressTracker;
use tauri::Runtime;
+/// 创建文件,Windows 上文件被其他进程短暂占用(杀软扫描、残留进程)时自动重试
+///
+/// os error 32 = ERROR_SHARING_VIOLATION(文件被占用),os error 5 = 访问被拒绝,
+/// 这两类错误通常是瞬时的,等待锁释放后重试即可成功。
+fn create_file_with_retry(outpath: &Path) -> Result<fs::File, String> {
+ const MAX_ATTEMPTS: u32 = 12;
+ const RETRY_DELAY: Duration = Duration::from_millis(250);
+
+ for attempt in 1..=MAX_ATTEMPTS {
+ match fs::File::create(outpath) {
+ Ok(file) => return Ok(file),
+ Err(e) if matches!(e.raw_os_error(), Some(32 | 5)) => {
+ log::warn!(
+ "File {:?} is locked by another process (attempt {}/{})",
+ outpath,
+ attempt,
+ MAX_ATTEMPTS
+ );
+ std::thread::sleep(RETRY_DELAY);
+ }
+ Err(e) => return Err(e.to_string()),
+ }
+ }
+ Err(format!(
+ "File {:?} is still locked after {} attempts",
+ outpath, MAX_ATTEMPTS
+ ))
+}
+
/// 解压 ZIP 文件到指定目录
pub fn extract_zip<'a, R: Runtime>(
tracker: &ProgressTracker<'a, R>,
@@ -69,7 +99,7 @@ pub fn extract_zip<'a, R: Runtime>(
format!("Failed to create parent directory {:?}: {}", p, e)
})?;
}
- let mut outfile = fs::File::create(&outpath).map_err(|e| {
+ let mut outfile = create_file_with_retry(&outpath).map_err(|e| {
log::error!(
"Failed to create file {:?}: {} (parent exists: {})",
outpath,
diff --git a/src-tauri/src/service/workflow/mod.rs b/src-tauri/src/service/workflow/mod.rs
index ffd0faf3..1e36e97e 100644
--- a/src-tauri/src/service/workflow/mod.rs
+++ b/src-tauri/src/service/workflow/mod.rs
@@ -253,6 +253,15 @@ pub async fn stop(app_handle: tauri::AppHandle) -> Result<(), String> {
/// 安装环境(Node.js 运行时 + 打包的 Harness 发行版)
pub async fn install(app_handle: &tauri::AppHandle) -> Result<(), String> {
log::info!("Starting installation process");
+
+ // 安装前先停止正在运行的 Harness 服务:运行中的 node 进程会把
+ // 原生模块 DLL(如 sharp 的 libvips-42.dll)加载进内存并锁住文件,
+ // 不停止的话覆盖解压必然失败(Windows os error 32)
+ if is_dsh_running().await {
+ log::info!("Stopping running Harness service before installation");
+ stop(app_handle.clone()).await?;
+ }
+
let window = app_handle
.get_webview_window("main")
.ok_or("Failed to get main window")?;