From 07ea3a35ec912a4c57875ade6cb6c40b2d5d89d3 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sun, 4 Aug 2024 17:25:19 -0300 Subject: [PATCH] removeClientAndListeners() -- stop confusing listeners when removing from the same ws. --- listener.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/listener.go b/listener.go index d93498e..b29d090 100644 --- a/listener.go +++ b/listener.go @@ -102,7 +102,7 @@ func (rl *Relay) removeClientAndListeners(ws *WebSocket) { defer rl.clientsMutex.Unlock() if specs, ok := rl.clients[ws]; ok { // swap delete listeners and delete client (all specs will be deleted) - for _, spec := range specs { + for s, spec := range specs { // no need to cancel contexts since they inherit from the main connection context // just delete the listeners (swap-delete) srl := spec.subrelay @@ -112,7 +112,12 @@ func (rl *Relay) removeClientAndListeners(ws *WebSocket) { moved := srl.listeners[movedFromIndex] // this wasn't removed, but will be moved srl.listeners[spec.index] = moved - // update the index of the listener we just moved + // temporarily update the spec of the listener being removed to have index == -1 + // (since it was removed) so it doesn't match in the search below + rl.clients[ws][s].index = -1 + + // now we must update the the listener we just moved + // so its .index reflects its new position on srl.listeners movedSpecs := rl.clients[moved.ws] idx := slices.IndexFunc(movedSpecs, func(ls listenerSpec) bool { return ls.index == movedFromIndex