Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix header timeouts on operation wait #282

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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