From 6a2f29635e1bce402d403d71b87260acba7827de Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sat, 24 Oct 2026 11:42:53 +0200 Subject: [PATCH] =?UTF-8?q?Answer=20stability=20round=202:=201=20?= =?UTF-8?q?=F0=9F=94=B4,=202=20=F0=9F=9F=A1=20(0=20misread);=20drop=20a=20?= =?UTF-8?q?--memory=20of=200=20so=20the=20added=20limit=20stands,=20pin=20?= =?UTF-8?q?the=206m=20floor's=20edge,=20test=20env=20--unset,=20env=20--ig?= =?UTF-8?q?nore-environment=20and=20doas,=20and=20say=20on=20Linux=20in=20?= =?UTF-8?q?the=20rule=20text?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/docker_command.py | 9 +++++++-- bin/rule_hook/rules.py | 2 +- bin/rule_hook/test_rule_hook.py | 2 +- bin/run_heavy/main.py | 2 +- bin/test_docker_command.py | 4 ++++ 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/bin/docker_command.py b/bin/docker_command.py index 8103d105..4db69996 100644 --- a/bin/docker_command.py +++ b/bin/docker_command.py @@ -40,6 +40,11 @@ def unset_swap(value: str) -> bool: return value == "-1" or size(value) == 0 +def zero(value: str) -> bool: + """Whether a value is 0 in any unit: a -m 0 leaves memory unlimited, and would override the limit capped adds.""" + return size(value) == 0 + + def docker_subcommand(arguments: list[str]) -> list[str]: """The subcommand docker's arguments name, past its options: such as ["build"], ["compose", "run"] or ["buildx", "build"].""" return [arguments[at] for at in subcommand_at(arguments)] @@ -80,13 +85,13 @@ def helped(arguments: list[str]) -> bool: def capped(arguments: list[str], share: str) -> list[str]: """docker's arguments, with each limit a container they make lacks added right after the subcommand. - A container that sets neither limit gets share for both. One that sets one limit gets the other at the same size, since docker refuses less swap than memory. A --memory-swap of -1 or 0 is dropped; every other word stays. + A container that sets neither limit gets share for both. One that sets one limit gets the other at the same size, since docker refuses less swap than memory. A --memory-swap of -1 or 0, and a --memory of 0, is dropped; every other word stays. """ if not unlimited(arguments): return arguments memory = option_value(arguments, MEMORY_OPTIONS, memory_limit) swap = option_value(arguments, SWAP_OPTIONS, swap_limit) - rest = without(arguments, SWAP_OPTIONS, unset_swap) + rest = without(without(arguments, SWAP_OPTIONS, unset_swap), MEMORY_OPTIONS, zero) limits = ([] if memory else ["--memory", swap or share]) + ([] if swap else ["--memory-swap", memory or share]) after = subcommand_at(rest)[-1] + 1 return [*rest[:after], *limits, *rest[after:]] diff --git a/bin/rule_hook/rules.py b/bin/rule_hook/rules.py index 42b42f11..c837c979 100644 --- a/bin/rule_hook/rules.py +++ b/bin/rule_hook/rules.py @@ -32,7 +32,7 @@ RULES: dict[RuleName, Rule] = { ), "docker-run-memory": Rule( "a container made with docker run or docker create has a memory limit and a swap limit, as large as its work needs, such as `--memory 4g --memory-swap 4g`, so a runaway container fails alone instead of starving the machine; a swap limit equal to the memory limit keeps it out of swap", - "docker run, docker create, docker container run and docker container create, whether direct or through a launcher such as nice or timeout. One under run-heavy passes, as run-heavy's docker adds the limits, unless it names docker by its path, or changes or clears PATH, as env -i, sudo or doas do. A container a script or docker compose starts is not checked", + "docker run, docker create, docker container run and docker container create, whether direct or through a launcher such as nice or timeout. One under run-heavy passes, as run-heavy's docker adds the limits on Linux, unless it names docker by its path, or changes or clears PATH, as env -i, sudo or doas do. A container a script or docker compose starts is not checked", ), "ignorefile-sorted": Rule( "each block of an ignore file is sorted in code-point order, as LC_ALL=C sort does, since its order carries no meaning; where order matters, such as a re-include after its exclude, split the block with a blank line or a comment", diff --git a/bin/rule_hook/test_rule_hook.py b/bin/rule_hook/test_rule_hook.py index 26399a4e..82f04339 100644 --- a/bin/rule_hook/test_rule_hook.py +++ b/bin/rule_hook/test_rule_hook.py @@ -117,7 +117,7 @@ class CommandBreaches(unittest.TestCase): self.assertEqual(checked(command), []) def test_a_docker_run_or_create_without_a_memory_limit_breaks_docker_run_memory(self): - for command in ["docker run --rm node:24.22.1-alpine3.24 node x.js", "cd /w && docker run -v /w:/w alpine:3.22.2 true", "docker create alpine:3.22.2", "docker container run alpine:3.22.2 true", "docker container create alpine:3.22.2", "nice -n 19 docker run alpine:3.22.2 true", "docker -H unix:///d.sock run alpine:3.22.2 true", "docker run --memory-swap 4g alpine:3.22.2 true", "docker run -m 4g alpine:3.22.2 true", "docker run --memory=4g alpine:3.22.2 sh -c 'x --memory-swap 4g'", "docker run -m 4g alpine:3.22.2 tool --memory-swap", "docker run -m 4g --memory-swap -1 alpine:3.22.2 true", "docker run -m 4g --memory-swap=0 alpine:3.22.2 true", "docker run --rm python:3.13.1 python -m pytest", "docker run alpine:3.22.2 git commit -m x", "timeout 60 docker run alpine:3.22.2 true", "docker run node:24.22.1-alpine3.24 node --help", "docker run alpine:3.22.2 --help", "docker run -m 4g alpine:3.22.2 sleep 60", "~/.claude/bin/run-heavy /usr/bin/docker run alpine:3.22.2 true", "docker run --memory-swap 4g alpine:3.22.2 grep -m 1 x", "run-heavy env PATH=/usr/bin docker run alpine:3.22.2 true", "run-heavy sudo docker run alpine:3.22.2 true", "docker run alpine:3.22.2 git tag -m 1.2.3", "run-heavy env -i docker run alpine:3.22.2 true", "run-heavy env -u PATH docker run alpine:3.22.2 true"]: + for command in ["docker run --rm node:24.22.1-alpine3.24 node x.js", "cd /w && docker run -v /w:/w alpine:3.22.2 true", "docker create alpine:3.22.2", "docker container run alpine:3.22.2 true", "docker container create alpine:3.22.2", "nice -n 19 docker run alpine:3.22.2 true", "docker -H unix:///d.sock run alpine:3.22.2 true", "docker run --memory-swap 4g alpine:3.22.2 true", "docker run -m 4g alpine:3.22.2 true", "docker run --memory=4g alpine:3.22.2 sh -c 'x --memory-swap 4g'", "docker run -m 4g alpine:3.22.2 tool --memory-swap", "docker run -m 4g --memory-swap -1 alpine:3.22.2 true", "docker run -m 4g --memory-swap=0 alpine:3.22.2 true", "docker run --rm python:3.13.1 python -m pytest", "docker run alpine:3.22.2 git commit -m x", "timeout 60 docker run alpine:3.22.2 true", "docker run node:24.22.1-alpine3.24 node --help", "docker run alpine:3.22.2 --help", "docker run -m 4g alpine:3.22.2 sleep 60", "~/.claude/bin/run-heavy /usr/bin/docker run alpine:3.22.2 true", "docker run --memory-swap 4g alpine:3.22.2 grep -m 1 x", "run-heavy env PATH=/usr/bin docker run alpine:3.22.2 true", "run-heavy sudo docker run alpine:3.22.2 true", "docker run alpine:3.22.2 git tag -m 1.2.3", "run-heavy env -i docker run alpine:3.22.2 true", "run-heavy env -u PATH docker run alpine:3.22.2 true", "run-heavy env --unset=PATH docker run alpine:3.22.2 true", "run-heavy env --ignore-environment docker run alpine:3.22.2 true", "run-heavy doas docker run alpine:3.22.2 true"]: with self.subTest(command): self.assertEqual(checked(command), ["docker-run-memory"]) diff --git a/bin/run_heavy/main.py b/bin/run_heavy/main.py index a7ee6e52..44176e39 100644 --- a/bin/run_heavy/main.py +++ b/bin/run_heavy/main.py @@ -16,7 +16,7 @@ first on the command's PATH. That docker adds the limits a container lacks, then - A container that sets neither limit gets a memory limit and a swap limit, both the share. - A container that sets one of them keeps it, and gets the other at the same size. -- A --memory-swap of -1, unlimited swap, is dropped and replaced. +- A --memory-swap of -1, unlimited swap, or a --memory of 0 is dropped and replaced. Every other docker call goes through unchanged. A docker compose service, and a docker named by its full path, get no cap: give those containers their own limits. diff --git a/bin/test_docker_command.py b/bin/test_docker_command.py index fcdc0b97..1845e291 100644 --- a/bin/test_docker_command.py +++ b/bin/test_docker_command.py @@ -13,6 +13,9 @@ class Capped(unittest.TestCase): (["run", "img", "python", "-m", "pytest"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "img", "python", "-m", "pytest"]), (["run", "img", "grep", "-m", "1", "x"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "img", "grep", "-m", "1", "x"]), (["run", "img", "git", "tag", "-m", "1.2.3"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "img", "git", "tag", "-m", "1.2.3"]), + (["run", "-m", "0", "img"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "img"]), + (["run", "-m", "6143k", "img"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "-m", "6143k", "img"]), + (["run", "img", "tool", "-m", "6"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "img", "tool", "-m", "6"]), (["run", "img", "stress", "--memory", "2"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "img", "stress", "--memory", "2"]), ]: with self.subTest(given): @@ -21,6 +24,7 @@ class Capped(unittest.TestCase): def test_a_container_s_own_limit_is_kept_and_sets_the_limit_it_lacks(self): for given, expected in [ (["run", "-m", "10g", "img"], ["run", "--memory-swap", "10g", "-m", "10g", "img"]), + (["run", "-m", "6m", "img"], ["run", "--memory-swap", "6m", "-m", "6m", "img"]), (["run", "--memory=12g", "img"], ["run", "--memory-swap", "12g", "--memory=12g", "img"]), (["run", "--memory-swap", "2g", "img"], ["run", "--memory", "2g", "--memory-swap", "2g", "img"]), (["run", "-m", "4g", "--memory-swap", "-1", "img"], ["run", "--memory-swap", "4g", "-m", "4g", "img"]),