add a mutex lock around Challenges and Notices channel.

This commit is contained in:
fiatjaf
2023-03-31 22:09:01 -03:00
parent 411718f3c1
commit abfb7de394

View File

@@ -49,6 +49,7 @@ type Relay struct {
ConnectionContext context.Context // will be canceled when the connection closes ConnectionContext context.Context // will be canceled when the connection closes
okCallbacks s.MapOf[string, func(bool, string)] okCallbacks s.MapOf[string, func(bool, string)]
mutex sync.RWMutex
// custom things that aren't often used // custom things that aren't often used
// //
@@ -100,8 +101,10 @@ func (r *Relay) Connect(ctx context.Context) error {
// close these channels when the connection is dropped // close these channels when the connection is dropped
go func() { go func() {
<-r.ConnectionContext.Done() <-r.ConnectionContext.Done()
r.mutex.Lock()
close(r.Challenges) close(r.Challenges)
close(r.Notices) close(r.Notices)
r.mutex.Unlock()
}() }()
conn := NewConnection(socket) conn := NewConnection(socket)
@@ -161,17 +164,21 @@ func (r *Relay) Connect(ctx context.Context) error {
var content string var content string
json.Unmarshal(jsonMessage[1], &content) json.Unmarshal(jsonMessage[1], &content)
go func() { go func() {
r.mutex.RLock()
if r.ConnectionContext.Err() == nil { if r.ConnectionContext.Err() == nil {
r.Notices <- content r.Notices <- content
} }
r.mutex.RUnlock()
}() }()
case "AUTH": case "AUTH":
var challenge string var challenge string
json.Unmarshal(jsonMessage[1], &challenge) json.Unmarshal(jsonMessage[1], &challenge)
go func() { go func() {
r.mutex.RLock()
if r.ConnectionContext.Err() == nil { if r.ConnectionContext.Err() == nil {
r.Challenges <- challenge r.Challenges <- challenge
} }
r.mutex.RUnlock()
}() }()
case "EVENT": case "EVENT":
if len(jsonMessage) < 3 { if len(jsonMessage) < 3 {