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

memory leak on openwrt

Open PiggyRan opened this issue 11 months ago • 6 comments

Operating system

OpenWRT

System version

OpenWrt 23.05-SNAPSHOT/Linux 5.15.148

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.8.9

Environment: go1.21.5 linux/amd64
Tags: with_gvisor,with_dhcp,with_wireguard,with_reality_server,with_clash_api,with_quic,with_utls,with_ech
Revision: 96858eec4a0d9baf5e0d2d16c44b8d080f790b87
CGO: disabled

Description

heap profile: 472: 27572280 [24975: 203014488] @ heap/1048576
1: 20054016 [1: 20054016] @ 0x41158d 0x41300f 0x411d73 0x49f929 0x49ef19 0x49eedd 0x9a5ea2 0x9a5ea3 0x9ba14a 0x470f61
#	0x49f928	context.(*cancelCtx).propagateCancel+0x148				context/context.go:483
#	0x49ef18	context.withCancel+0x58							context/context.go:273
#	0x49eedc	context.WithCancelCause+0x1c						context/context.go:264
#	0x9a5ea1	github.com/sagernet/sing/common/canceler.NewTimeoutPacketConn+0x181	github.com/sagernet/[email protected]/common/canceler/packet_timeout.go:22
#	0x9a5ea2	github.com/sagernet/sing/common/canceler.NewPacketConn+0x182		github.com/sagernet/[email protected]/common/canceler/packet.go:34
#	0x9ba149	github.com/sagernet/sing-tun.(*Mixed).Start.func1.1+0x229		github.com/sagernet/[email protected]/stack_mixed.go:71
# go tool pprof heap.out
File: sing-box
Type: inuse_space
Time: Mar 22, 2024 at 4:21am (PDT)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top10
Showing nodes accounting for 241.24MB, 88.45% of 272.76MB total
Dropped 139 nodes (cum <= 1.36MB)
Showing top 10 nodes out of 114
      flat  flat%   sum%        cum   cum%
  101.70MB 37.28% 37.28%   133.70MB 49.02%  context.(*cancelCtx).propagateCancel
   42.15MB 15.45% 52.74%    42.15MB 15.45%  github.com/sagernet/sing/common/buf.newDefaultAllocator.func9
   32.50MB 11.92% 64.65%   165.70MB 60.75%  context.withCancel
      32MB 11.73% 76.39%       32MB 11.73%  context.(*cancelCtx).Done
   14.96MB  5.48% 81.87%    14.96MB  5.48%  github.com/sagernet/sing/common/buf.newDefaultAllocator.func10
    4.91MB  1.80% 83.67%     4.91MB  1.80%  github.com/sagernet/sing-tun.(*TCPNat).Lookup
    4.02MB  1.47% 85.15%     4.02MB  1.47%  bytes.growSlice
    3.50MB  1.28% 86.43%     3.50MB  1.28%  runtime.malg
       3MB  1.10% 87.53%        3MB  1.10%  github.com/sagernet/gvisor/pkg/tcpip/adapters/gonet.(*deadlineTimer).init
    2.50MB  0.92% 88.45%        3MB  1.10%  github.com/sagernet/sing-box/experimental/clashapi/trafficontrol.NewTCPTracker

Reproduction

using "tun" with "mixed" stack like this:

  "inbounds": [
    {
      "domain_strategy": "ipv4_only",
      "endpoint_independent_nat": false,
      "interface_name": "singboxtun0",
      "inet4_address": "172.19.0.1/28",
      "sniff": true,
      "sniff_override_destination": false,
      "stack": "mixed",
      "auto_route": true,
      "strict_route": false,
      "tag": "tun-in",
      "type": "tun"
    }
  ],

Logs

No response

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.

PiggyRan avatar Mar 22 '24 11:03 PiggyRan

Try 1.9.0-rc.2

nekohasekai avatar Mar 24 '24 18:03 nekohasekai

I tried 1.9.0-rc2, about 20 hours, from begining

28648     1 root     S    1211m 61%   0% /usr/bin/sing-box run -c /etc/config/sing-box/config.json -D /etc/config/sing-box/

to now

28648     1 root     S    2029m 102%   0% /usr/bin/sing-box run -c /etc/config/sing-box/config.json -D /etc/config/sing-box/

it looks also have memory leak.

call graph (inuse_space) as below: image

ggprof heap is below: heap.zip

PiggyRan avatar Mar 26 '24 02:03 PiggyRan

Can you provide a pprof goroutine SVG?

nekohasekai avatar Mar 26 '24 10:03 nekohasekai

goroutine svg is here:

goroutine

goroutine data file is here: goroutine.zip

Can you provide a pprof goroutine SVG?

PiggyRan avatar Mar 26 '24 11:03 PiggyRan

This seems to be caused by the vision sub-protocol implementation of vless in sing-box, I suggest you use multiplex instead or switch to the Xray server.

nekohasekai avatar Mar 26 '24 12:03 nekohasekai

Could you please explain more about how to use multiplex to replace vision sub-protocol? like below? And how about the security of multiplex?

Thanks.

{
  "log": {
    "level": "info"
  },
  "inbounds": [
    {
      "type": "vless",
      "tag": "vless-in",
      "listen": "0.0.0.0",
      "listen_port": 10086,
      "users": [
        {
          "uuid": "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx",
          "flow": "" 
        }
      ],
      "tls": {
        "enabled": true,
        "server_name": "xxx.xxxx.com",
        "reality": {
          "enabled": true,
          "handshake": {
            "server": "xxx.xxxx.com",
            "server_port": 443
          },
          "private_key": "xxxxxxxxxxxxxx",
          "short_id": [
            "xxxxxxxx"
          ]
        }
      },
      "multiplex": { 
        "enabled": true,
        "padding": true,
        "brutal": {
          "enabled": true,
          "up_mbps": 300,
          "down_mbps": 300
        }
      }
    }
  ],
  "outbounds": [
    {
      "type": "direct",
      "tag": "direct"
    }
  ]
}

This seems to be caused by the vision sub-protocol implementation of vless in sing-box, I suggest you use multiplex instead or switch to the Xray server.

PiggyRan avatar Mar 27 '24 02:03 PiggyRan