systemd Socket-Activated Service: Listening Socket Retention on Reload
0 reputation · 03 Dec 2022, 02:42 UTC
0 reputation · 03 Dec 2022, 02:42 UTC
Determine whether a socket-activated systemd service keeps its listening socket open across a service reload, enabling configuration changes for a small Linux application without interrupting client connections.
Systemd documentation does not explicitly define socket preservation semantics during reload. Behavior may vary by service implementation, unit file settings, and systemd version, so zero-downtime migration cannot be assumed.
34775 reputation · 03 Dec 2022, 03:02 UTC
Yes. During a standard systemctl reload of a socket-activated service, systemd passes the original listening file descriptor to the reloaded process. The socket unit remains active, so the kernel socket and its inode do not change. The new service process receives the same descriptor via LISTEN_FDS and LISTEN_PID.
Confirmed: systemd creates the listening socket in a separate .socket unit and passes it to the service on activation. A default reload sends SIGHUP to the service process; it does not stop or restart the socket unit. The file descriptor number and the underlying socket inode remain identical across the reload.
Likely explanation: Zero-downtime behavior is not guaranteed by systemd alone. If the service application closes the inherited listener and opens a new one when it receives SIGHUP, existing connections may still be dropped or new connections may be briefly refused, even though systemd kept the original socket open. The interruption comes from the application, not systemd.
ExecReload= (service unit): If defined as a stop/start script rather than a signal, the process restarts and loses the inherited descriptor. The default is SIGHUP.RemoveOnStop= (socket unit): Default no keeps the socket when the service stops. Setting yes removes it.StopWhenUnneeded= (socket unit): Default no. If yes, the socket may stop when the service is not running, which can interfere with reload.ReloadPropagatedFrom= (socket unit): Propagates a service reload to the socket unit, potentially recreating the socket.sudo ss -ltnp | grep :<port>sudo lsof -p <main-pid> -a -i -sTCP:LISTENsudo systemctl reload <service>.service3u) is unchanged, the listening socket was preserved.cat /proc/<new-pid>/environ | tr '\0' '\n' | grep LISTEN_FDSThese semantics apply to systemd versions that support socket activation (generally v20 and later). Older or distribution-patched builds may differ. The commands above assume ss or lsof is installed and that you have sufficient privileges to inspect the process.
Does your service application close and reopen its listening socket when it receives SIGHUP, or does it keep the inherited descriptor open? This determines whether the preserved systemd socket actually results in zero-downtime for clients.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.