{"record":{"id":"aac742c8e07f193f","repo":"lima-vm/lima","slug":"failed-to-unmarshal-line-q-w","errorCode":null,"errorMessage":"failed to unmarshal line %#q: %w","messagePattern":"failed to unmarshal line %#q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/guestagent/kubernetesservice/kubernetesservice.go","lineNumber":168,"sourceCode":"\tscanner := bufio.NewScanner(r)\n\t// increase buffer in case of large JSON objects\n\tconst maxBuf = 10 * units.MiB\n\tbuf := make([]byte, 0, 64*units.KiB)\n\tscanner.Buffer(buf, maxBuf)\n\n\tfor scanner.Scan() {\n\t\tline := scanner.Bytes()\n\t\tline = bytes.TrimSpace(line)\n\t\tif len(line) == 0 {\n\t\t\tcontinue\n\t\t}\n\n\t\tvar ev struct {\n\t\t\tType   eventType       `json:\"type\"`\n\t\t\tObject json.RawMessage `json:\"object\"`\n\t\t}\n\t\tif err := json.Unmarshal(line, &ev); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to unmarshal line %#q: %w\", string(line), err)\n\t\t}\n\n\t\tvar svc service\n\t\tif err := json.Unmarshal(ev.Object, &svc); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to unmarshal service object: %w (line=%#q)\", err, line)\n\t\t}\n\n\t\tkey := svc.Metadata.Namespace + \"/\" + svc.Metadata.Name\n\t\ts.rwMutex.Lock()\n\t\tswitch ev.Type {\n\t\tcase added, modified:\n\t\t\ts.serviceSpecs[key] = &svc.Spec\n\t\tcase deleted:\n\t\t\tdelete(s.serviceSpecs, key)\n\t\tdefault:\n\t\t\t// NOP\n\t\t}\n\t\ts.rwMutex.Unlock()","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/lima-vm/lima/blob/dd909d0973cd84fa35f9e1693181b4585ea616c1/pkg/guestagent/kubernetesservice/kubernetesservice.go#L150-L186","documentation":"readKubectlStream parses newline-delimited kubectl JSON watch output. Each line must be a JSON object with `type` and `object` fields. When a line is not valid JSON (truncated output, mixed non-JSON text like kubectl warnings, encoding corruption), this error is thrown with the offending line quoted.","triggerScenarios":"startAndStreamKubectl -> readKubectlStream when scanner yields a line that json.Unmarshal cannot decode into the anonymous {type, object} struct.","commonSituations":"kubectl emits plain-text warnings or error banners (e.g. 'Unable to connect') interleaved with the JSON stream; a line is truncated due to buffering or signal kill; locale/encoding issues corrupt output.","solutions":["Inspect the quoted line in the error; if it is a kubectl warning, run with `--warnings-as-errors=false` isolation or use `--output-watch-events`/stable flag combinations.","Ensure kubectl is invoked with `-o json` and a version supporting JSON watch output.","Filter or skip non-JSON lines before unmarshaling if robustness is needed.","Retry the watch if the stream was truncated by a transient kill."],"exampleFix":"// before: any garbage line aborts the whole stream\nif err := json.Unmarshal(line, &ev); err != nil {\n\treturn fmt.Errorf(\"failed to unmarshal line %#q: %w\", string(line), err)\n}\n// after: skip lines that are not JSON watch events\nif err := json.Unmarshal(line, &ev); err != nil {\n\tlogrus.Debugf(\"skipping non-JSON line: %s\", line)\n\tcontinue\n}","handlingStrategy":"validation","validationCode":"// skip lines that are not JSON objects before unmarshaling\ntrimmed := bytes.TrimSpace(line)\nif len(trimmed) == 0 || trimmed[0] != '{' {\n\treturn nil // skip non-JSON kubectl output\n}","typeGuard":null,"tryCatchPattern":"if err := readKubectlStream(stdout); err != nil {\n\tif strings.Contains(err.Error(), \"failed to unmarshal line\") {\n\t\tlog.WithError(err).Warn(\"skipping malformed kubectl output line\")\n\t\treturn nil\n\t}\n\treturn err\n}","preventionTips":["Always run kubectl with `-o json` for watch output.","Suppress or redirect kubectl warning text away from the JSON stream.","Use a kubectl version whose watch output is known-good.","Test the stream parser against kubectl warning scenarios."],"tags":["guestagent","json","parsing","kubernetes"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"dd909d0973cd84fa35f9e1693181b4585ea616c1","analyzedAt":"2026-09-01T14:24:59.842Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}