Skip to content

heavy: clean up everything a command started; heavy leftovers names the rest - #687

Merged
scuttletoby merged 2 commits into
mainfrom
fork/heavy-leftovers-159
Nov 20, 2026
Merged

scuttletoby merged 2 commits into
mainfrom
fork/heavy-leftovers-159

Conversation

@scuttletoby

Copy link
Copy Markdown
Owner

Fixes #159

What changes

  • heavy cleans up its own scope on every end.
    • A scope guard is a small setsid child named scope-guard. It holds no queue ticket and no other fd of the command.
    • When heavy dies, even from kill -9, the guard stops the scope and removes its containers, and prints heavy: heavy was killed: ....
    • When heavy ends normally, it tells the guard to exit quietly.
  • Containers. heavy removes containers that a command starts with the label fork-kit.heavy.owner=uid=<uid>,scope=<unit> (from heavy docker-label).
    • This happens when the command ends, when it is stopped, and when heavy is killed.
    • A kept scope (HEAVY_KEEP=1) keeps its containers.
    • heavy finds them with a label filter, checks the exact label with inspect, and only then removes them, so it never touches containers it did not start.
    • When Docker is missing or stopped, the run itself does not change.
  • Double-forked children are stopped with the scope. A command whose parent is already gone is refused at once.
  • heavy leftovers [--older-than AGE] [--json] only looks.
    • It names processes, scopes and containers older than a limit, by owner and age.
    • With no sweep settings it uses the default rules. It refuses a broken settings file, and never stops anything.
    • The sweep's own report now also shows the owner and age.
  • heavy.test.py now refuses to run inside a heavy scope. Its cleanup would stop the outer scope. Run it directly, as CI does.

General vs project

  • General (this PR): the guard, the removal of labelled containers, the leftovers look, and the owner/age report. These work on any machine with systemd user scopes, with or without Docker.
  • Project (config, not here): the age limit, the sweep rules, and whether a project's capacity check calls heavy leftovers. A project's tests must label their own containers with heavy docker-label to get removed. Unlabelled containers are only reported.

State question (in tests)

  • Killed half-way: kill -9 on heavy stops the command, its double-forked child and its container. A queued heavy killed with -9 frees its reservation.
  • Runs again: a rerun after a killed heavy starts clean, with no old scope or container left. heavy leftovers gives the same answer twice and changes nothing.
  • Old, empty or missing state:
    • with no sweep settings, the defaults are used;
    • a broken settings file is refused;
    • Docker missing or stopped is reported, and the processes are still named;
    • a kept scope keeps its container;
    • other containers are never touched.

Tests

  • sweep.test.py: 74 OK (1 skipped). queue.test.py: 70 OK. install.test.py: 16 OK.
  • heavy.test.py with real systemd: 40 OK, run twice.
  • Red check: the new tests were run against the old bin/heavy, taken from git archive origin/main fork-kit.
    • 8 tests failed: the killed-with-9 container + double-fork test, the container removal, the queued kill -9 test, the rerun, the Docker-down test, the guard name check, and TERM removing the container.
    • The leftovers sweep tests fail on the old kit, because it has no leftovers.
    • The normal-end double-fork test and the kept-scope test pass on the old code too. They guard behaviour that already worked.

🤖 Generated with Claude Code

…he rest

heavy now removes its own scope's leftovers on every end:
- a scope guard (a small setsid child that holds no queue ticket) stops the
  scope and removes its containers when heavy itself is killed, even with -9;
- containers a command starts with the heavy owner label are removed when the
  command ends, is stopped or heavy is killed; a kept scope keeps them;
- a command whose parent died is refused at once (double-forked children are
  stopped with the scope).

`heavy leftovers [--older-than AGE] [--json]` looks only: it names processes,
scopes and containers older than a limit, by owner and age, with default rules
when there are no sweep settings. It never stops anything.

Tests cover a killed heavy, a double-forked child, a container started by a
command, a kept scope, missing or stopped Docker, and a rerun after a kill.

Fixes #159

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@scuttletoby

