Repository navigation
Fix: Docker memory guard handles cgroup v2 'max' limit correctly - #2298
Open
dajiaohuang wants to merge 1 commit into
Open
dajiaohuang wants to merge 1 commit into
dajiaohuang wants to merge 1 commit into
Conversation
…guard - unclecode#2123: Parse /sys/fs/cgroup/memory.max before int() conversion; treat literal 'max' as no-limit, using host total RAM as denominator (matching existing cgroup v1 behavior for limit > 1e18). Previously int('max') raised ValueError, silently caught by bare except, causing the guard to report host-wide memory percentage instead of container percentage on unlimited cgroup v2 containers. Also narrow except clause to specific exceptions (OSError, ValueError, ZeroDivisionError).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2123.
On cgroup v2 hosts with no container memory limit (
docker runwithout-m, Kubernetes pods without limits, PaaS defaults),/sys/fs/cgroup/memory.maxcontains the literal stringmax. The previous code calledint(limit_path.read_text())which raisedValueError, silently caught by a bareexcept:, causingget_container_memory_percent()to fall through topsutil.virtual_memory().percent— reporting host-wide memory usage instead of container usage.This meant the memory threshold guard (default 95%) was measuring unrelated host/neighbor container memory, potentially triggering OOM kills prematurely or allowing the container to grow without bound.
Changes
"max"sentinel beforeint()conversion, correctly falling back to host total RAM (as the existing comment and cgroup v1> 1e18branch intended).except:to specific exceptions:OSError,ValueError,ZeroDivisionErrorto prevent silent failures on unexpected parse errors.import psutilto the top of the try block to avoid duplicate imports.Validation
memory.maxcontainsmax, usage is now correctly divided by host total RAM (matching cgroup v1 unlimited behavior).