This commit is contained in:
root 2025-07-10 19:27:14 +08:00
parent 8ae1718172
commit 946673c699
1 changed files with 44 additions and 14 deletions

View File

@ -1,6 +1,6 @@
use serde::Deserialize;
use std::process::Command;
use std::io::{BufRead, BufReader};
use std::io::{BufRead, BufReader, Read};
use std::time::{Duration, Instant};
use std::thread;
use std::sync::{Arc, Mutex};
@ -81,8 +81,8 @@ async fn execute_python_command(_app: tauri::AppHandle, args: &[String]) -> Resu
let stderr = child.stderr.take().unwrap();
// Use channels for concurrent reading
let (stdout_tx, stdout_rx) = mpsc::channel();
let (stderr_tx, stderr_rx) = mpsc::channel();
let (stdout_tx, _stdout_rx) = mpsc::channel();
let (stderr_tx, _stderr_rx) = mpsc::channel();
let (result_tx, result_rx) = mpsc::channel();
let progress_messages = Arc::new(Mutex::new(Vec::new()));
@ -93,10 +93,22 @@ async fn execute_python_command(_app: tauri::AppHandle, args: &[String]) -> Resu
let progress_messages_clone = Arc::clone(&progress_messages);
let final_result_clone = Arc::clone(&final_result);
thread::spawn(move || {
let reader = BufReader::new(stdout);
for line in reader.lines() {
match line {
Ok(line) => {
let mut reader = BufReader::new(stdout);
let mut buffer = Vec::new();
// Read raw bytes and handle encoding issues
loop {
buffer.clear();
match reader.read_until(b'\n', &mut buffer) {
Ok(0) => break, // EOF
Ok(_) => {
// Convert bytes to string, handling invalid UTF-8
let line = String::from_utf8_lossy(&buffer);
let line = line.trim_end_matches('\n').trim_end_matches('\r');
if line.is_empty() {
continue;
}
println!("Python stdout: {}", line);
// Parse JSON-RPC messages
@ -133,7 +145,7 @@ async fn execute_python_command(_app: tauri::AppHandle, args: &[String]) -> Resu
println!("Python other: {}", line);
}
if stdout_tx.send(line).is_err() {
if stdout_tx.send(line.to_string()).is_err() {
break;
}
}
@ -148,15 +160,28 @@ async fn execute_python_command(_app: tauri::AppHandle, args: &[String]) -> Resu
// Spawn thread for reading stderr
let error_messages_clone = Arc::clone(&error_messages);
thread::spawn(move || {
let reader = BufReader::new(stderr);
for line in reader.lines() {
match line {
Ok(line) => {
let mut reader = BufReader::new(stderr);
let mut buffer = Vec::new();
// Read raw bytes and handle encoding issues
loop {
buffer.clear();
match reader.read_until(b'\n', &mut buffer) {
Ok(0) => break, // EOF
Ok(_) => {
// Convert bytes to string, handling invalid UTF-8
let line = String::from_utf8_lossy(&buffer);
let line = line.trim_end_matches('\n').trim_end_matches('\r');
if line.is_empty() {
continue;
}
println!("Python stderr: {}", line);
if let Ok(mut messages) = error_messages_clone.lock() {
messages.push(line.clone());
messages.push(line.to_string());
}
if stderr_tx.send(line).is_err() {
if stderr_tx.send(line.to_string()).is_err() {
break;
}
}
@ -215,6 +240,10 @@ async fn execute_python_command(_app: tauri::AppHandle, args: &[String]) -> Resu
println!("Process was likely terminated by antivirus or system security");
} else if code == 1 {
println!("Process exited with error code 1 (general error)");
} else if code == 120 {
println!("Process exited with error code 120 (module import failure or unsupported function)");
} else {
println!("Process exited with unknown error code: {}", code);
}
}
} else {
@ -253,6 +282,7 @@ async fn execute_python_command(_app: tauri::AppHandle, args: &[String]) -> Resu
let unsigned_code = code as u32;
match code {
1 => "Python script failed with general error. Check if all dependencies are installed.".to_string(),
120 => "Python module import failed or function not supported. This may be due to missing dependencies or incompatible Python environment. Please run the environment test first.".to_string(),
-1073741510 => "Python process was terminated by antivirus or system security. Please add the application to your antivirus whitelist.".to_string(),
_ if unsigned_code == 3221225786 => "Python process was terminated by antivirus or system security. Please add the application to your antivirus whitelist.".to_string(),
_ => format!("Python script failed with exit code: {}. This may indicate a system or environment issue.", code)