From 23eaeb3198cc148bbbed80f4d89a77419910fd6f Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Wed, 20 May 2026 23:30:00 +0800 Subject: [PATCH] Fix dispatcher race operation in WintunEndpoint --- tun_windows_gvisor.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tun_windows_gvisor.go b/tun_windows_gvisor.go index 2ead278..9e68b44 100644 --- a/tun_windows_gvisor.go +++ b/tun_windows_gvisor.go @@ -3,6 +3,8 @@ package tun import ( + "sync" + "github.com/sagernet/gvisor/pkg/buffer" "github.com/sagernet/gvisor/pkg/tcpip" "github.com/sagernet/gvisor/pkg/tcpip/header" @@ -23,6 +25,7 @@ var _ stack.LinkEndpoint = (*WintunEndpoint)(nil) type WintunEndpoint struct { tun *NativeTun + access sync.RWMutex dispatcher stack.NetworkDispatcher } @@ -49,6 +52,8 @@ func (e *WintunEndpoint) Capabilities() stack.LinkEndpointCapabilities { } func (e *WintunEndpoint) Attach(dispatcher stack.NetworkDispatcher) { + e.access.Lock() + defer e.access.Unlock() if dispatcher == nil && e.dispatcher != nil { e.dispatcher = nil return @@ -88,7 +93,9 @@ func (e *WintunEndpoint) dispatchLoop() { Payload: packetBuffer, IsForwardedPacket: true, }) + e.access.RLock() dispatcher := e.dispatcher + e.access.RUnlock() if dispatcher == nil { pkt.DecRef() return @@ -99,6 +106,8 @@ func (e *WintunEndpoint) dispatchLoop() { } func (e *WintunEndpoint) IsAttached() bool { + e.access.RLock() + defer e.access.RUnlock() return e.dispatcher != nil }