cloudflare/cloudflared · error

error retrieving output from command '%s': %w

Error message

error retrieving output from command '%s': %w

What it means

This error wraps a failure of `exec.CommandContext(ctx, "sysctl", ...).Output()` when collecting file descriptor limits on macOS. The library runs `sysctl kern.maxfiles kern.maxfilesperproc` and expects a zero exit code; if sysctl is missing, errors, or is killed (e.g. by context cancellation), the raw error is wrapped with the full command string via `%w` so callers can unwrap with errors.As/Is.

Source

Thrown at diagnostic/system_collector_macos.go:115

	return info, err
}

func collectFileDescriptorInformation(ctx context.Context) (
	*FileDescriptorInformation,
	string,
	error,
) {
	const (
		fileDescriptorMaximumKey = "kern.maxfiles"
		fileDescriptorCurrentKey = "kern.num_files"
	)

	command := exec.CommandContext(ctx, "sysctl", fileDescriptorMaximumKey, fileDescriptorCurrentKey)

	stdout, err := command.Output()
	if err != nil {
		return nil, "", fmt.Errorf("error retrieving output from command '%s': %w", command.String(), err)
	}

	output := string(stdout)

	fileDescriptorInfo, err := ParseFileDescriptorInformationFromKV(
		output,
		fileDescriptorMaximumKey,
		fileDescriptorCurrentKey,
	)
	if err != nil {
		return nil, output, err
	}

	// returning raw output in case other collected information
	// resulted in errors
	return fileDescriptorInfo, output, nil
}

View on GitHub (pinned to 2253eeeb25)

Solutions

  1. Verify `sysctl kern.maxfiles kern.maxfilesperproc` succeeds in a shell on the affected host; fix PATH or restore the binary if it fails.
  2. Check the wrapped error (`errors.Unwrap` / `%w` chain) — exec.ExitError means sysctl ran and failed, exec.ErrNotFound means it was not found on PATH.
  3. Ensure the context passed to Collect has enough budget for the sysctl call; increase the timeout if context.DeadlineExceeded is the cause.
  4. If the host is not macOS or lacks these sysctls, skip macOS-specific collection or run the collector on a supported platform.

Example fix

// before
stdout, err := command.Output()
if err != nil {
	return nil, "", fmt.Errorf("error retrieving output from command '%s': %w", command.String(), err)
}
// after
stdout, err := command.Output()
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
	return nil, "", fmt.Errorf("error retrieving output from command '%s': %w, stderr: %s", command.String(), err, exitErr.Stderr)
}
if err != nil {
	return nil, "", fmt.Errorf("error retrieving output from command '%s': %w", command.String(), err)
}
Defensive patterns

Strategy: try-catch

Validate before calling

if runtime.GOOS != "darwin" {
	return fmt.Errorf("file descriptor collection is macOS-only")
}
if _, err := exec.LookPath("sysctl"); err != nil {
	return fmt.Errorf("sysctl not available: %w", err)
}
if err := ctx.Err(); err != nil {
	return fmt.Errorf("context already cancelled: %w", err)
}

Type guard

var exitErr *exec.ExitError
if errors.As(err, &exitErr) { /* sysctl ran and failed; inspect exitErr.Stderr */ }

Try / catch

info, extra, err := collector.Collect(ctx)
if err != nil {
	if errors.Is(err, exec.ErrNotFound) {
		log.Warn().Msg("sysctl missing; skipping fd diagnostics")
	} else if errors.Is(err, context.DeadlineExceeded) {
		log.Warn().Msg("fd diagnostics timed out")
	} else {
		return fmt.Errorf("fd diagnostics failed: %w", err)
	}
}

Prevention

When it happens

Trigger: Calling Collect() on a macOS host when: the `sysctl` binary is not on PATH; kern.maxfiles / kern.maxfilesperproc are unavailable on the kernel; the passed context is cancelled before Output() returns; or the child process exits non-zero for any reason.

Common situations: Running in a sandboxed/limited container that stubs or removes sysctl, minimal macOS environments (e.g. CI images) with stripped-down /usr/sbin PATH, or diagnostic collection invoked with a short-lived context that times out while the command runs.

Related errors


AI-assisted analysis of cloudflare/cloudflared@2253eeeb25 (2026-09-06). Data as JSON: /api/errors/3dccb0d59b8baaa6. Report an issue: GitHub.