Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用tcp参数 创建的连接未关闭 #965

Open
chenfeixyz opened this issue Aug 17, 2024 · 17 comments
Open

使用tcp参数 创建的连接未关闭 #965

chenfeixyz opened this issue Aug 17, 2024 · 17 comments

Comments

@chenfeixyz
Copy link

chenfeixyz commented Aug 17, 2024

if config.TCP {
conn, err := tcpraw.Dial("tcp", remoteAddr)
if err != nil {
return nil, errors.Wrap(err, "tcpraw.Dial()")
}
return kcp.NewConn(remoteAddr, block, config.DataShard, config.ParityShard, conn)
}
kcp-go ownConn 属性是false 未看到关闭的地方 查看进程fd数量一直在增加

@xtaci
Copy link
Owner

xtaci commented Aug 17, 2024

在std/copy.go中,会处理关闭。

@xtaci
Copy link
Owner

xtaci commented Aug 17, 2024

另外tcp的实现并不是很完美,可以用udp_to_raw

@chenfeixyz
Copy link
Author

在std/copy.go中,会处理关闭。

conn, err := tcpraw.Dial("tcp", remoteAddr) 是这个conn没有释放
kcp.NewConn 创建的UDPSession ownConn值是false Close的时候不会调用conn的Close

kcp-go sess.go Close函数
if s.l != nil { // belongs to listener
s.l.closeSession(s.remote)
return nil
} else if s.ownConn { // client socket close
return s.conn.Close()
} else {
return nil
}

@xtaci
Copy link
Owner

xtaci commented Aug 17, 2024

即使这里没关闭,那么garbage collection也会close掉这个链接,只是不够实时而已,不会出现fd不断增加的情况。

@xtaci
Copy link
Owner

xtaci commented Aug 17, 2024

可能问题出在tcpraw,但需要分析一下

@chenfeixyz
Copy link
Author

xtaci/tcpraw@7b3a1df @chenfeixyz 试试这个update,看是否重现, go get -u github.com/xtaci/tcpraw@7b3a1df734aa77772ebe655c067aea6f6c044a58

还是不行 tcpraw.Dial 创建的conn内有协程在引用自己 不会被GC标记 可以在kcp-go UDPSession里加个SetOwnConn函数吗

@xtaci
Copy link
Owner

xtaci commented Aug 18, 2024 via email

@xtaci
Copy link
Owner

xtaci commented Aug 18, 2024

image

这个是肯定会Close掉socket的

@xtaci
Copy link
Owner

xtaci commented Aug 18, 2024

tagged to : https://github.com/xtaci/tcpraw/releases/tag/v1.2.26

我在休假,估计要下周才能具体的debug,不过思路肯定是这个,让他GC就可以了。

kcptun的deps也更新到了v1.2.26

@chenfeixyz
Copy link
Author

现在是只要TCPconn不被引用,自然会gc 然后关闭掉socket 。注意我的setfinalizer改动 E-mail: @.*** Blog: https://github.com/xtaci

On Sun, Aug 18, 2024 at 13:31 chenfeixyz @.> wrote: @. <xtaci/tcpraw@7b3a1df> @chenfeixyz https://github.com/chenfeixyz 试试这个update,看是否重现, go get -u @.*** 还是不行 tcpraw.Dial 创建的conn内有协程在引用自己 不会被GC标记 可以在kcp-go UDPSession里加个SetOwnConn函数吗 — Reply to this email directly, view it on GitHub <#965 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAR45ZJKGZWYO5CSINH3QBDZSAWSJAVCNFSM6AAAAABMVMGBQ6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEOJVGEZDAMZSHA . You are receiving this because you commented.Message ID: @.***>

func (conn *tcpConn) cleaner() 这个函数是单独协程运行的 一直在引用tcpConn本身

@xtaci
Copy link
Owner

xtaci commented Aug 18, 2024

现在是只要TCPconn不被引用,自然会gc 然后关闭掉socket 。注意我的setfinalizer改动 E-mail: @.*** Blog: https://github.com/xtaci

On Sun, Aug 18, 2024 at 13:31 chenfeixyz @.> wrote: _@**._ <xtaci/tcpraw@7b3a1df> @chenfeixyz https://github.com/chenfeixyz 试试这个update,看是否重现, go get -u _@_.* 还是不行 tcpraw.Dial 创建的conn内有协程在引用自己 不会被GC标记 可以在kcp-go UDPSession里加个SetOwnConn函数吗 — Reply to this email directly, view it on GitHub <#965 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAR45ZJKGZWYO5CSINH3QBDZSAWSJAVCNFSM6AAAAABMVMGBQ6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEOJVGEZDAMZSHA . You are receiving this because you commented.Message ID: @.***>

func (conn *tcpConn) cleaner() 这个函数是单独协程运行的 一直在引用tcpConn本身

这个没有关系,内部各种交叉引用tcpConn都可以,但只要TCPConn没有被外部引用了,会触发关闭操作。

@xtaci
Copy link
Owner

xtaci commented Aug 18, 2024

kcptun的tcpraw.Dial后,返回的是*TCPConn,这个一旦没有被kcptun引用了,会触发SetFinalizer中的wrapper.Close,进而关闭socket fd

@xtaci
Copy link
Owner

xtaci commented Aug 19, 2024

commit d5a4974
@chenfeixyz 试下

主动关闭和被动关闭都具有了。

@chenfeixyz
Copy link
Author

commit d5a4974 @chenfeixyz 试下

主动关闭和被动关闭都具有了。

fd数量现在正常了

tcp_linux.go 文件 cleaner函数 time.NewTicker 没有加Stop

@xtaci
Copy link
Owner

xtaci commented Aug 19, 2024

@xtaci
Copy link
Owner

xtaci commented Aug 20, 2024

ca79329
还有这个commit,之前因为有udp2raw,没有花多少心思在tcp模拟

@xtaci
Copy link
Owner

xtaci commented Aug 20, 2024

还包含了iptables的重置

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

No branches or pull requests

2 participants