hashicorp/nomad · error

error decoding stats data: stats were nil

Error message

error decoding stats data: stats were nil

What it means

After successfully decoding the Docker stats API response into a DockerStats struct, collectDockerStats checks that the result is non-nil. A nil decoded value means the daemon returned an empty/JSON-null payload, so the driver reports this error rather than propagating a useless nil stats object.

Source

Thrown at drivers/docker/stats.go:156

	})
	if err != nil && err != io.EOF {
		return nil, fmt.Errorf("failed to collect stats: %w", err)
	}

	// Ensure the body is not nil to avoid potential panics. The statsReader
	// itself cannot be nil, so there is no need to check this.
	if statsReader.Body == nil {
		return nil, errors.New("error decoding stats data: no reader body")
	}

	err = json.NewDecoder(statsReader.Body).Decode(&stats)
	_ = statsReader.Body.Close()
	if err != nil && err != io.EOF {
		return nil, fmt.Errorf("failed to decode Docker response: %w", err)
	}

	if stats == nil {
		return nil, errors.New("error decoding stats data: stats were nil")
	}

	return stats, nil
}

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Confirm the container is still running; handle stop/remove races before stats collection
  2. Retry the collection — transient daemon responses usually resolve
  3. Upgrade/restart the Docker daemon if null stats responses persist
Defensive patterns

Strategy: retry

Validate before calling

// verify container is running before decoding stats
info, err := cli.ContainerInspect(ctx, containerID)
if err != nil || !info.State.Running { return fmt.Errorf("container not running") }

Type guard

func statsDecoded(s *DockerStats) bool { return s != nil }

Try / catch

stats, err := collectDockerStats(ctx, cli, containerID)
if err != nil {
    if strings.Contains(err.Error(), "stats were nil") {
        return retryWithBackoff(3, func() (interface{}, error) { return collectDockerStats(ctx, cli, containerID) })
    }
    return nil, err
}

Prevention

When it happens

Trigger: Docker's stats endpoint returns a null/empty JSON body (decode succeeds, err is nil or io.EOF) and the resulting stats struct is nil during collectStats.

Common situations: Container stopped/removed between request and decode; Docker daemon returning `{}`/null on transient failures; odd pre-1.x daemon behaviors during host memory pressure.

Related errors


AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04). Data as JSON: /api/errors/3175e736ae3a1853. Report an issue: GitHub.