fix(appd): make appd.wsport write non-fatal when XDG_RUNTIME_DIR is unset

write_ws_port failure is now logged as a warning rather than propagating
an error that would crash the service. Error context strings are added
to create_dir_all and write failures so the warning is actionable.
This commit is contained in:
Marco Allegretti 2026-03-11 11:36:47 +01:00
parent eef9ecc24a
commit e83be20798

View file

@ -132,7 +132,9 @@ async fn run() -> anyhow::Result<()> {
.with_context(|| format!("bind WebSocket {ws_addr}"))?; .with_context(|| format!("bind WebSocket {ws_addr}"))?;
let ws_bound_port = ws_listener.local_addr()?.port(); let ws_bound_port = ws_listener.local_addr()?.port();
tracing::info!(port = ws_bound_port, "WebSocket listener ready"); tracing::info!(port = ws_bound_port, "WebSocket listener ready");
write_ws_port(ws_bound_port)?; if let Err(e) = write_ws_port(ws_bound_port) {
tracing::warn!(error = %e, "could not write appd.wsport; servo-shell port discovery will fall back to default");
}
let _ = sd_notify::notify(false, &[sd_notify::NotifyState::Ready]); let _ = sd_notify::notify(false, &[sd_notify::NotifyState::Ready]);
@ -196,9 +198,9 @@ fn write_ws_port(port: u16) -> anyhow::Result<()> {
let runtime_dir = std::env::var("XDG_RUNTIME_DIR").context("XDG_RUNTIME_DIR not set")?; let runtime_dir = std::env::var("XDG_RUNTIME_DIR").context("XDG_RUNTIME_DIR not set")?;
let path = PathBuf::from(runtime_dir).join("weft/appd.wsport"); let path = PathBuf::from(runtime_dir).join("weft/appd.wsport");
if let Some(parent) = path.parent() { if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?; std::fs::create_dir_all(parent).with_context(|| format!("create {}", parent.display()))?;
} }
std::fs::write(&path, port.to_string())?; std::fs::write(&path, port.to_string()).with_context(|| format!("write {}", path.display()))?;
Ok(()) Ok(())
} }