fix: Proxy stdout corruption and auto-daemonize

This commit is contained in:
Riz Ashraf committed 2026-09-08 15:32:49 +01:00
1 parent 18fd05bb11
commit 5f0eba8a80
1 file changed
+16 -2
+16 -2
View File
@@ -31,6 +31,8 @@ struct Cli {
command: Option<Commands>, command: Option<Commands>,
#[arg(long)] #[arg(long)]
target: Option<String>, target: Option<String>,
#[arg(long)]
daemon: bool,
} }
#[derive(Subcommand)] #[derive(Subcommand)]
@@ -185,7 +187,7 @@ fn run_server(state: Arc<MemoryState>) -> SdkResult<()> {
}), }),
); );
println!("MCP Memory Server running on http://0.0.0.0:3000/sse"); eprintln!("MCP Memory Server running on http://0.0.0.0:3000/sse");
server.start().await.unwrap(); server.start().await.unwrap();
Ok(()) Ok(())
@@ -199,6 +201,8 @@ fn main() -> SdkResult<()> {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
{ {
use std::os::windows::process::CommandExt;
if !cli.daemon {
loop { loop {
if let Err(_) = std::net::TcpListener::bind("0.0.0.0:3000") { if let Err(_) = std::net::TcpListener::bind("0.0.0.0:3000") {
// Port in use, become a stub proxy! // Port in use, become a stub proxy!
@@ -212,7 +216,17 @@ fn main() -> SdkResult<()> {
Err(_) => std::thread::sleep(std::time::Duration::from_millis(1000)), Err(_) => std::thread::sleep(std::time::Duration::from_millis(1000)),
} }
} else { } else {
break; // Proceed as Leader // Port is free. We must spawn the daemon, then loop again to become proxy
std::process::Command::new(std::env::current_exe().unwrap())
.arg("--daemon")
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.creation_flags(0x08000000) // CREATE_NO_WINDOW
.spawn()
.expect("Failed to spawn daemon");
std::thread::sleep(std::time::Duration::from_millis(500));
}
} }
} }
} }