Repository navigation
udpnat2: Fix losing the first packet of a new flow - #120
Open
janeblower wants to merge 2 commits into
Open
janeblower wants to merge 2 commits into
janeblower wants to merge 2 commits into
Conversation
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.
nekohasekai
force-pushed
the
dev
branch
2 times, most recently
from
December 3, 2026 12:21
355b3af to
35ad80e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Service.NewPacketreadsconn.handlerunderhandlerAccess.RLock()but releases the lock before it queues the packet (service.go#L69-L72, service.go#L91-L96). The connection handler goroutine started for a new flow callsSetHandleras soon as copying starts. If that happens between the read and the send,SetHandlerdrains a still-emptypacketChan, and the packet is then queued into a channel nobody reads any more, because the connection is in push mode now. The first packet of the flow is lost.In sing-box v1.14.6 a single-datagram exchange through
tproxy(one DNS query, one UDP ping) sometimes gets no answer because of this. I hit it in about 1 of 10 runs of my end-to-end suite on shared CI runners.The fix keeps the read lock until the packet is queued, so
SetHandlerruns either before the read (the packet goes straight to the handler) or after the send (its drain picks the packet up). The handler itself is still called without the lock.The new test causes evictions, and with them the race detector reported a second, older race:
Closereadc.handlerwithout the lock whileSetHandlerwrote it. The first commit fixes that, so the package is clean under-race.TestServiceDeliversFirstPacketopens 100000 flows with one packet each, with a handler that callsSetHandlerright away:go testgo test -racemainClosemain+ first commitgo vet ./common/udpnat2/is clean.