req: hold the mutex for the whole defs read-modify-write in the outbox fan-out, reads and the Authors append were racing.

This commit is contained in:
Yasuhiro Matsumoto
2026-07-15 11:54:34 +09:00
parent 7c28e75c4b
commit 6a011ae7d9
+19 -35
View File
@@ -333,21 +333,15 @@ func performReq(
} }
matchUrl := func(def nostr.DirectedFilter) bool { return def.Relay == url } matchUrl := func(def nostr.DirectedFilter) bool { return def.Relay == url }
idx := slices.IndexFunc(defs, matchUrl) mu.Lock()
if idx == -1 { if slices.IndexFunc(defs, matchUrl) == -1 {
// new relay, add it // new relay, add it
mu.Lock() defs = append(defs, nostr.DirectedFilter{
idx = slices.IndexFunc(defs, matchUrl) Filter: filter,
if idx == -1 { Relay: url,
defs = append(defs, nostr.DirectedFilter{ })
Filter: filter,
Relay: url,
})
mu.Unlock()
continue
}
mu.Unlock()
} }
mu.Unlock()
} }
return nil return nil
@@ -373,30 +367,20 @@ func performReq(
} }
matchUrl := func(def nostr.DirectedFilter) bool { return def.Relay == url } matchUrl := func(def nostr.DirectedFilter) bool { return def.Relay == url }
idx := slices.IndexFunc(defs, matchUrl) mu.Lock()
if idx == -1 { if idx := slices.IndexFunc(defs, matchUrl); idx == -1 {
// new relay, add it // new relay, add it
mu.Lock() filter := filter.Clone()
// check again after locking to prevent races filter.Authors = []nostr.PubKey{pubkey}
idx = slices.IndexFunc(defs, matchUrl) defs = append(defs, nostr.DirectedFilter{
if idx == -1 { Filter: filter,
// then add it Relay: url,
filter := filter.Clone() })
filter.Authors = []nostr.PubKey{pubkey} } else {
defs = append(defs, nostr.DirectedFilter{ // existing relay, add this pubkey
Filter: filter, defs[idx].Authors = append(defs[idx].Authors, pubkey)
Relay: url,
})
mu.Unlock()
continue // done with this relay url
}
// otherwise we'll just use the idx
mu.Unlock()
} }
mu.Unlock()
// existing relay, add this pubkey
defs[idx].Authors = append(defs[idx].Authors, pubkey)
} }
return nil return nil