Copy link
Copy Markdown
Owner Author

Review lens A (safety: what can it kill or remove?) — SAFE

Head 55d5fbaf58. Read #159 and the whole diff against main. Ran locally: sweep.test.py 74 OK (1 skipped), queue.test.py 70 OK, install.test.py 16 OK (all under heavy), heavy.test.py 40 OK (run directly, real systemd, fake docker). CI: all checks green.

Blocking: none.
Major: none.

What I checked and found safe

  • Container removal never touches another owner's container. remove_containers (fork-kit/heavy/bin/heavy:314) lists by label filter, then keeps only containers whose inspect label equals uid=<uid>,scope=<this unit> and whose full id came from that ps, then runs rm by full 64-hex id after --. A loose filter only leaks; it can never widen what gets removed. Container ids are never reused, so there is no TOCTOU between inspect and rm.
  • Label forgery. The label is unforgeable enough for the job. Only a process with Docker access can set it, and that is root-equivalent anyway. An accidental match needs the exact uid plus a scope name with 32 random bits that is live at that moment.
  • Docker missing, stopped or garbled: docker_tool returns None, or ps/inspect fails and returns [] with a problem line. Nothing is removed and the run's exit code does not change (heavy.test.py covers this).
  • The guard cannot outlive heavy and hit a later scope.
    • The guard acts at most once, right at EOF on its pipe.
    • Only heavy holds the write end. It is O_CLOEXEC, so the command, systemd-run and the subprocess calls drop it. The GroupProbe fork closes it explicitly.
    • On a normal end, done() writes d and then reaps the guard. Any other exit of heavy closes the pipe, so the guard always ends.
    • The cleanup is bounded: systemctl timeouts plus the docker timeouts (20 + 20 + 120 s).
  • HEAVY_KEEP: no guard. Containers are kept unless heavy was stopped by a signal, which matches what happens to the scope (bin/heavy:674, :798).
  • Signals and ordering: the guard is forked while WATCH/FORWARD/SIGCHLD are blocked. It sets SIG_IGN before it unblocks them, so no early TERM can kill it. Fork does not inherit pending signals. setsid takes it out of a group kill. T3 kills by process group (packages/provider-core/src/server/processGroup.ts), so the guard survives that. It closes the queue ticket fd, so a killed queued job frees its reservation once the scope stops (tested).
  • Orphaned start: the getppid() != parent check in child_setup (bin/heavy:545) closes the window where heavy dies before PDEATHSIG is set.
  • heavy leftovers really only looks:
    • It sets log_file = None and calls run(cfg, act=False). Every branch of run() returns before stop or remove when act is false.
    • It takes no lock and writes no report.
    • A broken machine file is still refused, because optional only covers a missing file, section or key.
  • Shell quoting: bin/heavy builds every command as an argv list, with no shell. The label value's = and , are safe in --label/--filter: the Docker CLI splits at the first = and does not split on commas.

