Repository navigation
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 thanderp.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 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
wgengine/magicsock/derp_test.goput a TCP proxy between magicsock and a realderpserver. 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 inderp.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.TS_DEBUG_ALWAYS_USE_DERP). I froze one node's DERP connection and marked it quiet, then sent a WireGuard ping through the tunnel:derp-1 quiet for 5m0s, ping failed: context deadline exceeded)derp.Recv(derp-1): ... i/o timeout)./tool/go test -race ./wgengine/magicsock/ ./wgengine/ ./derp/...passes.-raceand 40 times with-cpu=1,4, with no failures.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