Skip to content

Commit

Permalink
Merge pull request #282 from stgraber/main
Browse files Browse the repository at this point in the history
Fix header timeouts on operation wait
  • Loading branch information
hallyn authored Dec 7, 2023
2 parents e0a6bfc + e5a0594 commit f09e9b6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
36 changes: 27 additions & 9 deletions cmd/incusd/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -947,19 +947,37 @@ func operationWaitGet(d *Daemon, r *http.Request) response.Response {
ctx, cancel = context.WithCancel(r.Context())
}

defer cancel()
waitResponse := func(w http.ResponseWriter) error {
defer cancel()

// Write header to avoid client side timeouts.
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusOK)
f, ok := w.(http.Flusher)
if ok {
f.Flush()
}

err = op.Wait(ctx)
if err != nil {
return response.SmartError(err)
}
// Wait for the operation.
err = op.Wait(ctx)
if err != nil {
_ = response.SmartError(err).Render(w)
return nil
}

_, body, err := op.Render()
if err != nil {
return response.SmartError(err)
_, body, err := op.Render()
if err != nil {
_ = response.SmartError(err).Render(w)
return nil
}

_ = response.SyncResponse(true, body).Render(w)
return nil
}

return response.SyncResponse(true, body)
return response.ManualResponse(waitResponse)
}

// Then check if the query is from an operation on another node, and, if so, forward it
Expand Down
13 changes: 10 additions & 3 deletions internal/server/response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ func (r *syncResponse) Render(w http.ResponseWriter) error {
code = http.StatusOK
}

w.WriteHeader(code)
if w.Header().Get("Connection") != "keep-alive" {
w.WriteHeader(code)
}

// Handle plain text responses.
if r.plaintext {
Expand Down Expand Up @@ -347,7 +349,9 @@ func (r *errorResponse) Render(w http.ResponseWriter) error {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Content-Type-Options", "nosniff")

w.WriteHeader(r.code) // Set the error code in the HTTP header response.
if w.Header().Get("Connection") != "keep-alive" {
w.WriteHeader(r.code) // Set the error code in the HTTP header response.
}

_, err = fmt.Fprintln(w, buf.String())

Expand Down Expand Up @@ -524,7 +528,10 @@ func (r *forwardedResponse) Render(w http.ResponseWriter) error {
w.Header().Set(key, response.Header.Get(key))
}

w.WriteHeader(response.StatusCode)
if w.Header().Get("Connection") != "keep-alive" {
w.WriteHeader(response.StatusCode)
}

_, err = io.Copy(w, response.Body)
return err
}
Expand Down

0 comments on commit f09e9b6

Please sign in to comment.