WireGuard proxy: Fix potential startup races (#6852)

https://github.com/XTLS/Xray-core/pull/6852#issuecomment-5871816550

Fixes https://github.com/XTLS/Xray-core/issues/6850
This commit is contained in:
v2ray
2026-09-29 18:13:43 +00:00
committed by RPRX
parent 48ad0300ea
commit 08cb6e6bca
3 changed files with 25 additions and 10 deletions
+12 -2
View File
@@ -52,9 +52,12 @@ func (b *bind) Open(port uint16) (fns []conn.ReceiveFunc, actualPort uint16, err
case <-ch:
default:
errors.LogErrorInner(context.Background(), err, "unexpected closed")
if b.downFunc != nil {
b.mu.Lock()
downFunc := b.downFunc
b.mu.Unlock()
if downFunc != nil {
go func() {
common.Must(b.downFunc())
common.Must(downFunc())
}()
}
}
@@ -76,6 +79,13 @@ func (b *bind) Open(port uint16) (fns []conn.ReceiveFunc, actualPort uint16, err
}, uint16(c.LocalAddr().(*net.UDPAddr).Port), nil
}
// setDownFunc sets downFunc after the device is created, since the device may already be using the bind.
func (b *bind) setDownFunc(f func() error) {
b.mu.Lock()
defer b.mu.Unlock()
b.downFunc = f
}
func (b *bind) Close() error {
b.mu.Lock()
defer b.mu.Unlock()