fix(distributed): set node header before first byte on streaming error path

In the chat.go error-from-worker branch the call to applyNodeIDHeader
came after fmt.Fprintf had already written to the response writer.
Go's http.ResponseWriter commits headers on the first Write, so the
X-LocalAI-Node header was silently dropped on streaming error
responses. Move the call before each Fprintf so the header is set on
both the marshal-error and the normal-error paths.

The non-streaming chat path and the completion error path were
already correctly ordered.

Assisted-by: Claude:claude-opus-4-7[1m]
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
Ettore Di Giacinto
2026-05-24 21:03:19 +00:00
parent df8418cb2d
commit 88306d562d

View File

@@ -449,12 +449,13 @@ func ChatEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, evaluator
respData, marshalErr := json.Marshal(errorResp)
if marshalErr != nil {
xlog.Error("Failed to marshal error response", "error", marshalErr)
applyNodeIDHeader()
fmt.Fprintf(c.Response().Writer, "data: {\"error\":{\"message\":\"Internal error\",\"type\":\"server_error\"}}\n\n")
} else {
applyNodeIDHeader()
fmt.Fprintf(c.Response().Writer, "data: %s\n\n", respData)
}
fmt.Fprintf(c.Response().Writer, "data: [DONE]\n\n")
applyNodeIDHeader()
c.Response().Flush()
return nil