From 5ae19bc0e4eaacb08b6b296c2e29c26c587c3aec Mon Sep 17 00:00:00 2001 From: Viktor Liu Date: Thu, 20 Aug 2026 14:19:47 +0200 Subject: [PATCH] Fail modifyPeers before any removal when a peer's state is unavailable --- client/internal/engine.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/client/internal/engine.go b/client/internal/engine.go index 28fae5c9a..fddaa1722 100644 --- a/client/internal/engine.go +++ b/client/internal/engine.go @@ -863,15 +863,20 @@ func (e *Engine) modifyPeers(peersUpdate []*mgmProto.RemotePeerConfig) error { } } - // second, close all modified connections and remove them from the state map, - // remembering which of them were active + // second, look up the activation state of all modified peers before removing + // any of them, so an unavailable state leaves the current connections intact active := make(map[string]bool, len(modified)) for _, p := range modified { peerPubKey := p.GetWgPubKey() - if state, err := e.statusRecorder.GetPeer(peerPubKey); err == nil { - active[peerPubKey] = state.ConnStatus != peer.StatusIdle + state, err := e.statusRecorder.GetPeer(peerPubKey) + if err != nil { + return fmt.Errorf("get status of modified peer %s: %w", peerPubKey, err) } - if err := e.removePeer(peerPubKey); err != nil { + active[peerPubKey] = state.ConnStatus != peer.StatusIdle + } + // then close all modified connections and remove them from the state map + for _, p := range modified { + if err := e.removePeer(p.GetWgPubKey()); err != nil { return err } }