Skip to content

magicsock: handle UDP read feedback - #21902

Merged
raggi merged 1 commit into
mainfrom
raggi/iOS-receive
Oct 30, 2026
Merged

raggi merged 1 commit into
mainfrom
raggi/iOS-receive

Conversation

@raggi

@raggi raggi commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Retry UDP reads returning EHOSTUNREACH or ENETUNREACH, including their
Winsock equivalents, without returning those errors to wireguard-go.
Count them in magicsock_recv_udp_error_reachability. Other read errors are unchanged.

Updates #20616
Updates #19504

Signed-off-by: James Tucker [email protected]

@raggi
raggi force-pushed the raggi/iOS-receive branch 2 times, most recently from 0603ce4 to 0837feb Compare October 23, 2026 15:56
@illotum
illotum self-requested a review October 23, 2026 23:36
@chris-reusch

Copy link
Copy Markdown

Thanks for picking this up. Adding a data point from iOS in case it helps with testing: iPhone 17, Tailscale iOS 1.102.3, Mullvad exit node, T-Mobile US cellular (IPv6-only with 464XLAT). It reproduces when switching between home Wi-Fi and cellular: the app stays Connected but stops passing traffic until the tunnel is toggled. Happy to test a TestFlight build with this change if that's useful.

@StirlingEngine

StirlingEngine commented Oct 24, 2026 •

Copy link
Copy Markdown

Is this related to that #21832? The behavior looks very similar.

@raggi

raggi commented Oct 24, 2026

Copy link
Copy Markdown
Member Author

Thanks for picking this up. Adding a data point from iOS in case it helps with testing: iPhone 17, Tailscale iOS 1.102.3, Mullvad exit node, T-Mobile US cellular (IPv6-only with 464XLAT). It reproduces when switching between home Wi-Fi and cellular: the app stays Connected but stops passing traffic until the tunnel is toggled. Happy to test a TestFlight build with this change if that's useful.

Please reproduce this and provide a bug report BUG- reference so that I can look at the logs. The hand summarized description is insufficient to reproduce/diagnose due to the number of parts involved, but for most occurrences of what I have been able to track down so far, the logs would provide exact data that I need to write a precise fix.

@pprotas

pprotas commented Oct 25, 2026

Copy link
Copy Markdown

My iOS reproduction and bug-report ID are in #19504 (comment). The report was generated while the tunnel was still broken, before restarting Tailscale.

@jwhited

jwhited commented Oct 26, 2026

Copy link
Copy Markdown
Member

Have we considered a simpler change in wireguard-go wrt how conn.ReceiveFunc errors are handled? e.g. spin in wireguard-go until net.ErrClosed, get rid of the max spin count and net.Temporary() condition (everything is temporary besides net.ErrClosed).

@raggi

raggi commented Oct 26, 2026

Copy link
Copy Markdown
Member Author

Have we considered a simpler change in wireguard-go wrt how conn.ReceiveFunc errors are handled? e.g. spin in wireguard-go until net.ErrClosed, get rid of the max spin count and net.Temporary() condition (everything is temporary besides net.ErrClosed).

spin how hard? how do we want to pass metrics back over? those kinds of questions make it easier to leave on this side, eventually i want to remove the i/o pump from that side because of related issues.

@jwhited

jwhited commented Oct 26, 2026

Copy link
Copy Markdown
Member

Have we considered a simpler change in wireguard-go wrt how conn.ReceiveFunc errors are handled? e.g. spin in wireguard-go until net.ErrClosed, get rid of the max spin count and net.Temporary() condition (everything is temporary besides net.ErrClosed).

spin how hard? how do we want to pass metrics back over? those kinds of questions make it easier to leave on this side, eventually i want to remove the i/o pump from that side because of related issues.

No limit, remove the sleep. I will admit it just moves the need for spin protection into magicsock. I'm hoping we can remove the requirement of maintaining an exhaustive list of "temporary" / re-bindable errors that we have to keep learning new behaviors around, vs just treating everything other than close as temporary.

@raggi

raggi commented Oct 26, 2026

Copy link
Copy Markdown
Member Author

