sing-box icon indicating copy to clipboard operation
sing-box copied to clipboard

Feature request: systemd readiness notification (sd_notify) for Type=notify

Open hebo6 opened this issue 1 month ago • 0 comments

Problem

The shipped unit release/config/sing-box.service uses the default Type=simple, so systemd marks sing-box as started right after ExecStart is forked, long before it is actually ready.

With a TUN inbound, a lot happens after that point. In box.Start() the TUN interface is created in StartStateStart. Then, in StartStatePostStart, the link is brought up, routes and ip rules are installed and auto_redirect nftables rules are applied. Before startup there may also be remote rule-set downloads. Any unit ordered with After=sing-box.service can start while traffic is not yet routed through sing-box.

Real-world log (1.13.0-alpha.36, TUN with auto_route + auto_redirect, remote rule-sets):

19:09:47.671 systemd[1]: Starting sing-box service...       <- systemd considers it started here
19:09:49.148 inbound/tun[0]: started at tun0
19:09:49.148 inbound/http[...]: tcp server started at 127.0.0.1:10809
19:09:49.440 sing-box started (1.633s)                      <- actually ready

Neither the TUN interface appearing nor the inbound port listening can be used as a readiness probe. Both happen before routes/nftables are set up. The only workaround is a guessed delay such as ExecStartPost=/usr/bin/sleep 5, which is either too long or not long enough depending on network conditions.

Proposal

Support the systemd notification protocol (sd_notify) when $NOTIFY_SOCKET is set:

  • Send READY=1 after instance.Start() succeeds in create() (cmd/sing-box/cmd_run.go), right after sing-box started is logged
  • Optionally send STOPPING=1 before closing, and RELOADING=1 / READY=1 around the SIGHUP reload. This would also allow Type=notify-reload
  • Change the shipped unit to Type=notify

The protocol is just writing a datagram to the unix socket named by $NOTIFY_SOCKET. It needs no extra dependency and is a no-op when the variable is unset, so non-systemd environments are unaffected.

Related

#733 asked for socket activation, which is a different feature

hebo6 avatar Oct 16 '26 12:10 hebo6