Skip to content

fix slow COW listings, a freeze hang, and two profile recording gaps - #369

Open
ghazariann wants to merge 4 commits into
multikernel:mainfrom
ghazariann:fix/codex-under-learn
Open

ghazariann wants to merge 4 commits into
multikernel:mainfrom
ghazariann:fix/codex-under-learn

Conversation

@ghazariann

@ghazariann ghazariann commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Four independent bugs found while running the Codex CLI under learn.

List a directory through the kernel unless it changed

Once anything had been written in the COW branch, every getdents64 went through the merge, which stats each entry several times in the sequential supervisor loop. With a large workdir such as learn's /, one write made every later listing slow and stalled the sandbox.

Now only a directory that changed is merged: something was created or deleted inside it, or the directory itself was created or deleted. Every other directory is listed by the kernel as is.

Any workload that lists large directories after its first write pays for it, for example a zsh compinit walking $PATH. Repro:

mkdir /tmp/cow-big && (cd /tmp/cow-big && seq 1 100000 | xargs touch)
sandlock run -r /usr -r /lib -r /lib64 -r /bin -r /etc -w /tmp \
  --workdir / --on-exit abort -- sh -c 'echo x > /tmp/cow-trigger; ls -f /tmp/cow-big' | wc -l

Without the echo, this takes 214ms; with it, 2893ms.

Release a traced process that died while frozen

A process killed while frozen for another process's execve is a zombie still attached to the supervisor. Its exit goes to the tracer first, so its parent's wait does not see it. PTRACE_DETACH fails on it with ESRCH, the error was ignored, and the sandbox waited forever for its last process to exit.

When PTRACE_DETACH fails, the supervisor calls waitpid on the tracee. For a dead tracee this ends the attachment and hands the zombie to its parent. A tracee killed but not yet a zombie is retried for up to 100ms.

How the hang happens, step by step:

  1. The supervisor (the tracer) freezes zsh.
  2. While zsh is frozen, something sends it SIGKILL. SIGKILL is the one signal that still kills a frozen process.
  3. zsh is now a zombie. While a process is traced, its exit goes to the tracer first, not to its parent.
  4. The supervisor tries to let go with PTRACE_DETACH. That only works on a stopped process. zsh is dead, so the call fails with ESRCH.
  5. The old code ignored that failure. The zombie is now stuck: the parent's wait does not see it while it is traced, and the tracer never collects it.
  6. The sandbox waits until every process in it is gone. The zombie never goes away, so it waits forever.

About half of the learn runs hung after the workload finished, leaving a [zsh] <defunct> traced by a supervisor thread. With the fix, 12 of 12 exited.

Name legacy open(2) events "openat"

syscall_name mapped every legacy path syscall to its *at name except open, so a policy_fn saw a legacy open as "unknown" and missed the files opened with it, such as by musl binaries.

For Codex, a musl binary, the learned profile had no /etc/resolv.conf or ~/.codex config, and the replay failed to connect.

Resolve /proc/self/exe in an exec path

The exec branch recorded the path as given, so a program that re-execs itself through /proc/self/exe got that /proc link as a grant, which run refuses. Canonicalize it like every other recorded path.

run refused such a profile with:

fs grant /proc/self/exe is a /proc link and grants nothing

Tests

  • needs_dir_intercept_ignores_a_write_elsewhere, needs_dir_intercept_sees_a_file_staged_inside, needs_dir_intercept_sees_a_deleted_child, needs_dir_intercept_sees_the_directory_deleted, and two has_deleted_under tests
  • test_learn_then_run_legacy_open
  • test_learn_then_run_exec_proc_self_exe

Verified

With these fixes, the Codex CLI 0.173.0 works under sandlock learn and then sandlock run with the learned profile, for a plain answer, the shell tool, and file create plus apply_patch.

The commands used, for reference.

sandlock learn -o /tmp/codex_hi.toml -- \
  codex exec --skip-git-repo-check -s danger-full-access "say hi"

sandlock learn -o /tmp/codex_shell.toml -- \
  codex exec --skip-git-repo-check -s danger-full-access "run the command: ls -d */ | head -3"

sandlock run --profile-file /tmp/codex_shell.toml --cwd "$PWD" -- \
  codex exec --skip-git-repo-check -s danger-full-access "run the command: ls -d */ | head -3"

Once anything had been written in the COW branch, every getdents64
went through the merge, which stats each entry several times in the
sequential supervisor loop. With a large workdir such as learn's "/",
one write made every later listing slow and stalled the sandbox.

Now only a directory that changed is merged: something was created
or deleted inside it, or the directory itself was created or deleted.
Every other directory is listed by the kernel as is.

Signed-off-by: Vahagn <wanghang25@mails.tsinghua.edu.cn>
A process killed while frozen for another process's execve is a
zombie still attached to the supervisor. Its exit goes to the tracer
first, so its parent's wait does not see it. PTRACE_DETACH fails on
it with ESRCH, the error was ignored, and the sandbox waited forever
for its last process to exit.

When PTRACE_DETACH fails, the supervisor calls waitpid on the tracee.
For a dead tracee this ends the attachment and hands the zombie to its
parent. A tracee killed but not yet a zombie is retried for up to
100ms.

Signed-off-by: Vahagn <wanghang25@mails.tsinghua.edu.cn>
syscall_name mapped every legacy path syscall to its *at name except
open, so a policy_fn saw a legacy open as "unknown" and missed the
files opened with it, such as by musl binaries.

Signed-off-by: Vahagn <wanghang25@mails.tsinghua.edu.cn>
The exec branch recorded the path as given, so a program that re-execs
itself through /proc/self/exe got that /proc link as a grant, which
run refuses. Canonicalize it like every other recorded path.

Signed-off-by: Vahagn <wanghang25@mails.tsinghua.edu.cn>
@ghazariann
ghazariann force-pushed the fix/codex-under-learn branch from e9cfef3 to a12d0ea Compare December 3, 2026 15:26

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant