"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(runner): apply sandbox rlimits after exec, not in a forked child (#1355)
Haozhe Wu87a289814ac6
diff --git a/agent/src/core/runner.py b/agent/src/core/runner.pyindex 2e01cd273..74b01a7fb 100644--- a/agent/src/core/runner.py+++ b/agent/src/core/runner.py@@ -12,7 +12,7 @@ import tempfileimport timefrom dataclasses import dataclassfrom pathlib import Path-from typing import Any, Callable, Dict, Optional+from typing import Any, Dict, Optionalfrom rich.console import Console@@ -82,32 +82,66 @@ def _rlimit_as_bytes() -> int:return mb * 1024 * 1024-def _make_rlimit_preexec() -> Callable[[], None] | None:- """Build a POSIX ``preexec_fn`` that caps subprocess address space + fds.+# Applied by the freshly-exec'd child, NOT by a ``preexec_fn``. ``preexec_fn``+# runs Python bytecode between fork() and exec(); when the parent is+# multi-threaded — which ``vibe-trading serve`` always is (uvicorn workers plus+# the background agent loops) — that is undefined behaviour per POSIX and+# CPython documents it as unsafe. On aarch64/glibc 2.34 it reliably SIGSEGVs the+# child, so every backtest launched from the Web UI died before exec (#1355).+# Running the same setrlimit calls after exec, in a single-threaded interpreter,+# is safe on every platform and keeps the identical ceiling.+#+# argv contract: ``python -c BOOTSTRAP <as_bytes> <nofile> <entry> [args...]``.+_SANDBOX_RLIMIT_BOOTSTRAP = """\+import os, runpy, sys+_as_bytes, _nofile = int(sys.argv[1]), int(sys.argv[2])+sys.argv = sys.argv[3:]+try:+ import resource+except ImportError:+ resource = None+if resource is not None:+ for _res, _target in (+ (resource.RLIMIT_AS, _as_bytes),+ (resource.RLIMIT_NOFILE, _nofile),+ ):+ try:+ _soft, _hard = resource.getrlimit(_res)+ _new_hard = _target if _hard == resource.RLIM_INFINITY else min(_target, _hard)+ resource.setrlimit(_res, (min(_target, _new_hard), _new_hard))+ except (ValueError, OSError):+ pass+sys.path.insert(0, os.path.dirname(os.path.abspath(sys.argv[0])))+runpy.run_path(sys.argv[0], run_name="__main__")+"""+++def _rlimit_bootstrap_argv() -> list[str] | None:+ """Return the ``-c`` bootstrap argv prefix that caps address space + fds.++ Returns None on Windows (no ``resource`` module), where the caller runs the+ entry script directly and no ceiling is applied — unchanged from before.++ The two limits are resolved in the parent and passed as literal argv values+ so the env parsing keeps a single source of truth (``_rlimit_as_bytes``).+ The bootstrap lowers limits best-effort: a hardened parent limit already+ below the target is never raised, and it runs after any UID drop, where+ lowering is always permitted for an unprivileged process. It then puts the+ entry script's own directory on sys.path, which ``python script.py`` does+ for free and ``python -c`` does not; the cwd entry ``-c`` leaves behind is+ harmless because execute() already exports an explicit cwd on PYTHONPATH.- Returns None on Windows (no ``resource`` module). The closure runs in the- forked child after any UID drop; lowering rlimits is always permitted for an- unprivileged process, and each limit is applied best-effort so a hardened- parent limit already below the target is never raised.+ Returns:+ ``["-c", <bootstrap>, <as_bytes>, <nofile>]`` on POSIX, else None."""if resource is None:return None-- as_bytes = _rlimit_as_bytes()-- def _apply_limits() -> None: # pragma: no cover - runs in forked child- for res, target in (- (resource.RLIMIT_AS, as_bytes),- (resource.RLIMIT_NOFILE, _SANDBOX_RLIMIT_NOFILE),- ):- try:- _soft, hard = resource.getrlimit(res)- new_hard = target if hard == resource.RLIM_INFINITY else min(target, hard)- resource.setrlimit(res, (min(target, new_hard), new_hard))- except (ValueError, OSError):- pass-- return _apply_limits+ return [+ "-c",+ _SANDBOX_RLIMIT_BOOTSTRAP,+ str(_rlimit_as_bytes()),+ str(_SANDBOX_RLIMIT_NOFILE),+ ]def _prepare_sandbox_home(real_home: Path | None) -> Path:@@ -556,7 +590,10 @@ class Runner:python_cmd = self._pick_python_interpreter()console.print(f"[dim]Runner: using Python: {python_cmd}[/dim]")- cmd = [python_cmd, str(entry_script)]+ # The rlimit ceiling is applied by the exec'd interpreter itself, never+ # by a preexec_fn — see _SANDBOX_RLIMIT_BOOTSTRAP for why (#1355).+ bootstrap = _rlimit_bootstrap_argv()+ cmd = [python_cmd, *(bootstrap or []), str(entry_script)]if cli_args:cmd.extend(cli_args)@@ -601,10 +638,6 @@ class Runner:encoding="utf-8",errors="ignore",)- preexec = _make_rlimit_preexec()- if preexec is not None:- run_kwargs["preexec_fn"] = preexec-try:process = self._run_sandboxed(cmd, run_kwargs)finally:diff --git a/agent/tests/test_runner_env.py b/agent/tests/test_runner_env.pyindex 131a2284b..2f388463e 100644--- a/agent/tests/test_runner_env.py+++ b/agent/tests/test_runner_env.py@@ -10,7 +10,7 @@ import pytestfrom src.core import runner as runner_modfrom src.core.runner import (Runner,- _make_rlimit_preexec,+ _rlimit_bootstrap_argv,_prepare_sandbox_home,_resolve_sandbox_credentials,)@@ -240,12 +240,107 @@ def test_prepare_sandbox_home_preseeds_mootdx_config(tmp_path: Path) -> None:shutil.rmtree(sandbox, ignore_errors=True)-def test_make_rlimit_preexec_returns_callable_on_posix() -> None:- # Structural check only — the returned closure mutates *this* process's- # rlimits if called directly, so it must never be invoked in-process here;- # test_execute_* below already exercises it end-to-end via a real fork.- preexec = _make_rlimit_preexec()- assert preexec is None or callable(preexec)+def test_rlimit_bootstrap_argv_shape_on_posix() -> None:+ # Structural check only; test_execute_applies_address_space_rlimit below+ # proves the ceiling really lands, end-to-end through a real exec.+ argv = _rlimit_bootstrap_argv()+ if runner_mod.resource is None:+ assert argv is None+ return+ assert argv[0] == "-c"+ assert argv[1] == runner_mod._SANDBOX_RLIMIT_BOOTSTRAP+ # The two ceilings are resolved in the parent and passed as literal argv,+ # so the child never re-parses the env.+ assert int(argv[2]) == runner_mod._rlimit_as_bytes()+ assert int(argv[3]) == runner_mod._SANDBOX_RLIMIT_NOFILE+++def test_rlimit_bootstrap_is_valid_python() -> None:+ import ast++ ast.parse(runner_mod._SANDBOX_RLIMIT_BOOTSTRAP)+++def test_execute_never_passes_preexec_fn(monkeypatch, tmp_path: Path) -> None:+ """#1355: preexec_fn runs Python bytecode in the forked child of a+ multi-threaded parent (``vibe-trading serve``), which POSIX leaves+ undefined and which SIGSEGVs on aarch64/glibc 2.34. The rlimit ceiling must+ reach the child through the exec'd bootstrap instead, never through a fork+ callback."""+ seen: list[dict] = []+ real_run = runner_mod.subprocess.run++ def _spy_run(cmd, **kwargs):+ seen.append({"cmd": list(cmd), "kwargs": dict(kwargs)})+ return real_run(cmd, **kwargs)++ monkeypatch.setattr(runner_mod.subprocess, "run", _spy_run)++ run_dir = tmp_path / "run"+ run_dir.mkdir()+ entry = _probe_entry(tmp_path, "print('no-preexec')\n")++ result = Runner(timeout=60).execute(entry, run_dir, cwd=tmp_path)++ assert result.success, result.stderr+ assert "no-preexec" in result.stdout+ assert seen, "subprocess.run was never called"+ assert all("preexec_fn" not in call["kwargs"] for call in seen)+ if runner_mod.resource is not None:+ # ...and the ceiling is genuinely still requested, so a future patch+ # cannot satisfy this test by simply dropping the sandbox limits.+ launch = seen[-1]["cmd"]+ assert "-c" in launch+ assert launch[launch.index("-c") + 1] == runner_mod._SANDBOX_RLIMIT_BOOTSTRAP+++def test_execute_bootstrap_preserves_argv_and_script_dir(tmp_path: Path) -> None:+ # The -c wrapper must be transparent to the entry script: argv[0] is the+ # script, argv[1:] are the caller's cli_args, __name__ is "__main__", and+ # the script's own directory is importable exactly as with `python x.py`.+ # The script lives in its OWN directory, NOT in cwd — otherwise the `-c`+ # interpreter already has cwd on sys.path and the sibling import would+ # succeed even if the bootstrap never added the script dir at all.+ script_dir = tmp_path / "strategy"+ script_dir.mkdir()+ (script_dir / "sibling_mod.py").write_text(+ "VALUE = 'imported-from-script-dir'\n", encoding="utf-8"+ )+ run_dir = tmp_path / "run"+ run_dir.mkdir()+ entry = _probe_entry(+ script_dir,+ "import sys\n"+ "import sibling_mod\n"+ "print('name=' + __name__)\n"+ "print('argv0=' + sys.argv[0])\n"+ "print('rest=' + ','.join(sys.argv[1:]))\n"+ "print('sibling=' + sibling_mod.VALUE)\n",+ )++ result = Runner(timeout=60).execute(+ entry, run_dir, cwd=tmp_path, cli_args=["--alpha", "7"]+ )++ assert result.success, result.stderr+ out = result.stdout+ assert "name=__main__" in out+ assert f"argv0={entry}" in out+ assert "rest=--alpha,7" in out+ assert "sibling=imported-from-script-dir" in out+++def test_execute_bootstrap_propagates_nonzero_exit(tmp_path: Path) -> None:+ # A failing strategy must still surface its own exit code and traceback,+ # not be swallowed or remapped by the bootstrap.+ run_dir = tmp_path / "run"+ run_dir.mkdir()+ entry = _probe_entry(tmp_path, "import sys\nsys.exit(3)\n")++ result = Runner(timeout=60).execute(entry, run_dir, cwd=tmp_path)++ assert not result.success+ assert result.exit_code == 3def test_rlimit_as_bytes_respects_env_override(monkeypatch) -> None:@@ -334,7 +429,7 @@ def test_execute_retries_without_uid_drop_when_drop_fails(@pytest.mark.skipif(runner_mod.resource is None, reason="POSIX resource module required")def test_execute_applies_address_space_rlimit(monkeypatch, tmp_path: Path) -> None:- # Prove the preexec_fn actually ran in the child by reading back its+ # Prove the exec'd bootstrap actually applied the limits by reading back# RLIMIT_NOFILE (RLIMIT_AS is a no-op on macOS but NOFILE is portable).import resource as _resource