ipn/ipnlocal: prevent cc leaks on multiple Start calls

If Start was called multiple times concurrently, it would
create a new client and shutdown the previous one. However
there was a race possible between shutting down the old one
and assigning a new one where the concurent goroutine may
have assigned another one already and it would leak.

Updates tailscale/corp#14471

Signed-off-by: Maisem Ali <maisem@tailscale.com>
pull/9230/head
Maisem Ali 1 year ago committed by Maisem Ali
parent 6bc15f3a73
commit 52a19b5970

@ -1470,6 +1470,13 @@ func (b *LocalBackend) Start(opts ipn.Options) error {
} }
b.mu.Lock() b.mu.Lock()
// Even though we reset b.cc above, we might have raced with
// another Start() call. If so, shut down the previous one again
// as we do not know if it was created with the same options.
prevCC = b.resetControlClientLocked()
if prevCC != nil {
defer prevCC.Shutdown() // must be called after b.mu is unlocked
}
b.cc = cc b.cc = cc
b.ccAuto, _ = cc.(*controlclient.Auto) b.ccAuto, _ = cc.(*controlclient.Auto)
endpoints := b.endpoints endpoints := b.endpoints

Loading…
Cancel
Save