Have we considered a simpler change in wireguard-go wrt how conn.ReceiveFunc errors are handled? e.g. spin in wireguard-go until net.ErrClosed, get rid of the max spin count and net.Temporary() condition (everything is temporary besides net.ErrClosed).

spin how hard? how do we want to pass metrics back over? those kinds of questions make it easier to leave on this side, eventually i want to remove the i/o pump from that side because of related issues.

No limit, remove the sleep. I will admit it just moves the need for spin protection into magicsock. I'm hoping we can remove the requirement of maintaining an exhaustive list of "temporary" / re-bindable errors that we have to keep learning new behaviors around, vs just treating everything other than close as temporary.

i'll have a closer look and see. i'm wary of widely classifying things as temporary, lots of stuff like ebadf are definitely fatal. will update once i've had a look

@illotum

illotum commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

We do need to guard on EBADF and ENOTSOCK, but the set is small.

FWIW we just established a precedent for metrics: tailscale/wireguard-go@383e864

@raggi

raggi commented Oct 27, 2026

Copy link
Copy Markdown
Member Author

We do need to guard on EBADF and ENOTSOCK, but the set is small.

It's important to note here that the set isn't as small or stable as we might like. There are covered cases already where EDR software has arbitrarily altered the errno meaning and set of errnos that may be returned from socket syscalls.

I wish I could create a safe subdivide based on a stable kernel ABI here, and if the kernel defined it's own ABI we could, but alas, EDR's have taken this over and broken those rules, lacking Linus' policing.

@walterbarrera1234

Copy link
Copy Markdown

@raggi iOS reproduction, with a bug report generated while the tunnel was broken (before toggling):

