thanos-io/thanos · error

Failed to set GOMEMLIMIT automatically

Error message

Failed to set GOMEMLIMIT automatically

What it means

When --auto-gomemlimit is enabled, configureGoAutoMemLimit calls memlimit.SetGoMemLimitWithOpts with providers FromCgroup then FromSystem. If all providers fail to determine the container/system memory limit, the wrapped error "Failed to set GOMEMLIMIT automatically" is returned with limits = -1.

Solutions

  1. Verify the runtime exposes memory limits: check /sys/fs/cgroup/memory.max (v2) or /sys/fs/cgroup/memory/memory.limit_in_bytes (v1).
  2. Set GOMEMLIMIT manually as a fallback instead of relying on auto-detection.
  3. Run the container with an explicit memory limit (e.g. docker run --memory=4g) so cgroup providers can read it.

Example fix

// before (no memory limit visible to cgroup)
docker run thanosio/thanos tool ...
// after
docker run --memory=4g thanosio/thanos tool ...
# or set explicitly
docker run -e GOMEMLIMIT=3GiB thanosio/thanos tool ...
Defensive patterns

Strategy: fallback

Validate before calling

func canReadMemLimit() bool {
    for _, p := range []string{"/sys/fs/cgroup/memory.max", "/sys/fs/cgroup/memory/memory.limit_in_bytes"} {
        if _, err := os.Stat(p); err == nil { return true }
    }
    return false
}

Try / catch

// Go
limits, err := configureGoAutoMemLimit(common)
if err != nil {
    logger.Warn("falling back to explicit GOMEMLIMIT", "err", err)
    os.Setenv("GOMEMLIMIT", "3GiB") // or restart with the env set
}

Prevention

When it happens

Trigger: Running with --auto-gomemlimit in an environment where neither cgroup v1/v2 memory files nor system memory can be read (restricted /proc or /sys mounts, unusual sandbox, missing cgroup fs).

Common situations: Containers without cgroup memory limits mounted; hardened runtimes masking /sys/fs/cgroup; older kernels without cgroup memory controller; Windows hosts (system provider unsupported there).

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of thanos-io/thanos@35b8b99117 (2026-09-07). Data as JSON: /api/errors/89cb009117f0a28f. Report an issue: GitHub.

Appendix: source

Thrown at cmd/thanos/config.go:385

		limits int64 = -1
	)

	if common.memlimitRatio <= 0.0 || common.memlimitRatio > 1.0 {
		return limits, errors.New("--auto-gomemlimit.ratio must be greater than 0 and less than or equal to 1.")
	}

	if common.enableAutoGoMemlimit {
		limits, err = memlimit.SetGoMemLimitWithOpts(
			memlimit.WithRatio(common.memlimitRatio),
			memlimit.WithProvider(
				memlimit.ApplyFallback(
					memlimit.FromCgroup,
					memlimit.FromSystem,
				),
			),
		)
		if err != nil {
			return -1, errors.Wrap(err, "Failed to set GOMEMLIMIT automatically")
		}
	}

	return limits, nil
}

View on GitHub (pinned to 35b8b99117)