@@ -240,10 +240,11 @@ type agent struct {
240
240
sshServer * agentssh.Server
241
241
sshMaxTimeout time.Duration
242
242
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.
247
248
248
249
network * tailnet.Conn
249
250
addresses []netip.Prefix
@@ -625,7 +626,6 @@ func (a *agent) reportMetadata(ctx context.Context, conn drpc.Conn) error {
625
626
// changes are reported in order.
626
627
func (a * agent ) reportLifecycle (ctx context.Context , conn drpc.Conn ) error {
627
628
aAPI := proto .NewDRPCAgentClient (conn )
628
- lastReportedIndex := 0 // Start off with the created state without reporting it.
629
629
for {
630
630
select {
631
631
case <- a .lifecycleUpdate :
@@ -636,20 +636,20 @@ func (a *agent) reportLifecycle(ctx context.Context, conn drpc.Conn) error {
636
636
for {
637
637
a .lifecycleMu .RLock ()
638
638
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 ]
642
642
}
643
643
a .lifecycleMu .RUnlock ()
644
644
645
- if lastIndex == lastReportedIndex {
645
+ if lastIndex == a . lifecycleLastReportedIndex {
646
646
break
647
647
}
648
648
l , err := agentsdk .ProtoFromLifecycle (report )
649
649
if err != nil {
650
650
a .logger .Critical (ctx , "failed to convert lifecycle state" , slog .F ("report" , report ))
651
651
// Skip this report; there is no point retrying. Maybe we can successfully convert the next one?
652
- lastReportedIndex ++
652
+ a . lifecycleLastReportedIndex ++
653
653
continue
654
654
}
655
655
payload := & proto.UpdateLifecycleRequest {Lifecycle : l }
@@ -662,13 +662,13 @@ func (a *agent) reportLifecycle(ctx context.Context, conn drpc.Conn) error {
662
662
}
663
663
664
664
logger .Debug (ctx , "successfully reported lifecycle state" )
665
- lastReportedIndex ++
665
+ a . lifecycleLastReportedIndex ++
666
666
select {
667
667
case a .lifecycleReported <- report .State :
668
668
case <- a .lifecycleReported :
669
669
a .lifecycleReported <- report .State
670
670
}
671
- if lastReportedIndex < lastIndex {
671
+ if a . lifecycleLastReportedIndex < lastIndex {
672
672
// Keep reporting until we've sent all messages, we can't
673
673
// rely on the channel triggering us before the backlog is
674
674
// consumed.
0 commit comments