From 6a011ae7d9053687d3079e8e2c5389f58816e059 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Wed, 15 Jul 2026 03:47:02 +0900 Subject: [PATCH] req: hold the mutex for the whole defs read-modify-write in the outbox fan-out, reads and the Authors append were racing. --- req.go | 54 +++++++++++++++++++----------------------------------- 1 file changed, 19 insertions(+), 35 deletions(-) diff --git a/req.go b/req.go index 98e37bb..e19d048 100644 --- a/req.go +++ b/req.go @@ -333,21 +333,15 @@ func performReq( } matchUrl := func(def nostr.DirectedFilter) bool { return def.Relay == url } - idx := slices.IndexFunc(defs, matchUrl) - if idx == -1 { + mu.Lock() + if slices.IndexFunc(defs, matchUrl) == -1 { // new relay, add it - mu.Lock() - idx = slices.IndexFunc(defs, matchUrl) - if idx == -1 { - defs = append(defs, nostr.DirectedFilter{ - Filter: filter, - Relay: url, - }) - mu.Unlock() - continue - } - mu.Unlock() + defs = append(defs, nostr.DirectedFilter{ + Filter: filter, + Relay: url, + }) } + mu.Unlock() } return nil @@ -373,30 +367,20 @@ func performReq( } matchUrl := func(def nostr.DirectedFilter) bool { return def.Relay == url } - idx := slices.IndexFunc(defs, matchUrl) - if idx == -1 { + mu.Lock() + if idx := slices.IndexFunc(defs, matchUrl); idx == -1 { // new relay, add it - mu.Lock() - // check again after locking to prevent races - idx = slices.IndexFunc(defs, matchUrl) - if idx == -1 { - // then add it - filter := filter.Clone() - filter.Authors = []nostr.PubKey{pubkey} - defs = append(defs, nostr.DirectedFilter{ - Filter: filter, - Relay: url, - }) - mu.Unlock() - continue // done with this relay url - } - - // otherwise we'll just use the idx - mu.Unlock() + filter := filter.Clone() + filter.Authors = []nostr.PubKey{pubkey} + defs = append(defs, nostr.DirectedFilter{ + Filter: filter, + Relay: url, + }) + } else { + // existing relay, add this pubkey + defs[idx].Authors = append(defs[idx].Authors, pubkey) } - - // existing relay, add this pubkey - defs[idx].Authors = append(defs[idx].Authors, pubkey) + mu.Unlock() } return nil