Skip to content

Commit e9a1b61

Browse files
committed
Only wait for 5 seconds to report lifecycle at end
1 parent 80e4f0d commit e9a1b61

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
@@ -1269,18 +1269,28 @@ func (a *agent) Close() error {
12691269
// Set final state and wait for it to be reported because context
12701270
// cancellation will stop the report loop.
12711271
a.setLifecycle(ctx, lifecycleState)
1272-
// TODO(mafredri): What if the agent token is revoked, build outdated, etc.?
1273-
for s := range a.lifecycleReported {
1274-
if s == lifecycleState {
1275-
break
1276-
}
1277-
}
12781272

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

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

0 commit comments

Comments
 (0)