Skip to content

Commit 99dda4a

Browse files
authored
fix(agent): keep track of lastReportIndex between invocations of reportLifecycle() (#13075)
1 parent c24b562 commit 99dda4a

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

agent/agent.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,11 @@ type agent struct {
240240
sshServer *agentssh.Server
241241
sshMaxTimeout time.Duration
242242

243-
lifecycleUpdate chan struct{}
244-
lifecycleReported chan codersdk.WorkspaceAgentLifecycle
245-
lifecycleMu sync.RWMutex // Protects following.
246-
lifecycleStates []agentsdk.PostLifecycleRequest
243+
lifecycleUpdate chan struct{}
244+
lifecycleReported chan codersdk.WorkspaceAgentLifecycle
245+
lifecycleMu sync.RWMutex // Protects following.
246+
lifecycleStates []agentsdk.PostLifecycleRequest
247+
lifecycleLastReportedIndex int // Keeps track of the last lifecycle state we successfully reported.
247248

248249
network *tailnet.Conn
249250
addresses []netip.Prefix
@@ -625,7 +626,6 @@ func (a *agent) reportMetadata(ctx context.Context, conn drpc.Conn) error {
625626
// changes are reported in order.
626627
func (a *agent) reportLifecycle(ctx context.Context, conn drpc.Conn) error {
627628
aAPI := proto.NewDRPCAgentClient(conn)
628-
lastReportedIndex := 0 // Start off with the created state without reporting it.
629629
for {
630630
select {
631631
case <-a.lifecycleUpdate:
@@ -636,20 +636,20 @@ func (a *agent) reportLifecycle(ctx context.Context, conn drpc.Conn) error {
636636
for {
637637
a.lifecycleMu.RLock()
638638
lastIndex := len(a.lifecycleStates) - 1
639-
report := a.lifecycleStates[lastReportedIndex]
640-
if len(a.lifecycleStates) > lastReportedIndex+1 {
641-
report = a.lifecycleStates[lastReportedIndex+1]
639+
report := a.lifecycleStates[a.lifecycleLastReportedIndex]
640+
if len(a.lifecycleStates) > a.lifecycleLastReportedIndex+1 {
641+
report = a.lifecycleStates[a.lifecycleLastReportedIndex+1]
642642
}
643643
a.lifecycleMu.RUnlock()
644644

645-
if lastIndex == lastReportedIndex {
645+
if lastIndex == a.lifecycleLastReportedIndex {
646646
break
647647
}
648648
l, err := agentsdk.ProtoFromLifecycle(report)
649649
if err != nil {
650650
a.logger.Critical(ctx, "failed to convert lifecycle state", slog.F("report", report))
651651
// Skip this report; there is no point retrying. Maybe we can successfully convert the next one?
652-
lastReportedIndex++
652+
a.lifecycleLastReportedIndex++
653653
continue
654654
}
655655
payload := &proto.UpdateLifecycleRequest{Lifecycle: l}
@@ -662,13 +662,13 @@ func (a *agent) reportLifecycle(ctx context.Context, conn drpc.Conn) error {
662662
}
663663

664664
logger.Debug(ctx, "successfully reported lifecycle state")
665-
lastReportedIndex++
665+
a.lifecycleLastReportedIndex++
666666
select {
667667
case a.lifecycleReported <- report.State:
668668
case <-a.lifecycleReported:
669669
a.lifecycleReported <- report.State
670670
}
671-
if lastReportedIndex < lastIndex {
671+
if a.lifecycleLastReportedIndex < lastIndex {
672672
// Keep reporting until we've sent all messages, we can't
673673
// rely on the channel triggering us before the backlog is
674674
// consumed.

0 commit comments

Comments
 (0)