{"record":{"id":"89cb009117f0a28f","repo":"thanos-io/thanos","slug":"failed-to-set-gomemlimit-automatically","errorCode":null,"errorMessage":"Failed to set GOMEMLIMIT automatically","messagePattern":"Failed to set GOMEMLIMIT automatically","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/thanos/config.go","lineNumber":385,"sourceCode":"\t\tlimits int64 = -1\n\t)\n\n\tif common.memlimitRatio <= 0.0 || common.memlimitRatio > 1.0 {\n\t\treturn limits, errors.New(\"--auto-gomemlimit.ratio must be greater than 0 and less than or equal to 1.\")\n\t}\n\n\tif common.enableAutoGoMemlimit {\n\t\tlimits, err = memlimit.SetGoMemLimitWithOpts(\n\t\t\tmemlimit.WithRatio(common.memlimitRatio),\n\t\t\tmemlimit.WithProvider(\n\t\t\t\tmemlimit.ApplyFallback(\n\t\t\t\t\tmemlimit.FromCgroup,\n\t\t\t\t\tmemlimit.FromSystem,\n\t\t\t\t),\n\t\t\t),\n\t\t)\n\t\tif err != nil {\n\t\t\treturn -1, errors.Wrap(err, \"Failed to set GOMEMLIMIT automatically\")\n\t\t}\n\t}\n\n\treturn limits, nil\n}\n","sourceCodeStart":367,"sourceCodeEnd":391,"githubUrl":"https://github.com/thanos-io/thanos/blob/35b8b991177def87ed52dcf10f9b6d87f07282c8/cmd/thanos/config.go#L367-L391","documentation":"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.","triggerScenarios":"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).","commonSituations":"Containers without cgroup memory limits mounted; hardened runtimes masking /sys/fs/cgroup; older kernels without cgroup memory controller; Windows hosts (system provider unsupported there).","solutions":["Verify the runtime exposes memory limits: check /sys/fs/cgroup/memory.max (v2) or /sys/fs/cgroup/memory/memory.limit_in_bytes (v1).","Set GOMEMLIMIT manually as a fallback instead of relying on auto-detection.","Run the container with an explicit memory limit (e.g. docker run --memory=4g) so cgroup providers can read it."],"exampleFix":"// before (no memory limit visible to cgroup)\ndocker run thanosio/thanos tool ...\n// after\ndocker run --memory=4g thanosio/thanos tool ...\n# or set explicitly\ndocker run -e GOMEMLIMIT=3GiB thanosio/thanos tool ...","handlingStrategy":"fallback","validationCode":"func canReadMemLimit() bool {\n    for _, p := range []string{\"/sys/fs/cgroup/memory.max\", \"/sys/fs/cgroup/memory/memory.limit_in_bytes\"} {\n        if _, err := os.Stat(p); err == nil { return true }\n    }\n    return false\n}","typeGuard":null,"tryCatchPattern":"// Go\nlimits, err := configureGoAutoMemLimit(common)\nif err != nil {\n    logger.Warn(\"falling back to explicit GOMEMLIMIT\", \"err\", err)\n    os.Setenv(\"GOMEMLIMIT\", \"3GiB\") // or restart with the env set\n}","preventionTips":["Run containers with an explicit memory limit so cgroup providers can read it.","Pre-set GOMEMLIMIT in environments that restrict /proc or /sys access.","Smoke-test memory-limit detection in the target sandbox before production rollout."],"tags":["gomemlimit","runtime","containers","config"],"backgroundTag":"missing-env-var","analyzedSha":"35b8b991177def87ed52dcf10f9b6d87f07282c8","analyzedAt":"2026-09-07T01:49:59.689Z","contentChangedAt":"2026-09-07T01:49:59.689Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}