sing-box icon indicating copy to clipboard operation
sing-box copied to clipboard

[Bug] macOS 睡眠唤醒 / 网络切换后 WireGuard endpoint 握手永久卡死(sendmsg: socket is already connected),只能重启恢复 — sing-box 1.14.0

Open ngolin opened this issue 2 months ago • 1 comments

Operating system

macOS

System version

macOS 26.5 (Build 25F71),Apple Silicon(arm64,Mac17,4)

Installation type

Original sing-box Command Line

If you are using a graphical client, please provide the version of the client.

No response

Version

sing-box version 1.14.0

Environment: go1.26.7 darwin/arm64
Tags: with_gvisor,with_quic,with_dhcp,with_wireguard,with_utls,with_acme,with_clash_api,with_tailscale,with_ccm,with_ocm,with_cloudflared,with_naive_outbound,with_usbip,with_openvpn,with_openconnect,badlinkname,tfogo_checklinkname0
CGO: enabled

Description

macOS 上使用新版 endpoints 配置 WireGuard 时,在系统睡眠唤醒或网络切换(断开/重连 WiFi)后隧道无法自动恢复,WireGuard UDP socket 进入"已连接但失效"的卡死状态:

  1. 断网/睡眠瞬间日志出现 sendmsg: can't assign requested address 与 network is unreachable;
  2. 唤醒/恢复联网后,握手持续失败:每 ~5 秒一条 failed to send handshake initiation: ... sendmsg: socket is already connected(EISCONN),永不停歇;
  3. persistent_keepalive_interval 已配置(25s),但 keepalive 与握手走同一个失效 socket,无法触发重连;
  4. 所有走该隧道的新连接挂起 2m7s 后 operation timed out;
  5. 只有重启 sing-box 进程才能恢复。

已查阅相关 issue:#2745、#3402,该问题在当前版本仍可稳定复现。

Reproduction

前置条件:一台自建 WireGuard 服务端(任意地址,公网/局域网均可),macOS 上以 root 运行 sing-box 原始命令行程序。

最简配置 config.json(与线上配置结构一致,密钥已替换为占位符):

{
  "log": {
    "level": "warn",
    "timestamp": true,
    "output": "sing-box.log"
  },
  "inbounds": [
    {
      "type": "tun",
      "tag": "tun-in",
      "mtu": 1420,
      "stack": "system",
      "auto_route": true,
      "strict_route": true,
      "address": ["172.18.0.1/30"]
    }
  ],
  "endpoints": [
    {
      "type": "wireguard",
      "tag": "proxy",
      "mtu": 1420,
      "address": ["10.0.0.2/32"],
      "private_key": "PLACEHOLDER_LOCAL_PRIVATE_KEY",
      "peers": [
        {
          "address": "PLACEHOLDER_SERVER_IP",
          "port": 443,
          "public_key": "PLACEHOLDER_SERVER_PUBLIC_KEY",
          "allowed_ips": ["0.0.0.0/0"],
          "persistent_keepalive_interval": 25
        }
      ]
    }
  ],
  "route": {
    "final": "proxy",
    "rules": [
      { "ip_cidr": ["PLACEHOLDER_SERVER_IP/32"], "outbound": "direct" }
    ]
  }
}

重现步骤:

  1. sudo sing-box run -c config.json(需 root 权限创建 TUN);
  2. 确认隧道正常:curl https://1.1.1.1 成功;
  3. 让系统睡眠(合盖或手动睡眠),或直接断开 WiFi / 切换网络;
  4. 唤醒 / 恢复网络后观察:日志出现上述握手错误,隧道无法恢复,重启进程前一直持续。

期望行为:网络变化后 sing-box 应检测到接口/路由变更并重建 WireGuard UDP socket,自动重新完成握手(keepalive 已开启)。

实际行为:socket 保持"已连接"失效状态,握手请求每 5 秒失败一次并无限重试,进程存活期间永不恢复,只能手动重启。

Logs

# 17:25:57 断网/睡眠瞬间(EADDRNOTAVAIL + ENETUNREACH,本地路由消失)
+0800 2026-09-08 17:25:57 ERROR endpoint/wireguard[proxy]: peer(PLACEHOLDER…XXXX) - failed to send data packets: write udp4 0.0.0.0:52658->SERVER_IP:443: sendmsg: can't assign requested address
+0800 2026-09-08 17:25:57 ERROR router: process DNS packet: dial UDP connection: dial udp 119.29.29.29:53: connect: network is unreachable

