fix: notify systemd of new MAINPID during reload to prevent service termination

This commit is contained in:
2026-01-27 14:17:57 +08:00
parent 897237755f
commit 6ab6d2da90
2 changed files with 26 additions and 5 deletions

View File

@@ -100,10 +100,18 @@ pub async fn bind_listener(
use std::net::SocketAddr;
// AsRawFd removed
let addr: SocketAddr = addr_str.parse().map_err(|e: std::net::AddrParseError| {
error!("[{}] invalid address {}: {}", service_name, addr_str, e);
anyhow::anyhow!(e)
})?;
let normalized_addr = if addr_str.starts_with(":::") {
format!("[::]:{}", &addr_str[3..])
} else {
addr_str.to_string()
};
let addr: SocketAddr = normalized_addr
.parse()
.map_err(|e: std::net::AddrParseError| {
error!("[{}] invalid address {}: {}", service_name, addr_str, e);
anyhow::anyhow!(e)
})?;
let domain = if addr.is_ipv4() {
socket2::Domain::IPV4

View File

@@ -132,7 +132,20 @@ async fn main() -> anyhow::Result<()> {
match std::process::Command::new(&args[0])
.args(&args[1..])
.spawn() {
Ok(child) => info!("spawned new process with pid: {}", child.id()),
Ok(child) => {
let child_pid = child.id();
info!("spawned new process with pid: {}", child_pid);
// Notify systemd of NEW main PID so it doesn't kill the service when we exit
if let Ok(notify_socket) = std::env::var("NOTIFY_SOCKET") {
if let Ok(sock) = std::os::unix::net::UnixDatagram::unbound() {
let msg = format!("MAINPID={}\n", child_pid);
if let Err(e) = sock.send_to(msg.as_bytes(), notify_socket) {
error!("failed to send MAINPID to systemd: {}", e);
}
}
}
},
Err(e) => error!("failed to spawn new process: {}", e),
}
// Initiate graceful shutdown for this process