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>,
#[arg(long)]
target: Option<String>,
#[arg(long)]
daemon: bool,
}
#[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();
Ok(())
@@ -199,6 +201,8 @@ fn main() -> SdkResult<()> {
#[cfg(target_os = "windows")]
{
use std::os::windows::process::CommandExt;
if !cli.daemon {
loop {
if let Err(_) = std::net::TcpListener::bind("0.0.0.0:3000") {
// Port in use, become a stub proxy!
@@ -212,7 +216,17 @@ fn main() -> SdkResult<()> {
Err(_) => std::thread::sleep(std::time::Duration::from_millis(1000)),
}
} 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));
}
}
}
}