mirror of
https://github.com/usestrix/strix.git
synced 2026-08-16 01:16:40 +02:00
feat(runtime): opt-in resource limits for docker sandbox containers
Apply cgroup caps (mem_limit, shm_size, nano_cpus, pids_limit) to the sandbox container from STRIX_SANDBOX_* env vars. Unset values keep docker's unbounded default, so behavior is unchanged unless opted in.
This commit is contained in:
@@ -65,6 +65,30 @@ def _apply_sandbox_network(create_kwargs: dict[str, Any]) -> None:
|
|||||||
create_kwargs.pop("ports", None)
|
create_kwargs.pop("ports", None)
|
||||||
|
|
||||||
|
|
||||||
|
def _apply_resource_limits(create_kwargs: dict[str, Any]) -> None:
|
||||||
|
"""Apply optional cgroup resource caps from the environment. Unset/blank
|
||||||
|
values leave docker's default (unbounded), so this is opt-in per host."""
|
||||||
|
mem_limit = os.environ.get("STRIX_SANDBOX_MEM_LIMIT", "").strip()
|
||||||
|
if mem_limit:
|
||||||
|
create_kwargs["mem_limit"] = mem_limit
|
||||||
|
|
||||||
|
shm_size = os.environ.get("STRIX_SANDBOX_SHM_SIZE", "").strip()
|
||||||
|
if shm_size:
|
||||||
|
create_kwargs["shm_size"] = shm_size
|
||||||
|
|
||||||
|
cpus = os.environ.get("STRIX_SANDBOX_CPUS", "").strip()
|
||||||
|
if cpus:
|
||||||
|
with contextlib.suppress(ValueError):
|
||||||
|
nano_cpus = int(float(cpus) * 1_000_000_000)
|
||||||
|
if nano_cpus > 0:
|
||||||
|
create_kwargs["nano_cpus"] = nano_cpus
|
||||||
|
|
||||||
|
pids_limit = os.environ.get("STRIX_SANDBOX_PIDS_LIMIT", "").strip()
|
||||||
|
if pids_limit:
|
||||||
|
with contextlib.suppress(ValueError):
|
||||||
|
create_kwargs["pids_limit"] = int(pids_limit)
|
||||||
|
|
||||||
|
|
||||||
class StrixDockerSandboxSession(DockerSandboxSession):
|
class StrixDockerSandboxSession(DockerSandboxSession):
|
||||||
sandbox_network: str = ""
|
sandbox_network: str = ""
|
||||||
|
|
||||||
@@ -175,6 +199,7 @@ class StrixDockerSandboxClient(DockerSandboxClient):
|
|||||||
extra_hosts["host.docker.internal"] = "host-gateway"
|
extra_hosts["host.docker.internal"] = "host-gateway"
|
||||||
|
|
||||||
_apply_sandbox_network(create_kwargs)
|
_apply_sandbox_network(create_kwargs)
|
||||||
|
_apply_resource_limits(create_kwargs)
|
||||||
|
|
||||||
# Strix injection: host bind mounts (e.g. large repos passed via --mount)
|
# Strix injection: host bind mounts (e.g. large repos passed via --mount)
|
||||||
# that bypass the SDK's file-by-file LocalDir copy.
|
# that bypass the SDK's file-by-file LocalDir copy.
|
||||||
|
|||||||
Reference in New Issue
Block a user