bufio.CachedConn: data race on the cached buffer between Read/ReaderReplaceable and Close
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 55/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- go
- Lĩnh vực
- networking
Hướng nghiên cứu
Start in common/bufio/cache.go with CachedConn.Read, ReaderReplaceable, Close, and the corresponding CachedPacketConn methods. Reproduce the reported concurrent paths with the race detector, including the routing and DNS-over-TCP entry points named in the issue. Done means the cache has one consistent ownership path, without races or double release.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
sing v0.8.10 (via sing-box v1.13.12), go 1.26, -race
CachedConn.buffer is read and cleared without synchronization in Read and ReaderReplaceable, while Close (usually called from another goroutine) reads and clears it too. Close guards itself with taken, but Read / ReaderReplaceable don't check or set taken:
// common/bufio/cache.go
func (c *CachedConn) Read(p []byte) (n int, err error) {
if c.buffer != nil { // read
...
c.buffer.DecRef()
c.buffer.Release()
c.buffer = nil // write
}
return c.Conn.Read(p)
}
func (c *CachedConn) ReaderReplaceable() bool {
return c.buffer == nil // read
}
func (c *CachedConn) Close() error {
if c.taken.CompareAndSwap(false, true) {
if buffer := c.buffer; buffer != nil { // read (races with Read)
c.buffer = nil
buffer.DecRef()
buffer.Release() // can release a buffer Read already released
}
}
return c.Conn.Close()
}
CachedPacketConn.ReadPacket / Close have the same pattern.
Besides the race report, the worst case is the pooled buffer being released twice, so it can come back from buf.New to another connection while still referenced.
Where it shows up in sing-box 1.13.12 (both found with -race):
- Routing a sniffed TCP connection:
route.(*Router).routeConnectionwraps it inbufio.NewCachedConn;ConnectionManager.connectionCopythen callsReaderReplaceable(viaN.UnwrapCountReaderinbufio.CopyWithCounters) on one goroutine, while the other copy direction callsClose(route/conn.go:262 vs :271). - Hijacked DNS over TCP:
protocol/dns.HandleStreamDNSRequestreads the next query from the conn (handle.go:24) while the exchange goroutine of the previous query callsconn.Close()on error (handle.go:46).
When the wrapper can be unwrapped before copying, ReadCached (which does use taken) avoids it, but callers such as a ConnectionTracker that wraps the conn force the unsynchronized Read path.
Suggested fix: make every consumer of the cache take it through the same atomic flag, e.g.
func (c *CachedConn) Read(p []byte) (n int, err error) {
if !c.taken.Load() {
c.mu.Lock() // or take the buffer once with CompareAndSwap and keep it reader-local
...
}
return c.Conn.Read(p)
}
or have Read take the buffer once (CompareAndSwap(false, true)), move it into a reader-owned field, and never touch it from Close after that. ReaderReplaceable would then read an atomic. Happy to send a PR if the approach sounds right.
- Ngôn ngữ chính
- Go
- Star
- 131
- Fork
- 116
- Merge trung bình
- 1 giờ 42 phút
- Pull request đã merge (30 ngày)
- 1
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của SagerNet/sing
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
-
socks握手时的bufio会吞掉客户端流水线发送的首段payload,进而导致UoT v2客户端的每条流都会失败Có thể đã có người làm @Snowy117 đã nhận 14 ngày trước. Đang mở
Độ khó 4/5 1-2 ngày Mức phù hợp với người mới 48/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
-
addr 相关的几个问题Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 42/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 30/100
Tất cả issue của SagerNet/sing
Issue tương tự
-
automation models
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
QuantumNous/new-api#7930 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area/gateway kind/bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
vllm-project/aibrix#3226 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
kind/bug kind/flake
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 73/100
kubernetes-sigs/kueue#17518 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày