{"record":{"id":"f5c46b6cb939b7e0","repo":"lima-vm/lima","slug":"failed-to-unmarshal-service-object-w-line-q","errorCode":null,"errorMessage":"failed to unmarshal service object: %w (line=%#q)","messagePattern":"failed to unmarshal service object: %w \\(line=%#q\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/guestagent/kubernetesservice/kubernetesservice.go","lineNumber":173,"sourceCode":"\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()\n\t}\n\n\tif err := scanner.Err(); err != nil {\n\t\treturn fmt.Errorf(\"failed to scan kubectl event stream: %w\", err)\n\t}","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/lima-vm/lima/blob/dd909d0973cd84fa35f9e1693181b4585ea616c1/pkg/guestagent/kubernetesservice/kubernetesservice.go#L155-L191","documentation":"After the outer watch-event envelope unmarshals, the `object` field must decode into the internal `service` struct (metadata.namespace, metadata.name, spec ports, etc.). If the object payload does not match that shape, this error is thrown including the decode error and raw line. It means the JSON structure deviates from the expected Kubernetes Service schema the watcher models.","triggerScenarios":"readKubectlStream receives a watch event whose `object` fails to unmarshal into `service` (e.g. a DeletedEvent/NonResource payload, an object with unexpected types like null metadata where strings are expected).","commonSituations":"kubectl watch emitting non-Service objects due to wrong resource/selector flags; custom or aggregated API objects with divergent schema; newer kubectl output shape changes.","solutions":["Check the embedded decode error and raw line to see which field mismatches.","Confirm the kubectl command watches only Service objects (correct resource and `-o json` flags).","Compare the object's apiVersion/kind against the expected Service schema.","Pin or adjust the watcher's service struct fields to match the kubectl/API server version's schema."],"exampleFix":"// before: struct lacks a field whose type changed\nvar svc service\nif err := json.Unmarshal(ev.Object, &svc); err != nil { ... }\n// after: tolerant decoding of metadata\nvar svc service\ntype envelopeMeta struct {\n\tMetadata struct {\n\t\tName      string `json:\"name\"`\n\t\tNamespace string `json:\"namespace\"`\n\t} `json:\"metadata\"`\n}\nif err := json.Unmarshal(ev.Object, &svc); err != nil {\n\treturn fmt.Errorf(\"failed to unmarshal service object: %w (line=%#q)\", err, line)\n}","handlingStrategy":"validation","validationCode":"// verify the event object kind before decoding into service\nvar probe struct {\n\tKind string `json:\"kind\"`\n}\nif err := json.Unmarshal(ev.Object, &probe); err != nil || probe.Kind != \"Service\" {\n\treturn nil // ignore non-Service objects\n}","typeGuard":null,"tryCatchPattern":"if err := json.Unmarshal(ev.Object, &svc); err != nil {\n\tlog.WithError(err).Warnf(\"unrecognized service object, skipping line %s\", line)\n\tcontinue\n}","preventionTips":["Scope the kubectl watch to Service resources only.","Keep the internal service struct in sync with the cluster's API version.","Ignore objects whose kind/apiVersion differs from Service.","Add fuzz tests with real kubectl watch payloads."],"tags":["guestagent","json","schema","kubernetes"],"backgroundTag":"schema-validation-failed","analyzedSha":"dd909d0973cd84fa35f9e1693181b4585ea616c1","analyzedAt":"2026-09-01T14:24:59.842Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}