From d598619a6a6019ff6cd2145cbbc83599854d1b47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E6=89=87=E6=BB=91=E7=BF=94=E7=BF=BC?= Date: Tue, 15 Oct 2024 12:22:32 +0800 Subject: [PATCH] Routing: Add mutex for `Attributes` temporarily (#3908) https://github.com/XTLS/Xray-core/pull/3908#issuecomment-2412859858 --- common/session/session.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/common/session/session.go b/common/session/session.go index 43fb179a127b..d2f250f7525d 100644 --- a/common/session/session.go +++ b/common/session/session.go @@ -4,6 +4,7 @@ package session // import "github.com/amnezia-vpn/amnezia-xray-core/common/sessi import ( "context" "math/rand" + "sync" c "github.com/amnezia-vpn/amnezia-xray-core/common/ctx" "github.com/amnezia-vpn/amnezia-xray-core/common/errors" @@ -91,6 +92,10 @@ type Content struct { Attributes map[string]string SkipDNSResolve bool + + mu sync.Mutex + + isLocked bool } // Sockopt is the settings for socket connection. @@ -99,8 +104,22 @@ type Sockopt struct { Mark int32 } +// Some how when using mux, there will be a same ctx between different requests +// This will cause problem as it's designed for single request, like concurrent map writes +// Add a Mutex as a temp solution + // SetAttribute attaches additional string attributes to content. func (c *Content) SetAttribute(name string, value string) { + if c.isLocked { + errors.LogError(context.Background(), "Multiple goroutines are tring to access one routing content, tring to write ", name, ":", value) + } + c.mu.Lock() + c.isLocked = true + defer func() { + c.isLocked = false + c.mu.Unlock() + }() + if c.Attributes == nil { c.Attributes = make(map[string]string) }