# 17:26:09 唤醒后开始,每 ~5s 一条,直到进程重启前从未停止(本次会话共 417 条,跨 17:26:09 ~ 17:50:29)
+0800 2026-09-08 17:26:09 ERROR endpoint/wireguard[proxy]: peer(PLACEHOLDER…XXXX) - failed to send handshake initiation: write udp4 0.0.0.0:52658->SERVER_IP:443: sendmsg: socket is already connected
+0800 2026-09-08 17:26:14 ERROR endpoint/wireguard[proxy]: peer(PLACEHOLDER…XXXX) - failed to send handshake initiation: write udp4 0.0.0.0:52658->SERVER_IP:443: sendmsg: socket is already connected
+0800 2026-09-08 17:26:19 ERROR endpoint/wireguard[proxy]: peer(PLACEHOLDER…XXXX) - failed to send handshake initiation: write udp4 0.0.0.0:52658->SERVER_IP:443: sendmsg: socket is already connected
# …(417 条同构错误省略,重复至 17:50:29,进程存活,无任何恢复迹象)

# 期间所有经隧道的连接均挂起 2m7s 后超时
+0800 2026-09-08 17:37:42 ERROR connection: open connection to 17.248.224.13:443 using outbound/wireguard[proxy]: connect tcp 17.248.224.13:443: operation timed out


> 说明:以上为代表性片段。完整日志中握手错误在 417 条后仍继续(日志级别 warn 开启 timestamp),模式完全相同,故不逐一粘贴。

Supporter

Integrity requirements

  • [x] I confirm that I have read the documentation, understand the meaning of all the configuration items I wrote, and did not pile up seemingly useful options or default values.
  • [x] I confirm that I have provided the server and client configuration files and process that can be reproduced locally, instead of a complicated client configuration file that has been stripped of sensitive data.
  • [x] I confirm that I have provided the simplest configuration that can be used to reproduce the error I reported, instead of depending on remote servers, TUN, graphical interface clients, or other closed-source software.
  • [x] I confirm that I have provided the complete configuration files and logs, rather than just providing parts I think are useful out of confidence in my own intelligence.

ngolin avatar Sep 08 '26 09:09 ngolin

This appears to be fixed in current testing, and the fix matches the reported failure mode very closely.

The relevant sing-box commit is:

89ca622e99c5618b39adb638477b593c9e8cf708 Fix WireGuard endpoint stuck after network change on macOS

It updates:

github.com/sagernet/wireguard-go v0.0.5
→
github.com/sagernet/wireguard-go v0.0.6

wireguard-go v0.0.6 points to:

abd9348cd717a65b3cacbe136db71bd0aa62fdec conn: Reopen the bind when a connected socket stops working on darwin

The underlying failure looks exactly like the sequence in this report.

On Darwin, the single-peer WireGuard optimization can use a connected UDP socket. After sleep/network loss, that socket may remain logically connected while the underlying path/source address is no longer usable.

The new wireguard-go code treats these errors as requiring a new bind:

EADDRNOTAVAIL
ENETUNREACH
EHOSTUNREACH
ENETDOWN
EHOSTDOWN

and introduces:

ErrRebindRequired

When a send/receive path returns that error, wireguard-go schedules:

device.BindUpdate()

BindUpdate() closes the existing bind, opens fresh UDP sockets and clears cached peer source addresses.

That changes the recovery model from:

network disappears
→ connected UDP socket becomes stale
→ handshake retries keep using stale socket
→ EISCONN forever

to:

network disappears
→ stale connected socket produces path error
→ ErrRebindRequired
→ reopen WireGuard bind
→ fresh socket/source route
→ handshake can recover

So this looks like a direct fix for the reported sendmsg: socket is already connected loop rather than a generic network-change workaround.

The current published releases still predate this fix, so testing 1.14.0 or the existing 1.15.0-alpha.2 would not verify it.

A useful confirmation would be to retest with a build containing sing-box 89ca622e / wireguard-go v0.0.6 using both:

  1. sleep → wake;
  2. Wi-Fi disconnect/switch → reconnect.

The expected result is that the process reopens the bind and WireGuard resumes handshaking without restarting sing-box.

If that succeeds, I think #4511 can be closed as fixed by 89ca622e.

HeisLuka avatar Sep 09 '26 20:09 HeisLuka