From 5d7cb30e5b544a80c448da7f71d4b6d93e9af952 Mon Sep 17 00:00:00 2001 From: riccardom Date: Thu, 18 Jun 2026 08:35:21 +0200 Subject: [PATCH] Removes other occurrencies of connectClient check --- client/server/mdm.go | 5 ++--- client/server/server.go | 22 ++++++---------------- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/client/server/mdm.go b/client/server/mdm.go index 190f7b045..16ea489c8 100644 --- a/client/server/mdm.go +++ b/client/server/mdm.go @@ -61,8 +61,8 @@ func (s *Server) onMDMPolicyChange(_, _ *mdm.Policy) error { s.mutex.Lock() defer s.mutex.Unlock() - if !s.clientRunning { - // The client is not running, so there's no engine to restart. + if !s.connectClient.IsRunning() { + // No run in flight, so there's no engine to restart. return nil } @@ -148,7 +148,6 @@ func (s *Server) restartEngineForMDMLocked() error { _, cancel := context.WithCancel(s.rootCtx) s.actCancel = cancel - s.clientRunning = true s.clientRunningChan = make(chan struct{}) s.clientDoneChan = make(chan error, 1) log.Info("MDM restart: starting a fresh run with re-resolved config") diff --git a/client/server/server.go b/client/server/server.go index 7ea4cae3f..4eeedcdf1 100644 --- a/client/server/server.go +++ b/client/server/server.go @@ -62,10 +62,8 @@ type Server struct { mutex sync.Mutex config *profilemanager.Config proto.UnimplementedDaemonServiceServer - // clientRunning tracks "the daemon wants to be connected" — set true by - // Start / Up, cleared by Down / Logout. Protected by s.mutex. Whether a run - // is actually in flight is owned by the supervisor (connectClient.IsRunning). - clientRunning bool + // Whether a run is in flight is owned by the supervisor + // (connectClient.IsRunning); the daemon keeps no separate "running" flag. clientRunningChan chan struct{} // closed by the run when the engine is ready clientDoneChan chan error // receives the run's end result @@ -141,7 +139,7 @@ func (s *Server) Start() error { s.mutex.Lock() defer s.mutex.Unlock() - if s.clientRunning { + if s.connectClient.IsRunning() { return nil } @@ -229,7 +227,6 @@ func (s *Server) Start() error { return nil } - s.clientRunning = true s.clientRunningChan = make(chan struct{}) s.clientDoneChan = make(chan error, 1) // Boot autoconnect: no incoming RPC metadata. The supervisor runs the @@ -672,7 +669,7 @@ func (s *Server) Up(callerCtx context.Context, msg *proto.UpRequest) (*proto.UpR // by Down). IsRunning() reports whether a run is actually in flight. When // intent is up AND a run is in flight, the existing engine is on the job — // just wait for it. Otherwise fall through to start a fresh run. - if s.clientRunning && s.connectClient.IsRunning() { + if s.connectClient.IsRunning() { state := internal.CtxGetState(s.rootCtx) status, err := state.Status() if err != nil { @@ -758,7 +755,6 @@ func (s *Server) Up(callerCtx context.Context, msg *proto.UpRequest) (*proto.UpR s.statusRecorder.UpdateManagementAddress(s.config.ManagementURL.String()) s.statusRecorder.UpdateRosenpass(s.config.RosenpassEnabled, s.config.RosenpassPermissive) - s.clientRunning = true s.clientRunningChan = make(chan struct{}) s.clientDoneChan = make(chan error, 1) @@ -888,12 +884,6 @@ func (s *Server) cleanupConnection() error { return ErrServiceNotUp } - // Daemon intent flips to "down" — all callers (Down RPC, - // Logout RPC handlers) tear down the connection because the user - // explicitly asked for it. MDM restart does NOT go through this - // path, so its clientRunning stays true. - s.clientRunning = false - // Tear the client down through the lifecycle supervisor BEFORE cancelling // the retry context. Stop serializes on the supervisor queue and blocks // until the in-flight run has fully unwound (a clean, synchronous teardown). @@ -1039,7 +1029,7 @@ func (s *Server) validateProfileOperation(profileName string, allowActiveProfile // logoutFromProfile logs out from a specific profile by loading its config and sending logout request func (s *Server) logoutFromProfile(ctx context.Context, profileName, username string) error { activeProf, err := s.profileManager.GetActiveProfileState() - if err == nil && activeProf.Name == profileName && s.clientRunning { + if err == nil && activeProf.Name == profileName && s.connectClient.IsRunning() { return s.sendLogoutRequest(ctx) } @@ -1345,7 +1335,7 @@ func (s *Server) WaitJWTToken( // ExposeService exposes a local port via the NetBird reverse proxy. func (s *Server) ExposeService(req *proto.ExposeServiceRequest, srv proto.DaemonService_ExposeServiceServer) error { s.mutex.Lock() - if !s.clientRunning { + if !s.connectClient.IsRunning() { s.mutex.Unlock() return gstatus.Errorf(codes.FailedPrecondition, "client is not running, run 'netbird up' first") }