lima-vm/lima · error

failed to scan kubectl event stream: %w

Error message

failed to scan kubectl event stream: %w

What it means

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.

Source

Thrown at pkg/guestagent/kubernetesservice/kubernetesservice.go:190

		if err := json.Unmarshal(ev.Object, &svc); err != nil {
			return fmt.Errorf("failed to unmarshal service object: %w (line=%#q)", err, line)
		}

		key := svc.Metadata.Namespace + "/" + svc.Metadata.Name
		s.rwMutex.Lock()
		switch ev.Type {
		case added, modified:
			s.serviceSpecs[key] = &svc.Spec
		case deleted:
			delete(s.serviceSpecs, key)
		default:
			// NOP
		}
		s.rwMutex.Unlock()
	}

	if err := scanner.Err(); err != nil {
		return fmt.Errorf("failed to scan kubectl event stream: %w", err)
	}
	return nil
}

func (s *ServiceWatcher) GetPorts() []Entry {
	s.rwMutex.RLock()
	defer s.rwMutex.RUnlock()

	var entries []Entry
	for key, spec := range s.serviceSpecs {
		if spec.Type != serviceTypeNodePort &&
			spec.Type != serviceTypeLoadBalancer {
			continue
		}

		for _, portEntry := range spec.Ports {
			switch portEntry.Protocol {
			case protocolTCP, protocolUDP:

View on GitHub (pinned to dd909d0973)

Solutions

  1. Increase the scanner buffer: scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024) before the scan loop.
  2. Reduce Service annotation/status size on the cluster if lines are genuinely huge.
  3. Check for pipe/I/O errors underlying the scan failure and restart the kubectl watcher.

Example fix

// before
canner := bufio.NewScanner(stdout)
// after
scanner := bufio.NewScanner(stdout)
scanner.Buffer(make([]byte, 0, 64*1024), 4*1024*1024)
Defensive patterns

Strategy: fallback

Validate before calling

// ensure the scanner can handle large lines before scanning
scanner := bufio.NewScanner(stdout)
scanner.Buffer(make([]byte, 0, 64*1024), 4*1024*1024)

Try / catch

if err := readKubectlStream(stdout); err != nil {
	if errors.Is(err, bufio.ErrTooLong) || strings.Contains(err.Error(), "failed to scan kubectl event stream") {
		log.WithError(err).Warn("event stream scan failed; restarting watcher")
		go restartWatcher()
		return nil
	}
	return err
}

Prevention

When it happens

Trigger: 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).

Common situations: Services with massive annotation payloads (e.g. ELB status annotations) producing watch lines > 64KB; I/O error on the pipe to kubectl.

Related errors


AI-assisted analysis of lima-vm/lima@dd909d0973 (2026-09-01). Data as JSON: /api/errors/1fcf080e67b64d73. Report an issue: GitHub.