Skip to content

wgengine/magicsock: ping a quiet DERP connection before writing to it - #21768

Open
GodKimba wants to merge 1 commit into
tailscale:mainfrom
GodKimba:derp-stale-ping
Open

GodKimba wants to merge 1 commit into
tailscale:mainfrom
GodKimba:derp-stale-ping

Conversation

@GodKimba

Copy link
Copy Markdown

A DERP client only notices that its connection died when its read deadline expires, two minutes after the last frame it read. That deadline runs on the monotonic clock, which stops while the device sleeps. So a phone that wakes after its carrier dropped the TCP connection keeps writing into the dead connection for up to two minutes of awake time. Nothing rebinds, because the local network state didn't change, and the DERP server keeps routing the node's packets to the old connection. To the user, the tailnet is down until they toggle the VPN.

The server sends a keepalive at least every derp.KeepAlive (60s), so a connection that has been silent for longer than that is suspect. When magicsock is about to write to such a connection (silent for more than derp.KeepAlive + 10s), it now pings the server first. If no pong arrives within 3s, it closes the connection, so the next write reconnects. The quiet period is measured in wall time, which keeps running while the device sleeps.

Cost on healthy connections:

  • A live connection receives a keepalive before the threshold, so it is rarely pinged.
  • An idle node sends nothing, because the check only runs on the write path.
  • There is at most one ping in flight per connection.
  • If the connection was closed or replaced while the ping was in flight, nothing is done.

A new clientmetric, derp_quiet_ping_fail, counts the connections closed this way.

Updates #15776

This doesn't close #15776. It covers the case where the connection dies silently. A node that changed networks and has a DERP connection that is still alive but stale is a separate path.

Testing

  • New tests in wgengine/magicsock/derp_test.go put a TCP proxy between magicsock and a real derpserver. The proxy can "freeze" a connection, which keeps it open but drops every byte in both directions, the way a NAT mapping does when it expires during sleep.
    • TestQuietDERPConnPingedBeforeWrite: a connection heard from recently isn't pinged. A frozen connection that has been quiet is replaced, and the new connection answers a ping. Without the change in derp.go, this test fails ("dead DERP connection not replaced yet").
    • TestQuietDERPConnPingSuccessKeepsConn: a connection that is quiet but alive is kept after its ping succeeds, with no reconnect.
  • I also ran a throwaway end-to-end test, not included in this PR. Two magicsock+wireguard-go stacks were forced onto DERP only (TS_DEBUG_ALWAYS_USE_DERP). I froze one node's DERP connection and marked it quiet, then sent a WireGuard ping through the tunnel:
    • with this change, it got through after 3s (log: derp-1 quiet for 5m0s, ping failed: context deadline exceeded)
    • without it, it took 2m0.2s, which is the read deadline (derp.Recv(derp-1): ... i/o timeout)
  • ./tool/go test -race ./wgengine/magicsock/ ./wgengine/ ./derp/... passes.
  • I repeated the new tests 30 times with -race and 40 times with -cpu=1,4, with no failures.
  • staticcheck is clean.
  • Builds pass for ios/arm64, darwin/arm64, android/arm64, windows/amd64, linux/386, js/wasm and ts_omit_udptransport.

Context

Reported on an iPhone using Tailscale over 5G. After about 3 minutes in the background, nothing from the phone reached a Linux peer, while the app still showed "Connected". The peer logged derp-11 does not know about peer [...], and traffic only came back when the VPN was toggled. Similar reports are in #19504 and #21353.

🤖 Generated with Claude Code

A DERP client only notices that its connection died when its read
deadline expires, two minutes after the last frame it read. The
deadline runs on the monotonic clock, which stops while the device
sleeps, so a phone that wakes after its carrier dropped the TCP
connection keeps writing into the dead connection for up to two
minutes of awake time. Nothing rebinds, because the local network
state didn't change, and the DERP server keeps routing the node's
packets to the old connection. To the user, the tailnet is down until
they toggle the VPN.

The server sends a keepalive at least every derp.KeepAlive, so a
connection that has been silent for longer than that is suspect. When
magicsock is about to write to such a connection, ping the server, and
if no pong arrives within 3s, close the connection so the next write
reconnects. The quiet period is measured in wall time, which keeps
running while the device sleeps. Live connections get a keepalive
before the threshold, so they are rarely pinged, and an idle node
sends nothing.

A new clientmetric, derp_quiet_ping_fail, counts the connections
closed this way.

Updates tailscale#15776

Change-Id: I191cf7f8d589f3d2af367d9e60db228a92c2d422
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: Rafael Fadel <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clients do not promptly observe loss of DERP connectivity in some scenarios

1 participant