xjasonlyu / tun2socks

tun2socks - powered by gVisor TCP/IP stack
https://github.com/xjasonlyu/tun2socks/wiki
GNU General Public License v3.0
3.36k stars 458 forks source link

[Bug] panics with shadowsocks and lots of connections #409

Open ameir opened 1 month ago

ameir commented 1 month ago

Verify steps

Version

main as of 20240924

What OS are you seeing the problem on?

Linux

Description

I was comparing this to gost by running it in shadowsocks mode and running an Ubuntu torrent download to monitor CPU usage and speeds. Shortly after starting, tun2socks panicked.

CLI or Config

tun2socks --device tun://tun1 -proxy ss://chacha20-ietf-poly1305:xxxx@xxxx --loglevel warn

Logs

goroutine 9265 gp=0x7880d88 m=nil [IO wait]:
runtime.gopark(0x5ae1f0, 0x40153148, 0x2, 0x2, 0x5)
    runtime/proc.go:424 +0x104 fp=0x7473d0c sp=0x7473cf8 pc=0x8be44
runtime.netpollblock(0x40153138, 0x72, 0x0)
    runtime/netpoll.go:575 +0x100 fp=0x7473d24 sp=0x7473d0c pc=0x46d60
internal/poll.runtime_pollWait(0x40153138, 0x72)
    runtime/netpoll.go:351 +0x54 fp=0x7473d38 sp=0x7473d24 pc=0x8afdc
internal/poll.(*pollDesc).wait(0x77d07e8, 0x72, 0x0)
    internal/poll/fd_poll_runtime.go:84 +0x30 fp=0x7473d4c sp=0x7473d38 pc=0xf5f00
internal/poll.(*pollDesc).waitRead(...)
    internal/poll/fd_poll_runtime.go:89
internal/poll.(*FD).Read(0x77d07d0, {0x3d821c0, 0x20, 0x20})
    internal/poll/fd_unix.go:165 +0x22c fp=0x7473d94 sp=0x7473d4c pc=0xf6ce4
net.(*netFD).Read(0x77d07d0, {0x3d821c0, 0x20, 0x20})
    net/fd_posix.go:55 +0x38 fp=0x7473dc0 sp=0x7473d94 pc=0x1a0330
net.(*conn).Read(0x7892c40, {0x3d821c0, 0x20, 0x20})
    net/net.go:189 +0x48 fp=0x7473dec sp=0x7473dc0 pc=0x1ac394
net.(*TCPConn).Read(0x7892c40, {0x3d821c0, 0x20, 0x20})
    <autogenerated>:1 +0x44 fp=0x7473e0c sp=0x7473dec pc=0x1bbc70
io.ReadAtLeast({0x6422c8, 0x7892c40}, {0x3d821c0, 0x20, 0x20}, 0x20)
    io/io.go:335 +0x90 fp=0x7473e38 sp=0x7473e0c pc=0xcbc74
io.ReadFull(...)
    io/io.go:354
github.com/xjasonlyu/tun2socks/v2/transport/shadowsocks/shadowaead.(*Conn).initReader(0x78e2570)
    github.com/xjasonlyu/tun2socks/v2/transport/shadowsocks/shadowaead/stream.go:221 +0xa0 fp=0x7473e7c sp=0x7473e38 pc=0x449a80
github.com/xjasonlyu/tun2socks/v2/transport/shadowsocks/shadowaead.(*Conn).Read(0x78e2570, {0x78ec000, 0x5000, 0x8000})
    github.com/xjasonlyu/tun2socks/v2/transport/shadowsocks/shadowaead/stream.go:236 +0x28 fp=0x7473e9c sp=0x7473e7c pc=0x449bf8
github.com/xjasonlyu/tun2socks/v2/tunnel/statistic.(*tcpTracker).Read(0x5ee7700, {0x78ec000, 0x5000, 0x8000})
    github.com/xjasonlyu/tun2socks/v2/tunnel/statistic/tracker.go:58 +0x40 fp=0x7473ecc sp=0x7473e9c pc=0x479940
io.copyBuffer({0x404c36f8, 0x4121c00}, {0x404c3708, 0x5ee7700}, {0x78ec000, 0x5000, 0x8000})
    io/io.go:429 +0x218 fp=0x7473f18 sp=0x7473ecc pc=0xcc1d0
io.CopyBuffer({0x404c36f8, 0x4121c00}, {0x404c3708, 0x5ee7700}, {0x78ec000, 0x5000, 0x8000})
    io/io.go:402 +0x64 fp=0x7473f48 sp=0x7473f18 pc=0xcbf64
github.com/xjasonlyu/tun2socks/v2/tunnel.unidirectionalStream({0x404c36c8, 0x4121c00}, {0x648124, 0x5ee7700}, {0x588b18, 0xe}, 0x3d0a4b0)
    github.com/xjasonlyu/tun2socks/v2/tunnel/tcp.go:60 +0x11c fp=0x7473fcc sp=0x7473f48 pc=0x48240c
github.com/xjasonlyu/tun2socks/v2/tunnel.pipe.gowrap2()
    github.com/xjasonlyu/tun2socks/v2/tunnel/tcp.go:52 +0x58 fp=0x7473fec sp=0x7473fcc pc=0x482230
runtime.goexit({})
    runtime/asm_arm.s:884 +0x4 fp=0x7473fec sp=0x7473fec pc=0x93390
created by github.com/xjasonlyu/tun2socks/v2/tunnel.pipe in goroutine 9238
    github.com/xjasonlyu/tun2socks/v2/tunnel/tcp.go:52 +0x124


### How to Reproduce

Run in shadowsocks mode.
Create hundreds/thousands of connections, e.g. by downloading popular torrents.
xjasonlyu commented 1 month ago

That feels very odd to me because it doesn't look like it is caused directly by the tun2socks code.

ameir commented 1 month ago

When not stressed, everything works great. I should mention that this is running on a router, and resources are more constrained.