{"record":{"id":"25074598d7008c47","repo":"hashicorp/nomad","slug":"failed-to-decode-and-failed-to-read-buffered-data","errorCode":null,"errorMessage":"failed to decode and failed to read buffered data: %w","messagePattern":"failed to decode and failed to read buffered data: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"api/fs.go","lineNumber":305,"sourceCode":"\n\t\tfor {\n\t\t\t// Check if we have been cancelled\n\t\t\tselect {\n\t\t\tcase <-cancel:\n\t\t\t\tclose(frames)\n\t\t\t\treturn\n\t\t\tdefault:\n\t\t\t}\n\n\t\t\t// Decode the next frame\n\t\t\tvar frame StreamFrame\n\t\t\tif err := dec.Decode(&frame); err != nil {\n\t\t\t\tif err == io.EOF || err == io.ErrClosedPipe {\n\t\t\t\t\tclose(frames)\n\t\t\t\t} else {\n\t\t\t\t\tbuf, err2 := io.ReadAll(dec.Buffered())\n\t\t\t\t\tif err2 != nil {\n\t\t\t\t\t\terrCh <- fmt.Errorf(\"failed to decode and failed to read buffered data: %w\", errors.Join(err, err2))\n\t\t\t\t\t} else {\n\t\t\t\t\t\terrCh <- fmt.Errorf(\"failed to decode log endpoint response as JSON: %q\", buf)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\t// Discard heartbeat frames\n\t\t\tif frame.IsHeartbeat() {\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\tframes <- &frame\n\t\t}\n\t}()\n\n\treturn frames, errCh\n}","sourceCodeStart":287,"sourceCodeEnd":323,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/api/fs.go#L287-L323","documentation":"When streaming task logs/frames from the FS endpoint, the client JSON-decodes each frame. If decoding fails AND the remaining buffered data in the decoder also cannot be read, both errors are joined with errors.Join and surfaced with this message. It signals the stream delivered data that is not valid JSON and the buffer read also failed.","triggerScenarios":"Calling api.Query().Logs()/Stream() where the HTTP stream returns non-JSON or truncated content while dec.Buffered() is unreadable (e.g. connection cut mid-frame).","commonSituations":"Alloc just terminated mid-stream; proxy terminated the connection; server returned an HTML/plain-text error page instead of the JSON frame stream; TLS/agent crash mid-stream.","solutions":["Retry the log stream; transient disconnects are common with short-lived allocations","Check the target allocation is still running (nomad alloc status)","Capture errors.Join()'d wrapped error to see both the decode and buffer-read causes","Verify network path to the Nomad client node isn't dropping connections"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"for attempt := 0; attempt < 3; attempt++ {\n    err := streamLogs(ctx, allocID)\n    if err != nil && strings.Contains(err.Error(), \"failed to decode\") {\n        time.Sleep(backoff(attempt))\n        continue\n    }\n    return err\n}","preventionTips":["Check allocation health before opening a log stream","Verify the endpoint isn't behind an auth proxy that emits HTML","Retry transient stream failures with backoff"],"tags":["streaming","json","network"],"backgroundTag":"json-decode-failed","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}