cch123/golang-notes

io.md里benchmark代码存在泄露

katsusan opened this issue · 3 comments

➜  splicetest go test -benchmem -run=^$ -bench="BenchmarkNormalReadWrite|BenchmarkGoSplice" splice_test.go
goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i3 CPU       M 330  @ 2.13GHz
BenchmarkNormalReadWrite-4           705           1820369 ns/op         1079975 B/op        236 allocs/op
--- BENCH: BenchmarkNormalReadWrite-4
    splice_test.go:178: test:BenchmarkNormalReadWrite,total: 1,rate: 100.00%
    splice_test.go:178: test:BenchmarkNormalReadWrite,total: 100,rate: 57.00%
    splice_test.go:178: test:BenchmarkNormalReadWrite,total: 705,rate: 53.00%
BenchmarkGoSplice-4             2021/05/02 03:51:45 http: Accept error: accept tcp [::]:9610: accept4: too many open files; retrying in 5ms
2021/05/02 03:51:45 http: Accept error: accept tcp [::]:9610: accept4: too many open files; retrying in 5ms
2021/05/02 03:51:45 http: Accept error: accept tcp [::]:9610: accept4: too many open files; retrying in 5ms
...

➜  ~ lsof -p 31086
...
main.test 31086 root  988u     IPv4 679284      0t0     TCP localhost:9612->localhost:50176 (CLOSE_WAIT)
main.test 31086 root  989u     IPv4 676803      0t0     TCP localhost:9612->localhost:50214 (CLOSE_WAIT)
main.test 31086 root  992u     IPv4 678352      0t0     TCP localhost:9612->localhost:50192 (CLOSE_WAIT)
main.test 31086 root  998u     IPv4 679298      0t0     TCP localhost:9612->localhost:50198 (CLOSE_WAIT)
main.test 31086 root  999u     IPv4 679327      0t0     TCP localhost:9612->localhost:50228 (CLOSE_WAIT)
main.test 31086 root 1001u     IPv4 679315      0t0     TCP localhost:9612->localhost:50208 (CLOSE_WAIT)
main.test 31086 root 1006u     IPv4 677623      0t0     TCP localhost:9612->localhost:50222 (CLOSE_WAIT)
main.test 31086 root 1013u     IPv4 679331      0t0     TCP localhost:9612->localhost:50230 (CLOSE_WAIT)
main.test 31086 root 1015u     sock    0,9      0t0  680028 protocol: TCP
main.test 31086 root 1017u     IPv4 677630      0t0     TCP localhost:9612->localhost:50232 (CLOSE_WAIT)
main.test 31086 root 1021u     IPv4 676840      0t0     TCP localhost:9612->localhost:50238 (CLOSE_WAIT)

如上,https://github.com/cch123/golang-notes/blob/master/io.md 里最后splice的benchmark代码有sockfd泄露,
下面for循环里的defer n.Close()应该是执行不到的,close逻辑放到对应的normal和gosplice函数里合理一点。

    for {
	    n, err := l.Accept()
	    if err != nil {
		    continue
	    }
	    defer n.Close()
            ...
    }
wziww commented

感谢指出问题~

wziww commented

via #46

感谢提出,已合并