From 8817765aebff2485a9812fe9ae15e9dcffa15249 Mon Sep 17 00:00:00 2001 From: Misha Bragin Date: Fri, 2 Jun 2023 18:04:24 +0200 Subject: [PATCH] Add comment clarifying AddPeer race check (#927) --- management/server/peer.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/management/server/peer.go b/management/server/peer.go index f468e7369..b2fe0955a 100644 --- a/management/server/peer.go +++ b/management/server/peer.go @@ -495,6 +495,11 @@ func (am *DefaultAccountManager) AddPeer(setupKey, userID string, peer *Peer) (* return nil, nil, err } + // This is a handling for the case when the same machine (with the same WireGuard pub key) tries to register twice. + // Such case is possible when AddPeer function takes long time to finish after AcquireAccountLock (e.g., database is slow) + // and the peer disconnects with a timeout and tries to register again. + // We just check if this machine has been registered before and reject the second registration. + // The connecting peer should be able to recover with a retry. _, err = account.FindPeerByPubKey(peer.Key) if err == nil { return nil, nil, status.Errorf(status.PreconditionFailed, "peer has been already registered")