fix(server): safely drop detached tokio join handles and refactor timestamp boilerplate

This commit is contained in:
Riz Ashraf committed 2026-09-22 03:19:51 +01:00
1 parent 13c7eca4c2
commit 478655698e
15 files changed
+457 -312

No files matched your search

+26 -17
View File
@@ -22,7 +22,7 @@ use redb::ReadableTable;
use std::fs;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use std::time::Duration;
use clap::{Parser, Subcommand};
use std::collections::HashMap;
@@ -204,10 +204,7 @@ async fn gate_set_handler(
params: body.params.clone(),
status,
reason: body.reason.clone(),
timestamp: SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs(),
timestamp: crate::handlers_v2::utils::now_secs(),
};
app_state.handler.state.gates.modify(|gates| {
gates.retain(|g| !(g.action == record.action && g.target == record.target));
@@ -430,7 +427,9 @@ async fn run_server(state: Arc<MemoryState>) -> Result<(), Box<dyn std::error::E
tracing::info!("MCP Memory Server running on http://127.0.0.1:3000/sse");
let addr = std::env::var("MCP_PORT").unwrap_or_else(|_| "3000".to_string());
let addr: std::net::SocketAddr = format!("127.0.0.1:{}", addr).parse().expect("Invalid bind address");
let addr: std::net::SocketAddr = format!("127.0.0.1:{}", addr)
.parse()
.expect("Invalid bind address");
let listener = match tokio::net::TcpListener::bind(&addr).await {
Ok(l) => l,
@@ -726,14 +725,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
if !cli.daemon {
// Just spawn the daemon and exit. We no longer act as a proxy.
#[allow(clippy::zombie_processes)]
let _ = std::process::Command::new(std::env::current_exe().expect("Failed to get current executable path"))
.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");
let _ = std::process::Command::new(
std::env::current_exe().expect("Failed to get current executable path"),
)
.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");
return Ok(());
}
}
@@ -752,7 +753,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
{
let write_txn = db.begin_write().expect("Failed to begin write txn on redb");
{
let mut table = write_txn.open_table(crate::store::STORE_TABLE).expect("Failed to open STORE_TABLE");
let mut table = write_txn
.open_table(crate::store::STORE_TABLE)
.expect("Failed to open STORE_TABLE");
let stores = vec![
("knowledge_graph_master", "knowledge_graph_master.json"),
@@ -777,13 +780,19 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
];
for (key, file_name) in stores.iter() {
if table.get(*key).expect("Failed to read from table").is_none() {
if table
.get(*key)
.expect("Failed to read from table")
.is_none()
{
let json_path = base.join(file_name);
if json_path.exists()
&& let Ok(data) = fs::read(&json_path)
&& serde_json::from_slice::<serde_json::Value>(&data).is_ok()
{
table.insert(*key, data.as_slice()).expect("Failed to insert migrated data");
table
.insert(*key, data.as_slice())
.expect("Failed to insert migrated data");
let _ = fs::rename(&json_path, json_path.with_extension("json.migrated"));
}
}