Skip to content

Commit 5adc708

Browse files
committed
Only wait for 5 seconds to report lifecycle at end
1 parent 1a9f646 commit 5adc708

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

agent/agent.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,18 +1268,28 @@ func (a *agent) Close() error {
12681268
// Set final state and wait for it to be reported because context
12691269
// cancellation will stop the report loop.
12701270
a.setLifecycle(ctx, lifecycleState)
1271-
// TODO(mafredri): What if the agent token is revoked, build outdated, etc.?
1272-
for s := range a.lifecycleReported {
1273-
if s == lifecycleState {
1274-
break
1275-
}
1276-
}
12771271

12781272
if lifecycleState != codersdk.WorkspaceAgentLifecycleOff {
12791273
// TODO(mafredri): Delay shutdown, ensure debugging is possible.
12801274
_ = false
12811275
}
12821276

1277+
// Wait for the lifecycle to be reported, but don't wait forever so
1278+
// that we don't break user expectations.
1279+
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
1280+
defer cancel()
1281+
lifecycleWaitLoop:
1282+
for {
1283+
select {
1284+
case <-ctx.Done():
1285+
break lifecycleWaitLoop
1286+
case s := <-a.lifecycleReported:
1287+
if s == lifecycleState {
1288+
break lifecycleWaitLoop
1289+
}
1290+
}
1291+
}
1292+
12831293
close(a.closed)
12841294
a.closeCancel()
12851295
_ = a.sshServer.Close()

0 commit comments

Comments
 (0)