From 616b38e2aec3feb896190e4570694c419419f4bc Mon Sep 17 00:00:00 2001 From: Jyn <85330965+janeblower@users.noreply.github.com> Date: Tue, 1 Dec 2026 09:44:30 +0700 Subject: [PATCH 1/2] udpnat2: Read the handler under its lock in Close --- common/udpnat2/conn.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 } From 5c3c9662b353d877d1c5a8dfb4d45fb63cbcc9e1 Mon Sep 17 00:00:00 2001 From: Jyn <85330965+janeblower@users.noreply.github.com> Date: Tue, 1 Dec 2026 09:44:30 +0700 Subject: [PATCH 2/2] udpnat2: Fix losing the first packet of a new flow NewPacket released handlerAccess before queueing the packet, so a SetHandler in between drained an empty queue and the packet was left in a channel nobody reads. --- common/udpnat2/service.go | 3 +- common/udpnat2/service_test.go | 56 ++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 common/udpnat2/service_test.go 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) + } +}