From 16dacb0c504bef3ca2bacd9478eccaa640e9780d Mon Sep 17 00:00:00 2001 From: Martin Minkus Date: Tue, 1 Sep 2026 10:42:49 -0700 Subject: [PATCH] wgengine/router/osrouter: don't destroy the FreeBSD tun from Close freebsdRouter.Close ran "ifconfig tailscale0 destroy" while this process still holds the tun open: the engine closes the router before the tun device, and FreeBSD's tun(4) makes SIOCIFDESTROY sleep until the last descriptor closes. tailscaled therefore deadlocks against its own child ifconfig on every shutdown, and "service tailscaled restart" hangs forever: 51709 - I /usr/local/bin/tailscaled -port 41641 -tun tailscale0 ... 52455 - I ifconfig tailscale0 destroy 52403 1 I+ /bin/sh /usr/local/etc/rc.d/tailscaled restart Do only the PF cleanup at Close time. The interface left behind at process exit is destroyed by the next startup's cleanUp hook (router.HookCleanUp), which exists for exactly that and runs when nothing holds the device. Updates #5573 Change-Id: I7b739d948718bfb460240d2c0dd74658fc40ac82 Signed-off-by: Martin Minkus --- wgengine/router/osrouter/router_freebsd.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/wgengine/router/osrouter/router_freebsd.go b/wgengine/router/osrouter/router_freebsd.go index 0d1c37eb8..59aab5582 100644 --- a/wgengine/router/osrouter/router_freebsd.go +++ b/wgengine/router/osrouter/router_freebsd.go @@ -357,11 +357,16 @@ func (r *freebsdRouter) delPFNATRules() error { } func (r *freebsdRouter) Close() error { - cleanUp(r.logf, r.tunname) + // Don't destroy the interface here: this process still holds the tun + // open, and FreeBSD's tun(4) blocks destroy until the last close, which + // deadlocks shutdown. The next startup's cleanUp destroys leftovers. + cleanUpPF(r.logf) return nil } -func cleanUp(logf logger.Logf, interfaceName string) { +// cleanUpPF undoes this process's PF changes: it flushes the tailscale +// anchor's contents and removes the anchor references if we added them. +func cleanUpPF(logf logger.Logf) { // Flush only the tailscale PF anchor, leaving user rules intact. if out, err := cmd("pfctl", "-a", pfAnchorName, "-F", "all").CombinedOutput(); err != nil { logf("pfctl flush anchor %s: %v (%s)", pfAnchorName, err, strings.TrimSpace(string(out))) @@ -370,6 +375,12 @@ func cleanUp(logf logger.Logf, interfaceName string) { if err := removePFAnchorRef(); err != nil { logf("removing PF anchor ref: %v", err) } +} + +// cleanUp cleans up after a previous tailscaled process. It runs at startup +// (via router.HookCleanUp), when nothing holds the interface open. +func cleanUp(logf logger.Logf, interfaceName string) { + cleanUpPF(logf) // If the interface was left behind, ifconfig down will not remove it. // In fact, this will leave a system in a tainted state where starting tailscaled