From 91d2d341b79ca94ff6174812d09a9b3f5903118e Mon Sep 17 00:00:00 2001 From: riccardom Date: Tue, 16 Jun 2026 23:18:50 +0200 Subject: [PATCH] Guard is done inside not from external. Stop called unconditionally --- client/internal/connect.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/client/internal/connect.go b/client/internal/connect.go index 3bd5dd43b..5cc00955f 100644 --- a/client/internal/connect.go +++ b/client/internal/connect.go @@ -417,14 +417,12 @@ func (c *ConnectClient) run(runCtx context.Context, config *profilemanager.Confi c.engine = nil c.engineMutex.Unlock() - // todo: consider to remove this condition. Is not thread safe. - // We should always call Stop(), but we need to verify that it is idempotent - if engine.wgInterface != nil { - log.Infof("ensuring %s is removed, Netbird engine context cancelled", engine.wgInterface.Name()) - - if err := engine.Stop(); err != nil { - log.Errorf("Failed to stop engine: %v", err) - } + // Always tear the engine down once its context is cancelled. engine.Stop + // is nil-guarded per component, so calling it unconditionally is safe and + // avoids both the data race on engine.wgInterface and skipping teardown + // when the interface was never brought up (e.g. a mid-start failure). + if err := engine.Stop(); err != nil { + log.Errorf("Failed to stop engine: %v", err) } c.statusRecorder.ClientTeardown()