Skip to content

Commit ab1469f

Browse files
committed
handle r.Context() canceled
1 parent 7f0b970 commit ab1469f

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

agent/agentcontainers/containers.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,29 @@ func New(options ...Option) http.Handler {
5757
}
5858

5959
func (ch *devcontainersHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
60-
ct, err := ch.getContainers(r.Context())
61-
if err != nil {
62-
if errors.Is(err, context.Canceled) {
63-
httpapi.Write(r.Context(), rw, http.StatusRequestTimeout, codersdk.Response{
60+
select {
61+
case <-r.Context().Done():
62+
// Client went away.
63+
return
64+
default:
65+
ct, err := ch.getContainers(r.Context())
66+
if err != nil {
67+
if errors.Is(err, context.Canceled) {
68+
httpapi.Write(r.Context(), rw, http.StatusRequestTimeout, codersdk.Response{
69+
Message: "Could not get containers.",
70+
Detail: "Took too long to list containers.",
71+
})
72+
return
73+
}
74+
httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{
6475
Message: "Could not get containers.",
65-
Detail: "Took too long to list containers.",
76+
Detail: err.Error(),
6677
})
6778
return
6879
}
69-
httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{
70-
Message: "Could not get containers.",
71-
Detail: err.Error(),
72-
})
73-
return
74-
}
7580

76-
httpapi.Write(r.Context(), rw, http.StatusOK, ct)
81+
httpapi.Write(r.Context(), rw, http.StatusOK, ct)
82+
}
7783
}
7884

7985
func (ch *devcontainersHandler) getContainers(ctx context.Context) (codersdk.WorkspaceAgentListContainersResponse, error) {

0 commit comments

Comments
 (0)