diff --git a/control/controlclient/auto.go b/control/controlclient/auto.go index 8f4172414..a5eec8000 100644 --- a/control/controlclient/auto.go +++ b/control/controlclient/auto.go @@ -334,9 +334,10 @@ func (c *Auto) authRoutine() { report := func(err error, msg string) { c.logf("[v1] %s: %v", msg, err) - // don't send status updates for context errors, + // don't report context errors, // since context cancelation is always on purpose. if ctx.Err() == nil { + c.direct.health.SetAuthRoutineInError(err) c.sendStatus("authRoutine-report", err, "", nil) } } @@ -364,7 +365,6 @@ func (c *Auto) authRoutine() { f = "TryLogin" } if err != nil { - c.direct.health.SetAuthRoutineInError(err) report(err, f) if rle, ok := errors.AsType[*rateLimitError](err); ok { c.waitRetryAfter(ctx, "authRoutine", rle) diff --git a/control/controlclient/auto_test.go b/control/controlclient/auto_test.go index 40c3ab7f2..740c94be5 100644 --- a/control/controlclient/auto_test.go +++ b/control/controlclient/auto_test.go @@ -5,13 +5,17 @@ package controlclient import ( "context" + "errors" "sync" "testing" "testing/synctest" "time" + "tailscale.com/health" "tailscale.com/tailcfg" "tailscale.com/tstime" + "tailscale.com/types/key" + "tailscale.com/util/eventbus/eventbustest" ) func TestMapRoutineBackoffCanBePaused(t *testing.T) { @@ -171,3 +175,57 @@ func TestWaitRetryAfter(t *testing.T) { }) } } + +// TestAuthRoutineCanceledLoginNotReportedToHealth verifies that a login +// attempt that fails after it was canceled, such as when the client is +// paused, is not reported as a login error. +func TestAuthRoutineCanceledLoginNotReportedToHealth(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + ht := health.NewTracker(eventbustest.NewBus(t)) + ht.SetIPNState("Starting", true) + + // Each login attempt blocks until it receives its result. + loginErr := make(chan error) + ctx, cancel := context.WithCancel(t.Context()) + c := &Auto{ + logf: t.Logf, + direct: &Direct{ + logf: t.Logf, + health: ht, + hostinfo: new(tailcfg.Hostinfo), + getMachinePrivKey: func() (key.MachinePrivate, error) { + return key.MachinePrivate{}, <-loginErr + }, + }, + loginGoal: &LoginGoal{}, + authCtx: ctx, + authCancel: cancel, + authDone: make(chan struct{}), + } + defer func() { + c.mu.Lock() + c.closed = true + c.cancelAuthCtxLocked() + c.mu.Unlock() + close(loginErr) + <-c.authDone + }() + go c.authRoutine() + synctest.Wait() + + c.mu.Lock() + c.cancelAuthCtxLocked() + c.mu.Unlock() + loginErr <- errors.New("canceled attempt") + synctest.Wait() + if ht.IsUnhealthy(health.LoginStateWarnable) { + t.Error("canceled login attempt was reported to health") + } + + loginErr <- errors.New("failed attempt") + synctest.Wait() + if !ht.IsUnhealthy(health.LoginStateWarnable) { + t.Error("failed login attempt was not reported to health") + } + }) +} diff --git a/health/warnings.go b/health/warnings.go index bb6e7a242..8d367b93a 100644 --- a/health/warnings.go +++ b/health/warnings.go @@ -115,7 +115,8 @@ var LoginStateWarnable = condRegister(func() *Warnable { return "You are logged out." } }, - DependsOn: []*Warnable{IPNStateWarnable}, + DependsOn: []*Warnable{IPNStateWarnable}, + ImpactsConnectivity: true, } })