use std::path::Path;
use std::time::{Duration, Instant};
use crate::launch::Launch;
use crate::persist::{lock, Endpoint};
pub const DEFAULT_START_TIMEOUT: Duration = Duration::from_secs(60);
pub const START_TIMEOUT_ENV: &str = "MEMFORK_START_TIMEOUT";
pub const SLOW_START: Duration = Duration::from_secs(3);
const AFTER_EXIT: Duration = Duration::from_secs(2);
pub const LOG_FILE: &str = "memfork-daemon.log";
const POLL: Duration = Duration::from_millis(25);
#[derive(Debug, Clone, thiserror::Error)]
pub enum DaemonError {
#[error("{0}")]
DidNotStart(String),
#[error(
"a MemFork daemon from version {theirs} already owns {dir}, and this is \
version {ours}.\n\
Two versions must not share one store: the formats they write need not \
match, and guessing is how data gets corrupted.\n\
Run `memfork stop` to shut the running one down, then try again. If you \
upgraded with pip or cargo, any tool still running the old `memfork mcp` \
keeps the old version until that tool is restarted."
)]
VersionMismatch {
theirs: String,
ours: String,
dir: String,
},
#[error("cannot start the MemFork daemon: {0}")]
Spawn(String),
}
pub fn usable(dir: &Path) -> Result<Option<Endpoint>, DaemonError> {
let Some(endpoint) = lock::owner(dir) else {
return Ok(None);
};
if endpoint.port.is_none() {
return Ok(Some(endpoint));
}
check_version(dir, &endpoint)?;
Ok(Some(endpoint))
}
fn check_version(dir: &Path, endpoint: &Endpoint) -> Result<(), DaemonError> {
let theirs = endpoint.memfork_version.as_deref().unwrap_or("unknown");
if theirs != crate::VERSION {
return Err(DaemonError::VersionMismatch {
theirs: theirs.to_owned(),
ours: crate::VERSION.to_owned(),
dir: crate::style::path(dir),
});
}
Ok(())
}
pub fn ensure(dir: &Path, launch: &Launch, idle_seconds: u64) -> Result<Endpoint, DaemonError> {
ensure_reporting(dir, launch, idle_seconds, &|waited: Duration| {
eprintln!("memfork: {}", still_starting(waited));
})
}
pub fn still_starting(waited: Duration) -> String {
format!(
"still starting the MemFork daemon ({}s so far); a first start can take a \
while as the system checks a new program and the store is read",
waited.as_secs()
)
}
pub fn start_timeout() -> Result<Duration, DaemonError> {
match std::env::var(START_TIMEOUT_ENV) {
Err(_) => Ok(DEFAULT_START_TIMEOUT),
Ok(raw) if raw.trim().is_empty() => Ok(DEFAULT_START_TIMEOUT),
Ok(raw) => match raw.trim().parse::<u64>() {
Ok(seconds) if seconds > 0 => Ok(Duration::from_secs(seconds)),
_ => Err(DaemonError::Spawn(format!(
"{START_TIMEOUT_ENV} must be a whole number of seconds above zero, \
got `{raw}`"
))),
},
}
}
pub fn ensure_reporting(
dir: &Path,
launch: &Launch,
idle_seconds: u64,
slow: &dyn Fn(Duration),
) -> Result<Endpoint, DaemonError> {
if let Some(endpoint) = usable(dir)? {
if endpoint.port.is_some() {
return Ok(endpoint);
}
}
let timeout = start_timeout()?;
let log = dir.join(LOG_FILE);
let (tried, mut child) = spawn(dir, launch, idle_seconds, &log)?;
let started = Instant::now();
let mut deadline = started + timeout;
let mut said_slow = false;
let mut last = String::from("no endpoint file appeared");
while Instant::now() < deadline {
if let Some(status) = child.as_mut().and_then(|c| c.try_wait().ok().flatten()) {
child = None;
last = match status.code() {
Some(code) => format!("its process exited with code {code} before serving"),
None => "its process was ended before serving".to_owned(),
};
deadline = deadline.min(Instant::now() + AFTER_EXIT);
}
if !said_slow && started.elapsed() >= SLOW_START {
slow(started.elapsed());
said_slow = true;
}
match usable(dir) {
Ok(Some(endpoint)) if endpoint.port.is_some() => return Ok(endpoint),
Ok(Some(_)) => last = "something owns the directory but is not serving".to_owned(),
Ok(None) => {}
Err(e @ DaemonError::VersionMismatch { .. }) => return Err(e),
Err(e) => last = e.to_string(),
}
std::thread::sleep(POLL);
}
Err(DaemonError::DidNotStart(did_not_start(
started.elapsed().max(Duration::from_secs(1)).min(timeout),
timeout,
&last,
&tried,
&log,
)))
}
fn did_not_start(
waited: Duration,
timeout: Duration,
observed: &str,
tried: &str,
log: &Path,
) -> String {
let tail: Vec<String> = std::fs::read_to_string(log)
.map(|text| {
let lines: Vec<String> = text
.lines()
.map(str::trim_end)
.filter(|l| !l.is_empty())
.map(str::to_owned)
.collect();
let skip = lines.len().saturating_sub(8);
lines.into_iter().skip(skip).collect()
})
.unwrap_or_default();
let output = if tail.is_empty() {
format!(
" Its own output goes to {} (nothing was written there).",
crate::style::path(log)
)
} else {
format!(
" Its own output is in {}, which ends:\n{}",
crate::style::path(log),
tail.iter()
.map(|l| format!(" {l}"))
.collect::<Vec<_>>()
.join("\n")
)
};
let when = if waited < timeout {
format!("after {}s", waited.as_secs())
} else {
format!("within {}s", timeout.as_secs())
};
format!(
"the MemFork daemon did not start {when} ({observed}).\n\
\x20 Tried: {tried}\n\
{output}\n\
\x20 What to do: run that command yourself to see it fail in front of you; \
run `memfork doctor` to see the state of the data directory; and if this \
machine is simply slow to start a new program (an antivirus scan of a new \
executable, say), allow longer with {START_TIMEOUT_ENV}=180 and try again."
)
}
fn spawn(
dir: &Path,
launch: &Launch,
idle_seconds: u64,
log: &Path,
) -> Result<(String, Option<std::process::Child>), DaemonError> {
#[cfg(windows)]
windows_handles::stop_inheriting_std_handles();
let argv = launch.argv(&[
"serve",
"--data-dir",
&crate::style::path(dir),
"--port",
"0",
"--idle-timeout",
&idle_seconds.to_string(),
]);
let tried = argv
.iter()
.map(|part| {
if part.contains(' ') {
format!("\"{part}\"")
} else {
part.clone()
}
})
.collect::<Vec<_>>()
.join(" ");
if let Some(helper) = spawn_helper() {
spawn_through(&helper, &argv)?;
return Ok((tried, None));
}
let (program, args) = argv.split_first().ok_or_else(|| {
DaemonError::Spawn("there is no command to start a daemon with".to_owned())
})?;
let errors = std::fs::OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(log)
.map(|mut file| {
use std::io::Write as _;
let _ = writeln!(file, "memfork: starting the daemon: {}", argv.join(" "));
std::process::Stdio::from(file)
})
.unwrap_or_else(|_| std::process::Stdio::null());
let mut command = std::process::Command::new(program);
command
.args(args)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(errors);
#[cfg(unix)]
{
use std::os::unix::process::CommandExt;
command.process_group(0);
}
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200;
const DETACHED_PROCESS: u32 = 0x0000_0008;
command.creation_flags(CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS);
}
command
.spawn()
.map(|child| (tried, Some(child)))
.map_err(|e| DaemonError::Spawn(e.to_string()))
}
pub const SPAWN_VIA_ENV: &str = "MEMFORK_SPAWN_VIA";
fn spawn_helper() -> Option<Vec<String>> {
let raw = std::env::var(SPAWN_VIA_ENV).ok()?;
let parts: Vec<String> = serde_json::from_str(&raw).ok()?;
(!parts.is_empty()).then_some(parts)
}
fn spawn_through(helper: &[String], argv: &[String]) -> Result<(), DaemonError> {
let command = serde_json::to_string(argv)
.map_err(|e| DaemonError::Spawn(format!("cannot encode the daemon command: {e}")))?;
let (program, args) = helper
.split_first()
.ok_or_else(|| DaemonError::Spawn("the spawn helper is empty".to_owned()))?;
let output = std::process::Command::new(program)
.args(args)
.arg(command)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::piped())
.output()
.map_err(|e| DaemonError::Spawn(format!("cannot run the spawn helper: {e}")))?;
if output.status.success() {
return Ok(());
}
Err(DaemonError::Spawn(format!(
"the spawn helper failed: {}",
String::from_utf8_lossy(&output.stderr).trim()
)))
}
#[cfg(windows)]
mod windows_handles {
use windows_sys::Win32::Foundation::{SetHandleInformation, HANDLE_FLAG_INHERIT};
use windows_sys::Win32::System::Console::{
GetStdHandle, STD_ERROR_HANDLE, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
};
#[allow(unsafe_code)]
pub(super) fn stop_inheriting_std_handles() {
for which in [STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE] {
unsafe {
let handle = GetStdHandle(which);
if !handle.is_null() {
SetHandleInformation(handle, HANDLE_FLAG_INHERIT, 0);
}
}
}
}
}
#[cfg(windows)]
#[cfg_attr(not(feature = "brain"), allow(dead_code))]
pub(crate) fn stop_inheriting_std_handles() {
windows_handles::stop_inheriting_std_handles();
}
fn post_shutdown(port: u16, token: &str) -> Result<(), String> {
use std::io::{Read, Write};
let address = format!("127.0.0.1:{port}");
let mut stream = std::net::TcpStream::connect(&address)
.map_err(|e| format!("cannot reach the daemon on {address}: {e}"))?;
stream
.set_read_timeout(Some(Duration::from_secs(10)))
.map_err(|e| format!("cannot set a timeout on the connection: {e}"))?;
let request = format!(
"POST {path} HTTP/1.1\r\n\
Host: 127.0.0.1:{port}\r\n\
Authorization: Bearer {token}\r\n\
Content-Length: 0\r\n\
Connection: close\r\n\
\r\n",
path = crate::serve::SHUTDOWN_PATH,
);
stream
.write_all(request.as_bytes())
.map_err(|e| format!("cannot send the shutdown request: {e}"))?;
let mut answer = String::new();
let _ = stream.read_to_string(&mut answer);
if answer.starts_with("HTTP/1.1 401") {
return Err("the daemon rejected the token in its own endpoint file".to_owned());
}
Ok(())
}
pub fn stop(dir: &Path) -> Result<String, String> {
let Some(endpoint) = lock::owner(dir) else {
return Ok(format!("nothing is running on {}", crate::style::path(dir)));
};
let (Some(port), Some(token)) = (endpoint.port, endpoint.token.clone()) else {
return Err(format!(
"process {} owns {} but is not a daemon, so there is nothing to stop.\n\
It will release the directory when it exits.",
endpoint.pid,
crate::style::path(dir)
));
};
post_shutdown(port, &token)?;
let deadline = Instant::now() + Duration::from_secs(10);
while Instant::now() < deadline {
if lock::owner(dir).is_none() {
return Ok(format!(
"stopped the daemon (process {}) on {}",
endpoint.pid,
crate::style::path(dir)
));
}
std::thread::sleep(POLL);
}
Err(format!(
"asked process {} to stop, and it still holds {} ten seconds later",
endpoint.pid,
crate::style::path(dir)
))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn nothing_running_is_not_an_error() {
let dir = tempfile::tempdir().expect("tempdir");
assert!(usable(dir.path()).expect("checked").is_none());
assert!(stop(dir.path())
.expect("stop")
.contains("nothing is running"));
}
#[test]
fn a_daemon_from_another_version_is_refused_by_name() {
let dir = tempfile::tempdir().expect("tempdir");
let endpoint = Endpoint {
memfork_version: Some("0.0.1-from-the-past".to_owned()),
port: Some(1234),
token: Some("t".to_owned()),
..Endpoint::for_this_process()
};
match check_version(dir.path(), &endpoint) {
Err(DaemonError::VersionMismatch { theirs, ours, .. }) => {
assert_eq!(theirs, "0.0.1-from-the-past");
assert_eq!(ours, crate::VERSION);
}
other => panic!("a foreign version was accepted: {other:?}"),
}
}
#[test]
fn a_daemon_that_declares_no_version_is_also_refused() {
let dir = tempfile::tempdir().expect("tempdir");
let endpoint = Endpoint {
memfork_version: None,
port: Some(1234),
token: Some("t".to_owned()),
..Endpoint::for_this_process()
};
assert!(check_version(dir.path(), &endpoint).is_err());
}
#[test]
fn our_own_version_is_accepted() {
let dir = tempfile::tempdir().expect("tempdir");
let endpoint = Endpoint {
memfork_version: Some(crate::VERSION.to_owned()),
port: Some(1234),
token: Some("t".to_owned()),
..Endpoint::for_this_process()
};
assert!(check_version(dir.path(), &endpoint).is_ok());
}
#[test]
fn the_mismatch_message_says_what_to_do_about_it() {
let err = DaemonError::VersionMismatch {
theirs: "0.0.1".to_owned(),
ours: "9.9.9".to_owned(),
dir: "/tmp/x".to_owned(),
};
let text = err.to_string();
assert!(text.contains("memfork stop"), "{text}");
assert!(text.contains("0.0.1") && text.contains("9.9.9"), "{text}");
}
}