diff --git a/common/udpnat2/conn.go b/common/udpnat2/conn.go index c8360ac4..a3380143 100644 --- a/common/udpnat2/conn.go +++ b/common/udpnat2/conn.go @@ -174,7 +174,10 @@ func (c *natConn) SetTimeout(timeout time.Duration) bool { func (c *natConn) Close() error { c.closeOnce.Do(func() { close(c.doneChan) - common.Close(c.handler) + c.handlerAccess.RLock() + handler := c.handler + c.handlerAccess.RUnlock() + common.Close(handler) }) return nil } diff --git a/common/udpnat2/service.go b/common/udpnat2/service.go index adb5f2e3..123b2670 100644 --- a/common/udpnat2/service.go +++ b/common/udpnat2/service.go @@ -69,7 +69,6 @@ func (s *Service) NewPacket(bufferSlices [][]byte, source M.Socksaddr, destinati conn.handlerAccess.RLock() readWaitOptions := conn.readWaitOptions handler := conn.handler - conn.handlerAccess.RUnlock() var dataLen int for _, bufferSlice := range bufferSlices { dataLen += len(bufferSlice) @@ -80,6 +79,7 @@ func (s *Service) NewPacket(bufferSlices [][]byte, source M.Socksaddr, destinati } readWaitOptions.PostReturn(buffer) if handler != nil { + conn.handlerAccess.RUnlock() handler.NewPacketEx(buffer, destination) return } @@ -94,6 +94,7 @@ func (s *Service) NewPacket(bufferSlices [][]byte, source M.Socksaddr, destinati packet.Buffer.Release() N.PutPacketBuffer(packet) } + conn.handlerAccess.RUnlock() } func (s *Service) NewPacketBatch(buffers []*buf.Buffer, sources []M.Socksaddr, destination M.Socksaddr, userData any) { diff --git a/common/udpnat2/service_test.go b/common/udpnat2/service_test.go new file mode 100644 index 00000000..91c2bfdf --- /dev/null +++ b/common/udpnat2/service_test.go @@ -0,0 +1,56 @@ +package udpnat + +import ( + "context" + "net/netip" + "sync/atomic" + "testing" + "time" + + "github.com/sagernet/sing/common/buf" + M "github.com/sagernet/sing/common/metadata" + N "github.com/sagernet/sing/common/network" +) + +type testCountingHandler struct { + delivered *atomic.Int64 +} + +func (h testCountingHandler) NewPacketEx(buffer *buf.Buffer, _ M.Socksaddr) { + h.delivered.Add(1) + buffer.Release() +} + +type testPushConnectionHandler struct { + delivered *atomic.Int64 +} + +func (h testPushConnectionHandler) NewPacketConnectionEx(_ context.Context, conn N.PacketConn, _ M.Socksaddr, _ M.Socksaddr, _ N.CloseHandlerFunc) { + conn.(*natConn).SetHandler(testCountingHandler{h.delivered}) +} + +func TestServiceDeliversFirstPacket(t *testing.T) { + t.Parallel() + + var delivered atomic.Int64 + prepare := func(M.Socksaddr, M.Socksaddr, any) (bool, context.Context, N.PacketWriter, N.CloseHandlerFunc) { + return true, context.Background(), testPacketWriter{}, nil + } + service := New(testPushConnectionHandler{&delivered}, prepare, time.Minute, false) + const flows = 100000 + destination := M.ParseSocksaddr("1.1.1.1:53") + for i := 0; i < flows; i++ { + source := M.SocksaddrFromNetIP(netip.AddrPortFrom(netip.AddrFrom4([4]byte{10, byte(i >> 16), byte(i >> 8), byte(i)}), 1024)) + service.NewPacket([][]byte{[]byte("a")}, source, destination, nil) + if i%1000 == 999 { + service.Purge() + } + } + deadline := time.Now().Add(5 * time.Second) + for delivered.Load() < flows && time.Now().Before(deadline) { + time.Sleep(10 * time.Millisecond) + } + if lost := flows - delivered.Load(); lost != 0 { + t.Fatalf("%d of %d first packets lost", lost, flows) + } +}