From a2dca405c0fcb00e085ad498ec3443d147e42861 Mon Sep 17 00:00:00 2001 From: Zakk Date: Fri, 18 Sep 2026 01:57:25 +1000 Subject: [PATCH] fix(touch): copy the connected list before editing it during deletion DeleteWhich walked cssRaw.Get() while removing entries from cssAfter, which was the same slice header over the same backing array. After a non-tail removal the next touch saw the shifted list with a stale duplicate at the end: the same Which pointer was decremented twice or Disconnect ran on it, and cssAfter[:i] with the old length panicked with slice bounds out of range. Work on a copy and iterate that copy. --- service/server/service/touch.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/service/server/service/touch.go b/service/server/service/touch.go index 61bcb1f2..acd9ad62 100644 --- a/service/server/service/touch.go +++ b/service/server/service/touch.go @@ -12,8 +12,12 @@ func DeleteWhich(ws []*configure.Which) (err error) { // Sort touches to delete, placing larger indices first to delete from back to front data.SortSameTypeReverse() touches := data.Get() + // cssAfter is edited in place below; take a copy so the removals do not + // shift entries under the list being iterated (both used to alias the + // same backing array, leaving a stale duplicate at the tail after a + // non-tail removal and panicking on the next touch) cssRaw := configure.GetConnectedServers() - cssAfter := cssRaw.Get() + cssAfter := append([]*configure.Which(nil), cssRaw.Get()...) subscriptionsIndexes := make([]int, 0, len(ws)) serversIndexes := make([]int, 0, len(ws)) bDeletedSubscription := false @@ -23,7 +27,7 @@ func DeleteWhich(ws []*configure.Which) (err error) { switch v.TYPE { case configure.SubscriptionType: // Here a subscription is being deleted // Check if the currently connected node is in this subscription, if so, disconnect - css := cssRaw.Get() + css := cssAfter for i := len(css) - 1; i >= 0; i-- { cs := css[i] if cs != nil && cs.TYPE == configure.SubscriptionServerType { @@ -42,7 +46,7 @@ func DeleteWhich(ws []*configure.Which) (err error) { bDeletedSubscription = true case configure.ServerType: // Check if the currently connected node is this server, if so, disconnect - css := cssRaw.Get() + css := cssAfter for i := len(css) - 1; i >= 0; i-- { cs := css[i] if cs != nil && cs.TYPE == configure.ServerType {