Skip to content

Commit 2dbe00a

Browse files
authored
fix(api): Allow workspace agent coordinate to report disconnect (coder#6152)
1 parent 6189035 commit 2dbe00a

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

coderd/coderd.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,11 @@ func New(options *Options) *API {
230230
staticHandler = httpmw.HSTS(staticHandler, options.StrictTransportSecurityCfg)
231231

232232
r := chi.NewRouter()
233+
ctx, cancel := context.WithCancel(context.Background())
233234
api := &API{
235+
ctx: ctx,
236+
cancel: cancel,
237+
234238
ID: uuid.New(),
235239
Options: options,
236240
RootHandler: r,
@@ -676,6 +680,11 @@ func New(options *Options) *API {
676680
}
677681

678682
type API struct {
683+
// ctx is canceled immediately on shutdown, it can be used to abort
684+
// interruptible tasks.
685+
ctx context.Context
686+
cancel context.CancelFunc
687+
679688
*Options
680689
// ID is a uniquely generated ID on initialization.
681690
// This is used to associate objects with a specific
@@ -710,6 +719,8 @@ type API struct {
710719

711720
// Close waits for all WebSocket connections to drain before returning.
712721
func (api *API) Close() error {
722+
api.cancel()
723+
713724
api.WebsocketWaitMutex.Lock()
714725
api.WebsocketWaitGroup.Wait()
715726
api.WebsocketWaitMutex.Unlock()

coderd/workspaceagents.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request
601601
Valid: true,
602602
}
603603
disconnectedAt := workspaceAgent.DisconnectedAt
604-
updateConnectionTimes := func() error {
604+
updateConnectionTimes := func(ctx context.Context) error {
605605
err = api.Database.UpdateWorkspaceAgentConnectionByID(ctx, database.UpdateWorkspaceAgentConnectionByIDParams{
606606
ID: workspaceAgent.ID,
607607
FirstConnectedAt: firstConnectedAt,
@@ -620,15 +620,23 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request
620620
}
621621

622622
defer func() {
623+
// If connection closed then context will be canceled, try to
624+
// ensure our final update is sent. By waiting at most the agent
625+
// inactive disconnect timeout we ensure that we don't block but
626+
// also guarantee that the agent will be considered disconnected
627+
// by normal status check.
628+
ctx, cancel := context.WithTimeout(api.ctx, api.AgentInactiveDisconnectTimeout)
629+
defer cancel()
630+
623631
disconnectedAt = sql.NullTime{
624632
Time: database.Now(),
625633
Valid: true,
626634
}
627-
_ = updateConnectionTimes()
628-
_ = api.Pubsub.Publish(watchWorkspaceChannel(build.WorkspaceID), []byte{})
635+
_ = updateConnectionTimes(ctx)
636+
api.publishWorkspaceUpdate(ctx, build.WorkspaceID)
629637
}()
630638

631-
err = updateConnectionTimes()
639+
err = updateConnectionTimes(ctx)
632640
if err != nil {
633641
_ = conn.Close(websocket.StatusGoingAway, err.Error())
634642
return
@@ -668,7 +676,7 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request
668676
Time: database.Now(),
669677
Valid: true,
670678
}
671-
err = updateConnectionTimes()
679+
err = updateConnectionTimes(ctx)
672680
if err != nil {
673681
_ = conn.Close(websocket.StatusGoingAway, err.Error())
674682
return

0 commit comments

Comments
 (0)