Minor

  1. fork-kit/heavy/lib/heavy_sweep.py:1150: docker-label trusts HEAVY_SCOPE from the environment before the real cgroup.
    • A process that left heavy's scope but kept the env var still gets heavy's scope in its label. Examples: systemd-run --user --scope ... from inside the command, or a tool that copies the env into a new unit.
    • heavy then removes that container when its command ends, although the process using it is not in the scope and is not stopped.
    • It is still a descendant of this heavy's command, so this is not a foreign kill. But preferring the own cgroup when it is a heavy scope (and using the env only as a fallback) would make the label match "is in the scope" exactly.
  2. fork-kit/heavy/bin/heavy:263-285: the fallback kill in stop_scope signals pids read from cgroup.procs one by one.
    • This was already there, and the guard now uses it too. It only runs when systemctl stop failed.
    • A pid can exit and be reused between the read and os.kill (a tiny window, but a real PID-reuse path to a foreign process).
    • On cgroup v2 with kernel ≥ 5.14, writing 1 to <cg>/cgroup.kill is atomic and has no reuse window. Keep the pid loop only as the fallback when cgroup.kill is missing.
  3. fork-kit/heavy/bin/heavy:369: the guard is heavy's direct child.
    • A tree killer that walks children by ppid and sends KILL to all of them (pkill -9 -P, npm tree-kill) kills the guard together with heavy. The double-forked leftovers then stay until the sweep.
    • That is a leak, not a wrong kill, and the README covers it as "kill -9 on both". Worth one more line naming tree-kills there, or a double fork plus EOF-only sync so the guard is not in heavy's child list.
  4. fork-kit/heavy/bin/heavy:290-301: Docker reached only through a context.
    • If the context was picked with docker context use (the usual rootless setup) and there is no DOCKER_HOST/DOCKER_CONTEXT env and no access to /var/run/docker.sock, docker_tool returns None. heavy then silently removes nothing.
    • Safe (it only leaks), but the README's "none this user can reach: nothing said" hides a setup that can reach Docker. Possible fixes: also check ~/.docker/config.json currentContext, or say one line when docker exists but heavy skips it.
  5. fork-kit/heavy/lib/heavy_sweep.py:624: the installed sweep now honours HEAVY_DOCKER without the isabs check bin/heavy:294 has.
    • A relative value left in a shell's env would run ./<value> from the cwd under heavy sweep. It is the same user and the timer's env does not have it, so the risk is low.
    • For consistency, use the same isabs + X_OK rule as bin/heavy.

Not new and negligible, so no change asked: two live scopes drawing the same 8-hex suffix would make the second systemd-run --unit fail. heavy's cleanup would then stop the first scope. The guard adds nothing to this.

@scuttletoby

Copy link
Copy Markdown
Owner Author

Review lens B (state and tests): SAFE

I found nothing that loses data or breaks a run. CI is green: the real-systemd heavy.test.py passed 40 tests in the root job, and the kit shards passed. Every gap below is in the tests, not the code. Two of them are worth closing before merge, or as an immediate follow-up.

What I ran (locally, in a temp worktree):

  • heavy.test.py directly (real systemd user manager): 40 OK.
  • sweep.test.py, queue.test.py and install.test.py, each under heavy: 74 OK (1 skipped, the existing skip inside a heavy scope), OK, OK.
  • The new tests against main's bin/heavy (HEAVY_TEST_BIN): 8 fail. These are the kill-9 container and double-fork test, container removal, the queued kill-9 test, the rerun, Docker down, the guard name, and TERM removing the container. This matches the PR body.
  • The sweep's own red check covers the 5 new leftovers mutations.

State question

  • Same scope name again: units are heavy-<name>-<8 hex>, so a rerun never meets the old unit. The rerun test proves the same container name is free again, because communicate() waits for the guard to release stderr. OK.
  • Killed half-way through cleanup: the guard reads EOF and does the cleanup again. systemctl stop and docker rm -f can safely run twice. A docker rm racing heavy's own rm can print one stray "could not remove" line. Cosmetic.
  • Guard killed: heavy still cleans up. done() writing into a dead pipe gets EPIPE (Python ignores SIGPIPE) and is caught. The sweep covers the case where both heavy and the guard are killed. OK.
  • Old scopes and labels from the previous version: the label format (uid=..,scope=..) is unchanged. Old heavies have no guard and are swept as before. OK.
  • Empty or missing settings: with no file, no heavy section, or only heavy.queue, leftovers uses the defaults. A broken, newer, mode-0666 or too-young file is refused. Both are tested and mutation-checked. OK.

Major (tests: claimed behaviour that no test pins)
I removed each fix line in turn and ran the full heavy.test.py. These mutations stay green:

  1. No setsid in the guard, fork-kit/heavy/bin/heavy:379. Tests stay green, but this is the main real-world kill path: a harness or timeout killing heavy's whole process group with -9. I checked it by hand with a fake docker:
    • PR code, kill -9 -PGID: 0 processes left, container removed.
    • Without setsid: the guard dies with the group, and one double-forked process plus the container are left.
    • Please add a test that does kill -9 -<pgid> (start heavy with start_new_session=True).
  2. No container removal on the signal path, bin/heavy:773, where the command outlives the grace time or gets a second signal. Tests stay green. test_term_to_heavy_removes_the_commands_container (heavy.test.py:417) uses exec sleeper, which dies of TERM, so it covers the normal-end path at line 798 instead. Please add a container to the stubborn-command TERM or INT test.

