{"record":{"id":"f9c071e6d5c12d29","repo":"ory/hydra","slug":"failed-to-read-response-s","errorCode":null,"errorMessage":"Failed to read response: %s","messagePattern":"Failed to read response: (.+?)","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"cmd/cmd_perform_device_flow.go","lineNumber":224,"sourceCode":"\t\thttp.Error(w, fmt.Sprintf(\"Failed to create request: %s\", err), http.StatusInternalServerError)\n\t\treturn\n\t}\n\treq.Header.Set(\"Content-Type\", \"application/json\")\n\treq.Header.Set(\"Accept\", \"application/json\")\n\n\thc := cfg.HTTPClient\n\tif hc == nil {\n\t\thc = http.DefaultClient\n\t}\n\tres, err := hc.Do(req)\n\tif err != nil {\n\t\thttp.Error(w, fmt.Sprintf(\"Failed to accept user code request: %s\", err), http.StatusInternalServerError)\n\t\treturn\n\t}\n\tdefer res.Body.Close() //nolint:errcheck\n\traw, err := io.ReadAll(io.LimitReader(res.Body, 1<<20))\n\tif err != nil {\n\t\thttp.Error(w, fmt.Sprintf(\"Failed to read response: %s\", err), http.StatusInternalServerError)\n\t\treturn\n\t}\n\tif res.StatusCode != http.StatusOK {\n\t\thttp.Error(w, fmt.Sprintf(\"Failed to accept user code request: %s\", raw), http.StatusInternalServerError)\n\t\treturn\n\t}\n\tvar accepted struct {\n\t\tRedirectTo string `json:\"redirect_to\"`\n\t}\n\tif err := json.Unmarshal(raw, &accepted); err != nil || accepted.RedirectTo == \"\" {\n\t\thttp.Error(w, \"Malformed response from the accept endpoint\", http.StatusInternalServerError)\n\t\treturn\n\t}\n\n\thttp.Redirect(w, r, accepted.RedirectTo, http.StatusSeeOther)\n}\n\nfunc (s *deviceSrv) GETdone(w http.ResponseWriter, r *http.Request) {","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/ory/hydra/blob/4174065ffb052799890f7480f5360a877a67ffc1/cmd/cmd_perform_device_flow.go#L206-L242","documentation":"Returned when reading the response body of the admin device-accept call fails. The body is read through io.LimitReader(res.Body, 1<<20) (1 MiB cap); io.ReadAll errors on premature connection close, chunked-encoding corruption, or read timeouts. The handler responds with a 500.","triggerScenarios":"io.ReadAll(io.LimitReader(res.Body, 1<<20)) at cmd/cmd_perform_device_flow.go:224 returns an error — the Hydra admin connection dropped mid-response, a proxy terminated the chunked body, or a keep-alive/timeout cut the read short.","commonSituations":"Flaky networks between CLI and admin API; reverse proxies (nginx/traefik) with aggressive timeouts killing long responses; HTTP/1.0 style closes; running behind a load balancer that resets idle connections.","solutions":["Retry the whole accept request once on read failure — the error is transient and the PUT is idempotent for the same challenge.","Check proxy/load-balancer timeout settings between the CLI and Hydra admin and raise read timeouts.","Inspect Hydra admin logs at the same timestamp for a panic or early exit producing a truncated response."],"exampleFix":"// before\nraw, err := io.ReadAll(io.LimitReader(res.Body, 1<<20))\nif err != nil {\n\thttp.Error(w, fmt.Sprintf(\"Failed to read response: %s\", err), http.StatusInternalServerError)\n\treturn\n}\n// after\nraw, err := io.ReadAll(io.LimitReader(res.Body, 1<<20))\nif err != nil {\n\tlogger.Errorf(\"reading admin response failed: %v\", err)\n\thttp.Error(w, \"Truncated response from admin endpoint\", http.StatusBadGateway)\n\treturn\n}","handlingStrategy":"retry","validationCode":"// Pre-check not directly possible; mitigate by bounding the read and checking status early.\nif res.StatusCode >= 500 {\n\treturn fmt.Errorf(\"admin server error %d, body likely truncated/empty\", res.StatusCode)\n}","typeGuard":null,"tryCatchPattern":"raw, err := io.ReadAll(io.LimitReader(res.Body, 1<<20))\nif err != nil {\n\t// retry the whole request once — read errors are usually transient\n\tif res2, rerr := hc.Do(req.Clone(ctx)); rerr == nil {\n\t\tres, raw, err = res2, nil, nil\n\t\traw, err = io.ReadAll(io.LimitReader(res.Body, 1<<20))\n\t}\n\tif err != nil {\n\t\thttp.Error(w, \"truncated admin response\", http.StatusBadGateway)\n\t\treturn\n\t}\n}","preventionTips":["Raise proxy/LB read timeouts between client and Hydra admin","Retry the entire idempotent PUT on read failure","Keep the LimitReader bound so a hostile body can't exhaust memory","Correlate with admin-side logs when truncation recurs"],"tags":["network","http","io"],"backgroundTag":"truncated-response-body","analyzedSha":"4174065ffb052799890f7480f5360a877a67ffc1","analyzedAt":"2026-09-03T14:52:41.581Z","contentChangedAt":"2026-09-03T14:52:41.581Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}