Fix flow close racing with tracker attach

This commit is contained in:
世界
2026-10-23 19:57:09 +08:00
parent 0164ce7b8e
commit 92fe54be05
2 changed files with 21 additions and 13 deletions
+20 -12
View File
@@ -2,6 +2,7 @@ package route
import (
"context"
"sync"
"sync/atomic"
"time"
@@ -16,7 +17,7 @@ import (
var (
_ tun.FlowTracker = (*flowLogger)(nil)
_ tun.FlowTracker = (*flowInterrupter)(nil)
_ tun.FlowTracker = multiFlowTracker(nil)
_ tun.FlowTracker = (*multiFlowTracker)(nil)
)
type flowLogger struct {
@@ -70,34 +71,41 @@ func (l *flowLogger) CloseFlow(reason tun.FlowCloseReason) {
", upload ", byteformats.FormatBytes(uint64(l.upload.Load())), ", download ", byteformats.FormatBytes(uint64(l.download.Load())))
}
type multiFlowTracker []tun.FlowTracker
type multiFlowTracker struct {
access sync.Mutex
trackers []tun.FlowTracker
}
func (t multiFlowTracker) AttachFlow(handle tun.FlowHandle) {
for _, tracker := range t {
func (t *multiFlowTracker) AttachFlow(handle tun.FlowHandle) {
t.access.Lock()
defer t.access.Unlock()
for _, tracker := range t.trackers {
tracker.AttachFlow(handle)
}
}
func (t multiFlowTracker) CountForward(n int) {
for _, tracker := range t {
func (t *multiFlowTracker) CountForward(n int) {
for _, tracker := range t.trackers {
tracker.CountForward(n)
}
}
func (t multiFlowTracker) CountReverse(n int) {
for _, tracker := range t {
func (t *multiFlowTracker) CountReverse(n int) {
for _, tracker := range t.trackers {
tracker.CountReverse(n)
}
}
func (t multiFlowTracker) FlowEstablished() {
for _, tracker := range t {
func (t *multiFlowTracker) FlowEstablished() {
for _, tracker := range t.trackers {
tracker.FlowEstablished()
}
}
func (t multiFlowTracker) CloseFlow(reason tun.FlowCloseReason) {
for _, tracker := range t {
func (t *multiFlowTracker) CloseFlow(reason tun.FlowCloseReason) {
t.access.Lock()
defer t.access.Unlock()
for _, tracker := range t.trackers {
tracker.CloseFlow(reason)
}
}
+1 -1
View File
@@ -564,7 +564,7 @@ func (r *Router) preMatchFlow(ctx context.Context, metadata *adapter.InboundCont
if len(flowTrackers) == 1 {
return flowTrackers[0]
}
return multiFlowTracker(flowTrackers)
return &multiFlowTracker{trackers: flowTrackers}
}
return result
}