{"record":{"id":"e9920a3ebb0fe77d","repo":"thanos-io/thanos","slug":"received-message-larger-than-max-d-vs-d","errorCode":null,"errorMessage":"received message larger than max (%d vs %d)","messagePattern":"received message larger than max \\((.+?) vs (.+?)\\)","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"internal/cortex/util/http.go","lineNumber":55,"sourceCode":"}\n\nfunc (b *BasicAuth) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {\n\tf.StringVar(&b.Username, prefix+\"basic-auth-username\", \"\", \"HTTP Basic authentication username. It overrides the username set in the URL (if any).\")\n\tf.StringVar(&b.Password, prefix+\"basic-auth-password\", \"\", \"HTTP Basic authentication password. It overrides the password set in the URL (if any).\")\n}\n\n// IsEnabled returns false if basic authentication isn't enabled.\nfunc (b BasicAuth) IsEnabled() bool {\n\treturn b.Username != \"\" || b.Password != \"\"\n}\n\n// WriteJSONResponse writes some JSON as a HTTP response.\nfunc WriteJSONResponse(w http.ResponseWriter, v any) {\n\tw.Header().Set(\"Content-Type\", \"application/json\")\n\n\tdata, err := json.Marshal(v)\n\tif err != nil {\n\t\thttp.Error(w, err.Error(), http.StatusInternalServerError)\n\t\treturn\n\t}\n\n\t// We ignore errors here, because we cannot do anything about them.\n\t// Write will trigger sending Status code, so we cannot send a different status code afterwards.\n\t// Also this isn't internal error, but error communicating with client.\n\t_, _ = w.Write(data)\n}\n\n// WriteYAMLResponse writes some YAML as a HTTP response.\nfunc WriteYAMLResponse(w http.ResponseWriter, v any) {\n\t// There is not standardised content-type for YAML, text/plain ensures the\n\t// YAML is displayed in the browser instead of offered as a download\n\tw.Header().Set(\"Content-Type\", \"text/plain; charset=utf-8\")\n\n\tdata, err := yaml.Marshal(v)\n\tif err != nil {\n\t\thttp.Error(w, err.Error(), http.StatusInternalServerError)","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/thanos-io/thanos/blob/35b8b991177def87ed52dcf10f9b6d87f07282c8/internal/cortex/util/http.go#L37-L73","documentation":"WriteJSONResponse marshals v to JSON and writes it to the HTTP response body; if the serialized payload exceeds the allowed maximum message size it is rejected with this error. The message reports the actual size versus the configured maximum. It exists to prevent unbounded responses from exhausting memory or violating gRPC/message-size contracts.","triggerScenarios":"Calling WriteJSONResponse (directly or through RenderHTTPResponse) with a value whose json.Marshal output exceeds the configured max message size.","commonSituations":"Handlers returning very large query results or bulk exports over HTTP APIs, or limits tuned for gRPC (e.g. 4MB/16MB defaults) being applied to JSON responses.","solutions":["Reduce the response size (paginate, limit results, or stream instead of buffering the whole JSON)","Increase the configured max message size if legitimately large responses are expected","Compress the response (e.g. gzip) so the payload fits within limits","Switch the endpoint to a streaming variant if the library provides one"],"exampleFix":"// before\nutil.WriteJSONResponse(w, hugeResult)\n// after\nw.Header().Set(\"Content-Encoding\", \"gzip\")\ngz := gzip.NewWriter(w)\nutil.WriteJSONResponse(gzWriter{ResponseWriter: w, Writer: gz}, paginatedResult)","handlingStrategy":"validation","validationCode":"data, err := json.Marshal(v)\nif err != nil { return err }\nif len(data) > maxMessageSize {\n    return fmt.Errorf(\"response too large: %d > %d\", len(data), maxMessageSize)\n}","typeGuard":null,"tryCatchPattern":"if err := util.WriteJSONResponse(w, v); err != nil {\n    if strings.Contains(err.Error(), \"received message larger than max\") {\n        http.Error(w, \"response too large; paginate the request\", http.StatusRequestEntityTooLarge)\n        return\n    }\n    http.Error(w, err.Error(), http.StatusInternalServerError)\n}","preventionTips":["Paginate or limit result sets before serializing responses","Estimate serialized size before writing large responses","Configure max message sizes consistently across client and server","Prefer streaming endpoints for large payloads"],"tags":["http","json","payload-too-large"],"backgroundTag":"payload-too-large","analyzedSha":"35b8b991177def87ed52dcf10f9b6d87f07282c8","analyzedAt":"2026-09-07T01:49:59.689Z","contentChangedAt":"2026-09-07T01:49:59.689Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}