Minor

  • Mutations that stay green:
    • The guard ignoring TERM/INT/HUP/QUIT (bin/heavy:381, the service-stop case).
    • The getppid() != parent refusal (bin/heavy:545).
    • Kept scopes getting no guard (bin/heavy:674). If this broke, a kill -9 of a kept heavy would stop the kept daemon.
    • The exact-label check after inspect (bin/heavy:337). The fake ps already filters exactly (fake_tools.py:90), so this second check can never be the one that blocks a wrong match in tests. A fake mode whose ps matches the key only would pin it.
  • fork-ci-tests.txt:103 weight ~52s: CI took 37.3 s on this PR and 37.0 s on main's last green run. The new tests are fast fakes, and the 52 s looks like a local time on a busy box. ~37s matches CI. Under the 2x threshold, so no note fires.
  • Docker in every run: every non-kept heavy run calls docker ps when Docker is reachable (docker_tool, bin/heavy:290), even when the command never used Docker.
    • With docker.socket socket activation and docker.service stopped (for example, to free memory), each heavy run starts dockerd again.
    • A hung daemon adds up to 20 s + 20 s + 120 s to the end of every run (DOCKER_TIMEOUT, bin/heavy:69, :345).
    • A future option: only query Docker when the scope ever ran heavy docker-label.
  • Rootless Docker: rootless Docker chosen with docker context use (no DOCKER_HOST or DOCKER_CONTEXT env, no readable /var/run/docker.sock) is treated as "no Docker". Its labelled containers are never removed by heavy, only by the sweep.

Real vs fake: heavy.test.py uses real systemd, real setsid and double-forks, and real kill -9, with a fake docker. That matches what the PR claims. Real Docker's --filter label=K=V (where V contains = and ,) is not exercised. The exact inspect check makes that fail safe.

Public diff: no personal paths, host names, VM ids, user names or emails.

🤖 Generated with Claude Code

@scuttletoby
scuttletoby merged commit fd6a854 into main Nov 20, 2026
30 checks passed
scuttletoby added a commit that referenced this pull request Nov 21, 2026
Tests pin what the guard and heavy's clean-up rely on: setsid (a kill -9 of
heavy's process group), the guard ignoring heavy's signals, the parent-gone
refusal, no guard for a kept scope, heavy's own exact label check, and the
container removal on the signal path (grace time and second signal).