BUG-e5983d33fc21b4d30fb7693a2e732e3bb3bcbc3a3a40bba273a51d27547a63f0-20261027230726Z-97e2aa591ca22580

  • iOS 26.5, Tailscale 1.104.2. T-Mobile US: the phone is on a T-Mobile mobile hotspot (Wi-Fi) plus cellular (IPv6 with 464XLAT).
  • Reliable trigger: the T3 Code iOS app ([Bug]: iOS app usage is followed by Tailscale ReceiveIPv4 failure; Safari stays stable pingdotgg/t3code#25405) with saved environments pointing at tailnet machines that are powered off. With those enabled, the phone shows "MagicSock function not running" (ReceiveIPv4) within a few minutes while the VPN stays Connected. With the offline environments disabled it stayed stable for ~35 min; re-enabling them reproduced it within ~6 min.
  • From a Windows peer (1.102.4) during the failure (22:52–23:07 UTC): the phone kept sending direct disco pings to us over IPv6 every ~10–20 s (we received them and replied), and sent WireGuard handshake initiations that we answered, followed by ConsumeMessageInitiation: handshake replay, i.e. the phone retransmitted the same initiation because it never received our response. No data after that; the session expired. So the phone's send path kept working and only its receive path was dead, which matches the receive-func exit this PR addresses.
  • Toggling the VPN recovers it until the next failure.

@quanganhdo

Copy link
Copy Markdown

Codex helped me prepare this report:


Additional reproduction on Tailscale 1.104.2 for iOS.

I use Rex by Superlogical, a terminal multiplexer and remote terminal app with an iOS client:
https://www.superlogical.com/rex

Rex connects to remote servers over Tailscale. The macOS and Linux servers inspected during this incident use Rex’s built-in Tailscale integration, so each Rex server joins the tailnet as its own device.

Environment:

  • Client: iPhone running Tailscale 1.104.2.
  • Application: Rex Dev 0.1.0, build 1542.
  • Remote servers: macOS and Linux.
  • The problem occurs on both Wi-Fi and cellular.
  • I notice it after returning to Rex from another app or unlocking the phone.

The Tailscale app displays:

MagicSock Function Not Running
The MagicSock function ReceiveIPv4 is not running.
Code: magicsock-receive-func-error
Magicsock Function Name: ReceiveIPv4

During the captured incident:

  • Rex initially connected to both servers and successfully completed its bootstrap requests.
  • Both servers subsequently recorded the iPhone’s control connections resetting at the same time.
  • Rex then repeatedly reported: “The Rex control connection timed out before its handshake finished.”
  • Neither server logged a new iPhone control connection during the later handshake retries.
  • The iPhone stopped answering Tailscale packet probes from independent macOS and Linux peers. TSMP probes from macOS also failed.
  • The iPhone still appeared online in the tailnet.
  • The remote Rex servers remained running, and another client could still connect to the Linux Rex server over Tailscale.
  • Both Pi-hole DNS resolver peers remained reachable from the Linux peer. The failed probes to the iPhone used its Tailscale IP directly, without depending on DNS.

The ReceiveIPv4 warning was captured in a screenshot during the failure. Toggling Tailscale off and on has restored connectivity in previous occurrences, but the problem keeps returning. Recovery after this particular diagnostic capture had not yet been verified when I generated the report.

The captured incident occurred around October 27, 2026, 23:31–23:45 EDT, corresponding to October 28, 03:31–03:45 UTC.

Bug report generated on the affected iPhone at 2026-10-28 03:50:18 UTC:

BUG-f3167915d0d43993a7af616efc6716dc58ebfebdc63fd503ceac3d389ab2da00-20261028035018Z-57fe0cd53828a41d

Rex is the application in which I notice the failure, but the simultaneous failure of independent Tailscale packet probes and the ReceiveIPv4 warning show that the observed failure extends beyond Rex’s application protocol.

This appears consistent with the ReceiveIPv4 failure discussed in this issue. I have not established the underlying trigger. Please let me know whether additional diagnostics or a test build would help.

@Dinth

Dinth commented Oct 28, 2026

Copy link
Copy Markdown

Another iOS reproduction with a bug report captured while broken (before toggling):

BUG-8c969baa5f0f94470edab1bc8801dceabb2819885431a48d9e4a7d6b53481866-20261028133323Z-28e40e95f0c103de

iPhone 12 mini, iOS 27.2, Tailscale 1.104.2, MDM-deployed with VPN On Demand, using a FreeBSD exit node (1.98.5), with a single global nameserver reached via the subnet route. Happens several times a day on a corporate Wi-Fi (mapvarydest=true, netcheck udp=false) and when roaming to UK cellular.

Earlier incident the same day (2026-10-28, UTC), from the client log: 07:59:05 LinkChange: minor, 07:59:28 WireGuard handshake with the exit node completes, 07:59:38 DNS queries time out (the DNS server never received them, and the exit node logged no forwarded traffic), 07:59:53 dns-forward-failing, 08:00:20 magicsock-receive-func-error. Traffic only resumed after the extension restarted at 08:03:34 (new disco key seen by the peer). The bug report above is from a second occurrence at ~13:28–13:33 after roaming Wi-Fi → cellular.

@JayWStapleton

JayWStapleton commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Note to self: TSS-99056

@sfllaw

sfllaw commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Also related: TSS-106924

@raggi
raggi force-pushed the raggi/iOS-receive branch 2 times, most recently from 70489d6 to c43d3ec Compare October 30, 2026 22:29
@raggi raggi changed the title magicsock: keep receive funcs alive across benign read aborts magicsock: handle and pace UDP receive errors Oct 30, 2026
@raggi
raggi force-pushed the raggi/iOS-receive branch from c43d3ec to e3ce00f Compare October 30, 2026 22:33
@ViRb3

ViRb3 commented Oct 30, 2026 •

Copy link
Copy Markdown

Apologies if already seen, but linking here in case it was missed: I started hitting this issue consistently since updating to macOS 27.2 and it completely breaks P2P in Tailscale. Seems to be due to a conflict/bug with content filtering extensions like Little Snitch: #22082 (comment)

I raised a support ticket with the same report as well.

@raggi
raggi force-pushed the raggi/iOS-receive branch 2 times, most recently from 04ce2f1 to ef55a9c Compare October 30, 2026 22:59
@raggi raggi changed the title magicsock: handle and pace UDP receive errors magicsock: handle UDP read feedback Oct 30, 2026
Comment thread net/neterror/neterror_posix.go Resolved
Comment thread wgengine/magicsock/magicsock.go Resolved

@illotum illotum left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM aside of one subjective nit.

@jwhited jwhited left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM once Windows bits are moved from neterror_posix.go into neterror_windows.go, including the other pre-existing func. We can use x/sys/windows constants where available.

@raggi
raggi force-pushed the raggi/iOS-receive branch from ef55a9c to ae6f5b1 Compare October 30, 2026 23:14
Retry UDP reads returning EHOSTUNREACH or ENETUNREACH, including their
Winsock equivalents, without returning those errors to wireguard-go.
Count them in magicsock_recv_udp_error_reachability. Other read errors are unchanged.

Updates #20616
Updates #19504

Signed-off-by: James Tucker <[email protected]>
@raggi
raggi force-pushed the raggi/iOS-receive branch from ae6f5b1 to b660c81 Compare October 30, 2026 23:17
@raggi
raggi merged commit e9b151b into main Oct 30, 2026
62 checks passed
@raggi
raggi deleted the raggi/iOS-receive branch October 30, 2026 23:31
nbeirne added a commit that referenced this pull request Nov 5, 2026
This iterates on the work in #21902, #22100 and tailscale/wireguard-go#116.

The new version of wireguard-go discards the deathSpiral loop in favour
of allowing Bind's ReceiveFunc to pace read retries. This code is the
other side. Add back a backoff for non-feedback errors, and report the
receive func as broken to the health tracker once reads have been
failing for 30s. The loop only exits once net.ErrClosed is returned.

Updates #22097

Change-Id: I9f1134242c3cb5ac2ef3c282cf7bcd7606cb5b9e
Signed-off-by: Nick Beirne <[email protected]>

.

Change-Id: Iccfb5e3746d212856978e9f539f848fccf85058a

.
nbeirne added a commit that referenced this pull request Nov 5, 2026
This iterates on the work in #21902, #22100 and tailscale/wireguard-go#116.

The new version of wireguard-go discards the deathSpiral loop in favour
of allowing Bind's ReceiveFunc to pace read retries. This code is the
other side. Add back a backoff for non-feedback errors, and report the
receive func as broken to the health tracker once reads have been
failing for 30s. The loop only exits once net.ErrClosed is returned.

Updates #22097

Change-Id: I9f1134242c3cb5ac2ef3c282cf7bcd7606cb5b9e
Signed-off-by: Nick Beirne <[email protected]>

.

Change-Id: Iccfb5e3746d212856978e9f539f848fccf85058a

.
nbeirne added a commit that referenced this pull request Nov 5, 2026
This iterates on the work in #21902, #22100 and tailscale/wireguard-go#116.

The new version of wireguard-go discards the deathSpiral loop in favour
of allowing Bind's ReceiveFunc to pace read retries. This code is the
other side. Add back a backoff for non-feedback errors, and report the
receive func as broken to the health tracker once reads have been
failing for 30s. The loop only exits once net.ErrClosed is returned.

Updates #22097

Change-Id: I9f1134242c3cb5ac2ef3c282cf7bcd7606cb5b9e
Signed-off-by: Nick Beirne <[email protected]>
nbeirne added a commit that referenced this pull request Nov 6, 2026
This iterates on the work in #21902, #22100 and tailscale/wireguard-go#116.

The new version of wireguard-go discards the deathSpiral loop in favour
of allowing Bind's ReceiveFunc to pace read retries. This code is the
other side. Add back a backoff for non-feedback errors, and report the
receive func as broken to the health tracker once reads have been
failing for 30s. The loop only exits once net.ErrClosed is returned.

Updates #22097

Change-Id: I9f1134242c3cb5ac2ef3c282cf7bcd7606cb5b9e
Signed-off-by: Nick Beirne <[email protected]>
nbeirne added a commit that referenced this pull request Nov 6, 2026
This iterates on the work in #21902, #22100 and tailscale/wireguard-go#116.

wireguard-go no longer has a deathSpiral loop; it relies on the Bind's
ReceiveFunc to pace read retries. This is the magicsock side of that
change.

On read errors other than truncation and UDP read feedback, the receive
func now sleeps 100ms and retries rather than returning. If reads keep
failing for 30s it marks itself broken via ReceiveFuncStats.SetBroken.
The next successful read clears that state.

Updates #22097

Change-Id: I9f1134242c3cb5ac2ef3c282cf7bcd7606cb5b9e
Signed-off-by: Nick Beirne <[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.