Skip to content

Commit da4edd1

Browse files
authored
Routing: Add mutex for Attributes temporarily (XTLS#3908)
XTLS#3908 (comment)
1 parent 18c48ad commit da4edd1

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

common/session/session.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package session // import "github.com/xtls/xray-core/common/session"
44
import (
55
"context"
66
"math/rand"
7+
"sync"
78

89
c "github.com/xtls/xray-core/common/ctx"
910
"github.com/xtls/xray-core/common/errors"
@@ -65,7 +66,7 @@ type Outbound struct {
6566
Tag string
6667
// Name of the outbound proxy that handles the connection.
6768
Name string
68-
// Conn is actually internet.Connection. May be nil. It is currently nil for outbound with proxySettings
69+
// Conn is actually internet.Connection. May be nil. It is currently nil for outbound with proxySettings
6970
Conn net.Conn
7071
// CanSpliceCopy is a property for this connection
7172
// 1 = can, 2 = after processing protocol info should be able to, 3 = cannot
@@ -91,6 +92,10 @@ type Content struct {
9192
Attributes map[string]string
9293

9394
SkipDNSResolve bool
95+
96+
mu sync.Mutex
97+
98+
isLocked bool
9499
}
95100

96101
// Sockopt is the settings for socket connection.
@@ -99,8 +104,22 @@ type Sockopt struct {
99104
Mark int32
100105
}
101106

107+
// Some how when using mux, there will be a same ctx between different requests
108+
// This will cause problem as it's designed for single request, like concurrent map writes
109+
// Add a Mutex as a temp solution
110+
102111
// SetAttribute attaches additional string attributes to content.
103112
func (c *Content) SetAttribute(name string, value string) {
113+
if c.isLocked {
114+
errors.LogError(context.Background(), "Multiple goroutines are tring to access one routing content, tring to write ", name, ":", value)
115+
}
116+
c.mu.Lock()
117+
c.isLocked = true
118+
defer func() {
119+
c.isLocked = false
120+
c.mu.Unlock()
121+
}()
122+
104123
if c.Attributes == nil {
105124
c.Attributes = make(map[string]string)
106125
}

0 commit comments

Comments
 (0)