Hardening: the guard is double-forked (a tree kill of heavy misses it); a
stop by hand signals only pids still in the scope (pidfd + cgroup check) and
kills with cgroup.kill; docker-label names the scope it is in, HEAVY_SCOPE
only when the cgroup cannot be read; a Docker context counts as reachable;
the sweep takes only an absolute HEAVY_DOCKER; heavy asks Docker only after
a run that labelled a container. CI weight of sweep.test.py ~37s.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
scuttletoby added a commit that referenced this pull request Nov 21, 2026
…he rest (#687)

heavy now removes its own scope's leftovers on every end:
- a scope guard (a small setsid child that holds no queue ticket) stops the
  scope and removes its containers when heavy itself is killed, even with -9;
- containers a command starts with the heavy owner label are removed when the
  command ends, is stopped or heavy is killed; a kept scope keeps them;
- a command whose parent died is refused at once (double-forked children are
  stopped with the scope).

`heavy leftovers [--older-than AGE] [--json]` looks only: it names processes,
scopes and containers older than a limit, by owner and age, with default rules
when there are no sweep settings. It never stops anything.

Tests cover a killed heavy, a double-forked child, a container started by a
command, a kept scope, missing or stopped Docker, and a rerun after a kill.

Fixes #159

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
scuttletoby added a commit that referenced this pull request Nov 21, 2026
Tests pin what the guard and heavy's clean-up rely on: setsid (a kill -9 of
heavy's process group), the guard ignoring heavy's signals, the parent-gone
refusal, no guard for a kept scope, heavy's own exact label check, and the
container removal on the signal path (grace time and second signal).

Hardening: the guard is double-forked (a tree kill of heavy misses it); a
stop by hand signals only pids still in the scope (pidfd + cgroup check) and
kills with cgroup.kill; docker-label names the scope it is in, HEAVY_SCOPE
only when the cgroup cannot be read; a Docker context counts as reachable;
the sweep takes only an absolute HEAVY_DOCKER; heavy asks Docker only after
a run that labelled a container. CI weight of sweep.test.py ~37s.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
scuttletoby added a commit that referenced this pull request Nov 21, 2026
…he rest (#687)

heavy now removes its own scope's leftovers on every end:
- a scope guard (a small setsid child that holds no queue ticket) stops the
  scope and removes its containers when heavy itself is killed, even with -9;
- containers a command starts with the heavy owner label are removed when the
  command ends, is stopped or heavy is killed; a kept scope keeps them;
- a command whose parent died is refused at once (double-forked children are
  stopped with the scope).

`heavy leftovers [--older-than AGE] [--json]` looks only: it names processes,
scopes and containers older than a limit, by owner and age, with default rules
when there are no sweep settings. It never stops anything.

Tests cover a killed heavy, a double-forked child, a container started by a
command, a kept scope, missing or stopped Docker, and a rerun after a kill.

Fixes #159

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
scuttletoby added a commit that referenced this pull request Nov 21, 2026
Tests pin what the guard and heavy's clean-up rely on: setsid (a kill -9 of
heavy's process group), the guard ignoring heavy's signals, the parent-gone
refusal, no guard for a kept scope, heavy's own exact label check, and the
container removal on the signal path (grace time and second signal).

Hardening: the guard is double-forked (a tree kill of heavy misses it); a
stop by hand signals only pids still in the scope (pidfd + cgroup check) and
kills with cgroup.kill; docker-label names the scope it is in, HEAVY_SCOPE
only when the cgroup cannot be read; a Docker context counts as reachable;
the sweep takes only an absolute HEAVY_DOCKER; heavy asks Docker only after
a run that labelled a container. CI weight of sweep.test.py ~37s.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
scuttletoby added a commit that referenced this pull request Nov 21, 2026
…he rest (#687)

heavy now removes its own scope's leftovers on every end:
- a scope guard (a small setsid child that holds no queue ticket) stops the
  scope and removes its containers when heavy itself is killed, even with -9;
- containers a command starts with the heavy owner label are removed when the
  command ends, is stopped or heavy is killed; a kept scope keeps them;
- a command whose parent died is refused at once (double-forked children are
  stopped with the scope).

`heavy leftovers [--older-than AGE] [--json]` looks only: it names processes,
scopes and containers older than a limit, by owner and age, with default rules
when there are no sweep settings. It never stops anything.

Tests cover a killed heavy, a double-forked child, a container started by a
command, a kept scope, missing or stopped Docker, and a rerun after a kill.

Fixes #159

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
scuttletoby added a commit that referenced this pull request Nov 21, 2026
Tests pin what the guard and heavy's clean-up rely on: setsid (a kill -9 of
heavy's process group), the guard ignoring heavy's signals, the parent-gone
refusal, no guard for a kept scope, heavy's own exact label check, and the
container removal on the signal path (grace time and second signal).

Hardening: the guard is double-forked (a tree kill of heavy misses it); a
stop by hand signals only pids still in the scope (pidfd + cgroup check) and
kills with cgroup.kill; docker-label names the scope it is in, HEAVY_SCOPE
only when the cgroup cannot be read; a Docker context counts as reachable;
the sweep takes only an absolute HEAVY_DOCKER; heavy asks Docker only after
a run that labelled a container. CI weight of sweep.test.py ~37s.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

Catch leftover processes automatically (heavy cleans up its scope, a leftover sweep, capacity check names them)

1 participant