Skip to content

wgengine/magicsock: rebind and retry when a UDP receive fails - #22084

Closed
luben93 wants to merge 1 commit into
tailscale:mainfrom
luben93:luben93/magicsock-receive-recover
Closed

luben93 wants to merge 1 commit into
tailscale:mainfrom
luben93:luben93/magicsock-receive-recover

Conversation

@luben93

@luben93 luben93 commented Oct 30, 2026

Copy link
Copy Markdown

Makes magicsock recover when a UDP receive fails instead of leaving the tunnel unable to receive.

Symptom

On iOS the tunnel sometimes shows connected but passes no traffic; apps stall mid-stream and only toggling Tailscale recovers. Many reports in #17967 and earlier #14320, often with the health warning "MagicSock function ReceiveIPv4 is not running" (magicsock-receive-func-error). The iOS and Android apps use this code.

Mechanism (from reading the code, not reproduced on a device)

  • Conn.mkReceiveFunc returned any non-truncated ReadBatch error to wireguard-go.
  • In wireguard-go's RoutineReceiveIncoming, a returned error ends the receive goroutine permanently if it is a non-temporary net.Error, or after 10 consecutive errors. Only net.ErrClosed is a normal exit, and nothing restarts the goroutine until the Bind is reopened.
  • health.checkReceiveFuncsLocked detects this and sets a warnable but does not recover.
  • The send side already heals via maybeRebindOnError; there was no receive-side equivalent.

Change

For a read error other than the Conn or Bind closing: log it, rebind (throttled) and retry with a backoff of 50ms doubling to 1s, reset by a successful read. Closing the Conn or the Bind interrupts the wait. The 5s rebind throttle moves into maybeRebind, shared with the send path and made safe against concurrent callers with a compare-and-swap, since each receive func can now reach it.

Evidence level

The read error that occurs on iOS is not known; I could not capture logs at the moment of a stall. This is a defensive change to a path that currently has no recovery. A log line from a stall (Receive func ... exiting with error) would confirm or refute that this is the trigger. Happy to adjust, or to restrict the rebind to specific errors via shouldRebind if you prefer.

Testing

  • New TestReceiveFuncReadError: a fake socket that always fails; the receive func rebinds and keeps reading, and returns when the Bind closes.
  • go test -race -count=3 ./wgengine/magicsock/ ./health/ passes.
  • go vet is clean for linux, windows, darwin and js/wasm; plan9 builds. (go vet reports a pre-existing copylocks warning at endpoint_test.go:747, unrelated to this change.)

Questions for maintainers

  • Any read error, even a one-off, now triggers a full rebind and re-STUN, which resets peer paths, at most every 5s. Should it be limited to errors that shouldRebind recognises, or only after N consecutive errors?
  • A rebind racing with Bind close can reopen sockets; the send path has the same race today.

Updates #17967

🤖 Generated with Claude Code

mkReceiveFunc returned any non-truncated ReadBatch error to
wireguard-go. In RoutineReceiveIncoming a returned error ends the
receive goroutine for good if it is a non-temporary net.Error, or after
10 consecutive errors; only net.ErrClosed is a normal exit, and nothing
restarts the goroutine until the Bind is reopened. The tunnel then looks
connected but can no longer receive, which only toggling Tailscale
recovers from. The health check reports it as "ReceiveIPv4 is not
running" (magicsock-receive-func-error) but does not act on it.

The send path already heals with maybeRebindOnError. Do the same for
receives: for an error other than the Conn or Bind closing, log it,
rebind (throttled, now shared with the send path through maybeRebind,
which also makes the throttle safe against concurrent callers) and retry
the read with a backoff of 50ms doubling to 1s. Closing the Conn or the
Bind interrupts the wait.

The read error seen on iOS is not known; this is a defensive change.

Updates tailscale#17967

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: lucas persson <[email protected]>
@luben93

luben93 commented Nov 20, 2026

Copy link
Copy Markdown
Author

Superseded by the receive-error handling for #22097 (54c4153, 2c663f0, 254cb9a), released in 1.105.x. Thanks!

@luben93 luben93 closed this Nov 20, 2026
@IvoMajic

Copy link
Copy Markdown

@luben93
Hit this on macOS 27.2.1, Tailscale 1.104.5 (App Store, 1.104.5-t788384763-gd9218e278), Headscale control server, iPhone Personal Hotspot uplink. Same version as #22217. Captured the exiting error:

15:24:54.397 Rebind; defIf="en0", ips=[fe80::ce3:…/64]
15:24:54.400 Receive func ReceiveIPv4 exiting with error: *net.OpError, read udp4 0.0.0.0:41641: recvfrom: can't assign requested address
...
17:20:09.661 Rebind; defIf="en0", ips=[172.20.10.2/28 fe80::…]
17:20:21.134 health(warnable=magicsock-receive-func-error): error: The MagicSock function ReceiveIPv4 is not running.

Trigger: the hotspot briefly drops the interface's IPv4 → recvfrom returns EADDRNOTAVAIL → the receive goroutine exits. Later Rebinds after IPv4 returns don't restart it; only a VPN toggle does. Afterwards all peer traffic falls back to DERP. This confirms the receive-side rebind/retry approach in #22084.

@luben93

luben93 commented Nov 22, 2026

Copy link
Copy Markdown
Author

@luben93

Hit this on macOS 27.2.1, Tailscale 1.104.5 (App Store, 1.104.5-t788384763-gd9218e278), Headscale control server, iPhone Personal Hotspot uplink. Same version as #22217. Captured the exiting error:


15:24:54.397 Rebind; defIf="en0", ips=[fe80::ce3:…/64]

15:24:54.400 Receive func ReceiveIPv4 exiting with error: *net.OpError, read udp4 0.0.0.0:41641: recvfrom: can't assign requested address

...

17:20:09.661 Rebind; defIf="en0", ips=[172.20.10.2/28 fe80::…]

17:20:21.134 health(warnable=magicsock-receive-func-error): error: The MagicSock function ReceiveIPv4 is not running.

Trigger: the hotspot briefly drops the interface's IPv4 → recvfrom returns EADDRNOTAVAIL → the receive goroutine exits. Later Rebinds after IPv4 returns don't restart it; only a VPN toggle does. Afterwards all peer traffic falls back to DERP. This confirms the receive-side rebind/retry approach in #22084.

In not a maintainer i just noticed and posted a bug which seems to have been fixed in the testfligt build posted above

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.

2 participants