{"record":{"id":"afa366b4c62a29b1","repo":"vitessio/vitess","slug":"err-error-json-marshal-failure-afa366","errorCode":null,"errorMessage":"err.Error() (JSON marshal failure)","messagePattern":"err\\.Error\\(\\) \\(JSON marshal failure\\)","errorType":"http","errorClass":null,"httpStatus":500,"severity":"warning","filePath":"go/vt/vttablet/tabletserver/twopcz.go","lineNumber":175,"sourceCode":"\t}\n\tdistributed, prepared, failed, err := txe.ReadTwopcInflight()\n\tif err != nil {\n\t\thttp.Error(w, err.Error(), http.StatusInternalServerError)\n\t\treturn\n\t}\n\tformat := r.FormValue(\"format\")\n\tif format == \"json\" {\n\t\tw.Header().Set(\"Content-Type\", \"application/json\")\n\t\tjs, err := json.Marshal(struct {\n\t\t\tDistributed      []*tx.DistributedTx\n\t\t\tPrepared, Failed []*tx.PreparedTx\n\t\t}{\n\t\t\tDistributed: distributed,\n\t\t\tPrepared:    prepared,\n\t\t\tFailed:      failed,\n\t\t})\n\t\tif err != nil {\n\t\t\thttp.Error(w, err.Error(), http.StatusInternalServerError)\n\t\t\treturn\n\t\t}\n\t\tw.Header().Set(\"Content-Type\", \"application/json\")\n\t\tw.Write(js)\n\t\treturn\n\t}\n\n\tw.Write(gridTable)\n\tw.Write([]byte(\"<h2>WARNING: Actions on this page can jeopardize data integrity.</h2>\\n\"))\n\tif msg != \"\" {\n\t\tfmt.Fprintln(w, html.EscapeString(msg))\n\t}\n\n\tw.Write(startTable)\n\tw.Write(failedzHeader)\n\tfor _, row := range failed {\n\t\tif err := failedzRow.Execute(w, row); err != nil {\n\t\t\tlog.Error(fmt.Sprintf(\"queryz: couldn't execute template: %v\", err))","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/vttablet/tabletserver/twopcz.go#L157-L193","documentation":"This is the HTTP 500 returned by the /twopcz handler when json.Marshal of the in-flight distributed transaction report (Distributed, Prepared, Failed lists) fails. In practice this is rare — it means the tx structures could not be serialized, typically due to an in-memory data anomaly rather than user input. The raw err.Error() text is written as the response body.","triggerScenarios":"A request to /twopcz?format=json where json.Marshal of the []*tx.DistributedTx / []*tx.PreparedTx payload returns an error, e.g. the transaction store contains data that cannot be marshaled (unsupported/corrupt field values in the tx structs).","commonSituations":"Scripting /twopcz?format=json to collect 2PC state for automation and getting a 500 back; running against a build/version where the tx protobuf structures changed; extremely unusual in-memory state after a crash.","solutions":["Read the raw error in the 500 body; it names the marshal failure and the offending type/field.","Retry the request — transient in-memory anomalies are usually resolved by a fresh read.","Use the HTML format (omit format=json) to view the same data and confirm whether it is a serialization-only issue.","If reproducible, capture the error and the offending dtid and file an issue; restart vttablet to reset in-memory transaction state if it is wedged."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"// Fall back to HTML format when JSON rendering of 2PC data fails\nurl := tabletURL + \"/twopcz?format=json\"\nif lastJSONFailed {\n    url = tabletURL + \"/twopcz\"\n}","typeGuard":null,"tryCatchPattern":"resp, err := http.Get(tabletURL + \"/twopcz?format=json\")\nif resp.StatusCode == http.StatusInternalServerError {\n    // retry once with HTML format\n    resp, err = http.Get(tabletURL + \"/twopcz\")\n}","preventionTips":["Keep /twopcz automation tolerant: parse either JSON or HTML output.","Restart vttablet if in-memory transaction state produces reproducible serialization errors.","Report reproducible marshal failures with the offending dtid to the Vitess project.","Avoid relying solely on the JSON debug endpoint for critical recovery automation."],"tags":["vttablet","twopc","json","serialization"],"backgroundTag":"json-marshal-failed","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}