Skip to content

fix(route): prevent use-after-close race on PacketConn in sniffing loop#4166

Open
hklcf wants to merge 1 commit into
SagerNet:testingfrom
hklcf:fix/packetconn-race
Open

fix(route): prevent use-after-close race on PacketConn in sniffing loop#4166
hklcf wants to merge 1 commit into
SagerNet:testingfrom
hklcf:fix/packetconn-race

Conversation

@hklcf
Copy link
Copy Markdown

@hklcf hklcf commented May 23, 2026

Description

In
oute/route.go:709-768, the packet sniffing loop spawns a goroutine that calls \SetReadDeadline()\ and \ReadPacket()\ on \inputPacketConn. When \ctx.Done()\ fires, the main goroutine calls \inputPacketConn.Close()\ at line 728 while the goroutine may still be using the connection.

Fix

Wait for the goroutine to finish (<-done) after closing \inputPacketConn. Since \Close()\ unblocks \ReadPacket(), the goroutine will return quickly and the connection won't be used after close.

Related Issue

Fixes #4161

When ctx is cancelled during packet sniffing, inputPacketConn.Close()
is called while a goroutine may still be calling ReadPacket() and
SetReadDeadline() on the same conn. Wait for the goroutine to finish
after close to prevent concurrent access.

Fixes SagerNet#4161
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Race condition on PacketConn in packet sniffing loop (use-after-close + concurrent SetReadDeadline)

1 participant