Keep authentication recovery diagnostics off the monitor lock

Clear the recorded authentication failure under the lock, then emit the existing diagnostic with its captured count. A slow write can still delay its own poll, but no longer holds the shared lock needed by unrelated poll workers.

Contract-Neutral: Authentication failure accounting, resulting poll state and diagnostic fields are unchanged; the log is emitted after the protected reset.
Change-source: pulse-maintainer
This commit is contained in:
pulse-triage[bot]
2026-12-02 11:42:38 +00:00
parent 4f417824df
commit 63cf0fca62
2 changed files with 123 additions and 7 deletions

View File

@@ -0,0 +1,113 @@
package monitoring
import (
"bytes"
"context"
"slices"
"sync"
"testing"
"testing/synctest"
"time"
"github.com/rcourtman/pulse-go-rewrite/internal/models"
"github.com/rcourtman/pulse-go-rewrite/pkg/proxmox"
"github.com/rs/zerolog"
)
type heldAuthRecoveryWriter struct {
entered chan<- string
resume <-chan struct{}
}
func (w heldAuthRecoveryWriter) Write(p []byte) (int, error) {
if bytes.Contains(p, []byte("Authentication succeeded, resetting failure count")) {
w.entered <- string(p)
<-w.resume
}
return len(p), nil
}
func TestAuthRecoveryDiagnosticDoesNotBlockPeerPoll(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
now := time.Now()
m := newScheduleFixture(ctx, false, func(context.Context) ([]proxmox.Node, error) {
return []proxmox.Node{{Node: scheduleFixtureNode, Status: "online", CPU: 0.42, Uptime: 36000}}, nil
})
const peer = "peer-pve"
cfg := m.config.PVEInstances[0]
cfg.Name, cfg.Host = peer, "https://peer-pve.invalid:8006"
m.config.PVEInstances = append(m.config.PVEInstances, cfg)
m.pveClients[peer] = &pollWorkFixturePVE{getNodes: func(context.Context) ([]proxmox.Node, error) {
return []proxmox.Node{{Node: peer, Status: "online", CPU: 0.36, Uptime: 36000}}, nil
}}
m.lastClusterCheck[peer] = now
key := "pve-" + scheduleFixtureNode
m.authFailures = map[string]int{key: 2}
m.lastAuthAttempt = map[string]time.Time{key: now.Add(-time.Minute)}
entered, resume := make(chan string, 1), make(chan struct{})
unblock := sync.OnceFunc(func() { close(resume) })
restore := setPackageTestLogOutput(t, heldAuthRecoveryWriter{entered: entered, resume: resume}, zerolog.InfoLevel)
defer func() {
unblock()
cancel()
synctest.Wait()
restore()
}()
enqueue := func(name string) {
m.taskQueue.Upsert(ScheduledTask{
InstanceName: name, InstanceType: InstanceTypePVE, NextRun: now, Interval: scheduleFixtureCadence,
})
}
go m.taskWorker(ctx, 0)
go m.taskWorker(ctx, 1)
enqueue(scheduleFixtureNode)
synctest.Wait()
select {
case message := <-entered:
if !bytes.Contains([]byte(message), []byte(`"node":"pve-`+scheduleFixtureNode+`"`)) ||
!bytes.Contains([]byte(message), []byte(`"previousFailures":2`)) {
t.Errorf("recovery message lost its subject or count: %s", message)
}
default:
t.Fatal("recovering poll did not enter the held diagnostic")
}
// Fail promptly if the regression returns: a peer parked in
// RWMutex.RLock does not count as durably blocked for synctest.Wait.
if !m.mu.TryRLock() {
t.Fatal("recovery diagnostic is holding the monitor lock")
}
_, hasFailures := m.authFailures[key]
_, hasLastAttempt := m.lastAuthAttempt[key]
m.mu.RUnlock()
if hasFailures || hasLastAttempt {
t.Error("the successful reconnect must clear its failure state before writing the diagnostic")
}
enqueue(peer)
synctest.Wait()
hasReading := func(name string, cpu float64) bool {
nodes := m.state.GetSnapshot().Nodes
return slices.ContainsFunc(nodes, func(n models.Node) bool { return n.Name == name && n.CPU == cpu })
}
if hasReading(scheduleFixtureNode, 0.42) {
t.Fatal("the test no longer holds the recovering poll")
}
if !hasReading(peer, 0.36) {
t.Error("peer did not publish a fresh reading while the recovery diagnostic was held")
}
if next, ok := m.taskQueue.Get(InstanceTypePVE, peer); !ok || !next.NextRun.Equal(now.Add(scheduleFixtureCadence)) {
t.Error("peer did not schedule its next poll while the recovery diagnostic was held")
}
unblock()
synctest.Wait()
if !hasReading(scheduleFixtureNode, 0.42) || !hasReading(peer, 0.36) {
t.Error("both connections must publish their fresh readings by the time the sink resumes")
}
})
}

View File

@@ -1772,22 +1772,25 @@ func (m *Monitor) recordAuthFailure(instanceName string, nodeType string) {
// resetAuthFailures resets the failure count for a node after successful auth
func (m *Monitor) resetAuthFailures(instanceName string, nodeType string) {
m.mu.Lock()
defer m.mu.Unlock()
nodeID := instanceName
if nodeType != "" {
nodeID = nodeType + "-" + instanceName
}
if count, exists := m.authFailures[nodeID]; exists && count > 0 {
m.mu.Lock()
count := m.authFailures[nodeID]
if count > 0 {
delete(m.authFailures, nodeID)
delete(m.lastAuthAttempt, nodeID)
}
m.mu.Unlock()
// A slow diagnostic sink must not hold the shared monitor lock.
if count > 0 {
log.Info().
Str("node", nodeID).
Int("previousFailures", count).
Msg("Authentication succeeded, resetting failure count")
delete(m.authFailures, nodeID)
delete(m.lastAuthAttempt, nodeID)
}
}