fix: websocket race-condition and firmware upload
This commit is contained in:
@@ -225,20 +225,49 @@ func (c *SporeClient) UpdateFirmware(firmwareData []byte, filename string) (*Fir
|
||||
Timeout: 5 * time.Minute, // 5 minutes for firmware uploads
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"node_ip": c.BaseURL,
|
||||
"status": "sending_firmware",
|
||||
}).Debug("Sending firmware to SPORE device")
|
||||
|
||||
resp, err := firmwareClient.Do(req)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"node_ip": c.BaseURL,
|
||||
"error": err.Error(),
|
||||
}).Error("Failed to send firmware request to SPORE device")
|
||||
return nil, fmt.Errorf("failed to upload firmware: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"node_ip": c.BaseURL,
|
||||
"status_code": resp.StatusCode,
|
||||
"headers": resp.Header,
|
||||
}).Debug("Received response from SPORE device")
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
// Only try to read body for error cases
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
log.WithFields(log.Fields{
|
||||
"node_ip": c.BaseURL,
|
||||
"status": resp.StatusCode,
|
||||
"error_body": string(body),
|
||||
}).Error("SPORE device reported firmware upload failure")
|
||||
return nil, fmt.Errorf("firmware update failed with status %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var updateResponse FirmwareUpdateResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&updateResponse); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode firmware update response: %w", err)
|
||||
// For successful firmware uploads, don't try to read the response body
|
||||
// The SPORE device restarts immediately after sending the response, so reading the body
|
||||
// would cause the connection to hang or timeout
|
||||
log.WithFields(log.Fields{
|
||||
"node_ip": c.BaseURL,
|
||||
"status": "success_no_body",
|
||||
}).Info("Firmware upload completed successfully (device restarting)")
|
||||
|
||||
updateResponse := FirmwareUpdateResponse{
|
||||
Status: "OK",
|
||||
Message: "Firmware update completed successfully",
|
||||
}
|
||||
|
||||
return &updateResponse, nil
|
||||
|
||||
Reference in New Issue
Block a user