@@ -601,7 +601,7 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request
601
601
Valid : true ,
602
602
}
603
603
disconnectedAt := workspaceAgent .DisconnectedAt
604
- updateConnectionTimes := func () error {
604
+ updateConnectionTimes := func (ctx context. Context ) error {
605
605
err = api .Database .UpdateWorkspaceAgentConnectionByID (ctx , database.UpdateWorkspaceAgentConnectionByIDParams {
606
606
ID : workspaceAgent .ID ,
607
607
FirstConnectedAt : firstConnectedAt ,
@@ -620,15 +620,23 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request
620
620
}
621
621
622
622
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
+
623
631
disconnectedAt = sql.NullTime {
624
632
Time : database .Now (),
625
633
Valid : true ,
626
634
}
627
- _ = updateConnectionTimes ()
628
- _ = api .Pubsub . Publish ( watchWorkspaceChannel ( build .WorkspaceID ), [] byte {} )
635
+ _ = updateConnectionTimes (ctx )
636
+ api .publishWorkspaceUpdate ( ctx , build .WorkspaceID )
629
637
}()
630
638
631
- err = updateConnectionTimes ()
639
+ err = updateConnectionTimes (ctx )
632
640
if err != nil {
633
641
_ = conn .Close (websocket .StatusGoingAway , err .Error ())
634
642
return
@@ -668,7 +676,7 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request
668
676
Time : database .Now (),
669
677
Valid : true ,
670
678
}
671
- err = updateConnectionTimes ()
679
+ err = updateConnectionTimes (ctx )
672
680
if err != nil {
673
681
_ = conn .Close (websocket .StatusGoingAway , err .Error ())
674
682
return
0 commit comments