{"record":{"id":"5adfbe2f261c47c4","repo":"kovidgoyal/kitty","slug":"could-not-encode-message-to-kitty-with-error-w","errorCode":null,"errorMessage":"Could not encode message to kitty with error: %w","messagePattern":"Could not encode message to kitty with error: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kittens/choose_fonts/backend.go","lineNumber":73,"sourceCode":"\t}\n\tk.started = true\n\tk.timeout = 60 * time.Second\n\tk.wait_for_exit = make(chan error)\n\tgo func() {\n\t\tk.wait_for_exit <- k.cmd.Wait()\n\t}()\n\treturn\n}\n\nvar kitty_font_backend kitty_font_backend_type\n\nfunc (k *kitty_font_backend_type) send(v any) error {\n\tif k.to == nil {\n\t\treturn fmt.Errorf(\"Trying to send data when to pipe is nil\")\n\t}\n\tdata, err := json.Marshal(v)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"Could not encode message to kitty with error: %w\", err)\n\t}\n\tc := make(chan error)\n\tgo func() {\n\t\tif _, err = k.to.Write(data); err != nil {\n\t\t\tc <- fmt.Errorf(\"Failed to send message to kitty with I/O error: %w\", err)\n\t\t\treturn\n\t\t}\n\t\tif _, err = k.to.Write([]byte{'\\n'}); err != nil {\n\t\t\tc <- fmt.Errorf(\"Failed to send message to kitty with I/O error: %w\", err)\n\t\t\treturn\n\t\t}\n\t\tc <- nil\n\t}()\n\tselect {\n\tcase err := <-c:\n\t\treturn err\n\tcase <-time.After(k.timeout):\n\t\treturn fmt.Errorf(\"Timed out waiting to write to kitty font backend after %v\", k.timeout)","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/kovidgoyal/kitty/blob/6d5d0c440603ad9bdf6dcd599f73f6dde21acb44/kittens/choose_fonts/backend.go#L55-L91","documentation":"json.Marshal failed to serialize the message being sent to the kitty font backend. This only happens with values Go cannot encode: channels, functions, complex numbers, cyclic data structures, or NaN floats.","triggerScenarios":"Calling send(v) with v containing a channel, func, NaN/Inf float, or cyclic pointer graph destined for the +runpy backend.","commonSituations":"Custom forks of choose_fonts passing non-serializable state; floats computed as NaN (0/0) sneaking into font metrics structs.","solutions":["Ensure the payload contains only JSON-encodable types (no chan/func/complex/cycles)","Sanitize floats: replace NaN/Inf with math.IsNaN checks before send","Add a unit test that json.Marshals every message type you send"],"exampleFix":"// before\nbackend.send(map[string]any{\"v\": math.NaN()})\n// after\nbackend.send(map[string]any{\"v\": 0})","handlingStrategy":"validation","validationCode":"if _, err := json.Marshal(v); err != nil { /* reject payload before send */ }","typeGuard":"func isJSONEncodable(v any) bool { _, err := json.Marshal(v); return err == nil }","tryCatchPattern":"Pre-marshal to detect unencodable payloads; on error, sanitize the struct (drop chans/funcs, fix NaN) and retry.","preventionTips":["Keep message structs plain data","Guard floats with math.IsNaN/IsInf checks"],"tags":["kitty","json","serialization","choose-fonts"],"backgroundTag":"json-serialize-failed","analyzedSha":"6d5d0c440603ad9bdf6dcd599f73f6dde21acb44","analyzedAt":"2026-08-27T14:20:20.142Z","schemaVersion":2},"datasetVersion":"2026-08-27T19:17:21.184Z"}