{"record":{"id":"1fcf080e67b64d73","repo":"lima-vm/lima","slug":"failed-to-scan-kubectl-event-stream-w","errorCode":null,"errorMessage":"failed to scan kubectl event stream: %w","messagePattern":"failed to scan kubectl event stream: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/guestagent/kubernetesservice/kubernetesservice.go","lineNumber":190,"sourceCode":"\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}\n\treturn nil\n}\n\nfunc (s *ServiceWatcher) GetPorts() []Entry {\n\ts.rwMutex.RLock()\n\tdefer s.rwMutex.RUnlock()\n\n\tvar entries []Entry\n\tfor key, spec := range s.serviceSpecs {\n\t\tif spec.Type != serviceTypeNodePort &&\n\t\t\tspec.Type != serviceTypeLoadBalancer {\n\t\t\tcontinue\n\t\t}\n\n\t\tfor _, portEntry := range spec.Ports {\n\t\t\tswitch portEntry.Protocol {\n\t\t\tcase protocolTCP, protocolUDP:","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/lima-vm/lima/blob/dd909d0973cd84fa35f9e1693181b4585ea616c1/pkg/guestagent/kubernetesservice/kubernetesservice.go#L172-L208","documentation":"The bufio.Scanner used to read the kubectl JSON watch stream returned a non-EOF error, such as a line exceeding the scanner's token buffer size. The stream reader aborts and this error is propagated to startAndStreamKubectl.","triggerScenarios":"readKubectlStream when scanner.Scan() sets scanner.Err(), most commonly bufio.ErrTooLong from a JSON line larger than the default 64KB scanner buffer (a Service with a very large spec/status, huge annotations).","commonSituations":"Services with massive annotation payloads (e.g. ELB status annotations) producing watch lines > 64KB; I/O error on the pipe to kubectl.","solutions":["Increase the scanner buffer: scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024) before the scan loop.","Reduce Service annotation/status size on the cluster if lines are genuinely huge.","Check for pipe/I/O errors underlying the scan failure and restart the kubectl watcher."],"exampleFix":"// before\ncanner := bufio.NewScanner(stdout)\n// after\nscanner := bufio.NewScanner(stdout)\nscanner.Buffer(make([]byte, 0, 64*1024), 4*1024*1024)","handlingStrategy":"fallback","validationCode":"// ensure the scanner can handle large lines before scanning\nscanner := bufio.NewScanner(stdout)\nscanner.Buffer(make([]byte, 0, 64*1024), 4*1024*1024)","typeGuard":null,"tryCatchPattern":"if err := readKubectlStream(stdout); err != nil {\n\tif errors.Is(err, bufio.ErrTooLong) || strings.Contains(err.Error(), \"failed to scan kubectl event stream\") {\n\t\tlog.WithError(err).Warn(\"event stream scan failed; restarting watcher\")\n\t\tgo restartWatcher()\n\t\treturn nil\n\t}\n\treturn err\n}","preventionTips":["Always raise the scanner buffer above the default 64KB for kubectl output.","Avoid giant Service annotations in the watched cluster.","Restart the watcher on scan errors instead of dying permanently.","Monitor for recurring scan failures indicating oversized objects."],"tags":["guestagent","bufio","stream","kubernetes"],"backgroundTag":"bufio-scanner-token-too-long","analyzedSha":"dd909d0973cd84fa35f9e1693181b4585ea616c1","analyzedAt":"2026-09-01T14:24:59.842Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}