Skip to content

fix: kill every helper a serve-mode git fetch leaves behind (sc+git::) - #374

Merged
tonibergholm-codento merged 9 commits into
mainfrom
feat/sc-git-group-leader
Nov 22, 2026
Merged

tonibergholm-codento merged 9 commits into
mainfrom
feat/sc-git-group-leader

Conversation

@tonibergholm-codento

Copy link
Copy Markdown
Collaborator

sc serve --store sc+git:: runs every fetch from the Git host under a download watchdog (#371). The watchdog had a documented hole: a helper that outlived git's exit was never killed, because after git was reaped its process-group id could be reused and signalling it was unsafe. Examples are a smart-HTTP v0/v1 helper chain and a hostile process that git started. Such a helper could keep writing into the cache after the download was discarded.

Now, in serve mode on unix, git runs under a /bin/sh group leader that kills its own group:

s=$1; shift; ( r=0; command -- "$@" </dev/null || r=$?; echo $r >"$s" ) & exec >/dev/null 2>&1; read _ || :; kill -s KILL 0
  • How the leader works. sc holds the leader's stdin. Git's exit code comes back through a status file in the private cache directory, read strictly. Every end of a fetch, success or breach, closes that pipe, and the leader SIGKILLs its whole group with the shell's builtin kill.
  • No numeric group signal. sc never signals a group id itself and runs no external kill program. A reused group id therefore cannot be hit, and kill no longer has to be on the PATH. kill_group, may_signal_group and the missing-kill warning are gone.
  • The leader must be seen to do it. sc checks the leader is running just before closing its stdin, and requires it to end by SIGKILL. A leader killed from outside first, or one that ends any other way, fails the fetch and discards the download, even if git succeeded. The final reap is bounded.
  • The operator's shell state is stripped. SHELLOPTS, BASHOPTS, BASH_ENV, ENV and every BASH_FUNC_* are removed. On macOS /bin/sh is bash, and SHELLOPTS=monitor or an exported kill function could otherwise move git out of the group or replace the group kill.
  • Error text. Exit 126/127 keeps the "cannot run git … set SC_GIT" error, now with git's message. CLI mode and Windows are unchanged.

Limits, documented in ADR-0060 and THREAT-MODEL:

  • A process that leaves the group with setsid, such as an ssh ControlPersist master or a credential-cache daemon, is not killed.
  • If the leader is killed from outside, the fetch fails, and git or its helpers may survive.
  • An outside SIGKILL that lands between sc's check and the leader's own kill looks the same as the leader's own kill. Only the serve user or root can send it, never the Git host.
  • Each serve fetch costs an extra sh and about 20 ms.

ROADMAP records (10) as built.

Proof:

  • The leader script was run by hand on macOS /bin/sh (bash), dash, bash and zsh. Each propagated git's exit code and killed a helper that closed its pipes and kept writing, both after git exited and partway through.
  • New tests cover:
    • a helper that outlives git, dead afterwards;
    • a runaway fetch killed partway through;
    • a budget crossed after git exits;
    • a hostile shell environment;
    • exit-code propagation, and 127 as the cannot-run error;
    • the leader killed from outside, and a leader that ends the wrong way, both making a successful fetch an error;
    • strict status parsing.
  • Each rule was broken on purpose and a test failed. For example, removing kill -s KILL 0 or never closing stdin fails five tests.
  • fmt, clippy -D warnings, cargo test --workspace (1959 passed), the objio tests repeated and demo/run_git_carrier_demo.sh pass.

The plan was reviewed by Mythos (security), Astra and code-reviewer. The diff was reviewed by Mythos twice, by code-reviewer and with sol-loop.

Reviewed with sol-loop: 5 rounds with gpt-6.2-sol on high. Sol verdict: approved

Created with Claude Opus 5.5 in Claude Code (T3 Code), with a Claude Sonnet 5.5 subagent writing most of the implementation.

In serve mode on unix the fetch watchdog now spawns /bin/sh as the leader
of git's process group instead of git itself. The shell runs git in a
subshell, writes git's exit code to <cache>/.fetch-status-<n>, closes its
own stdout and stderr, and blocks reading a stdin pipe only sc holds.
When sc closes that pipe (or dies) the shell kills its whole group with
the builtin kill.

sc never waits for the leader before its group is dead: git's exit comes
from the status file (strict parse), so the leader's pid keeps pinning
the group id. Every end of a fetch, normal or breach, drops the pipe,
signals the group once through the system kill as a backup while the
leader is unreaped, reaps the leader within a bound, then settles and
measures. A helper that stays in git's group, including one that
outlives git, can no longer write after the discard. A helper that
leaves the group (setsid) still escapes.

Exit status 126 or 127 maps to the "cannot run git" error; a signalled
git reads as 128 + n. The CLI and non-unix builds are unchanged.

Tests rewrite the old "no group signal after git exits" cases for the
new rule and add: a quiet writer that outlives git is dead and stops
growing the pack directory, a mid-run breach kills the group with no
kill program, exit codes propagate, 126/127, a leader that dies early,
and the strict status parse.
…tchdog

ADR-0060, THREAT-MODEL, ARCHITECTURE, CLAUDE.md, the sc-cli-reference
skill and ROADMAP now say that serve mode on unix kills every helper that
stays in git's process group, including after git exits, and when sc
serve dies. A helper that leaves the group (setsid: an ssh ControlPersist
master, a credential-cache daemon) escapes; the status file trusts local
executables; SC_GIT is resolved by /bin/sh through command. Follow-on
(10) moves to built, and the claims that a helper outliving git is never
killed, that serve mode needs a kill program, and that a fetch outlives a
killed sc serve are removed for serve mode.
… leg

The escaped helper now writes its pid after setsid succeeds and the shim
waits for it, instead of sleeping, which raced the group kill. The late
writer test regains its serve-mode leg (a writer that left the group and
holds the pipes). describe_code words the 128 + n case as a hint, and
the THREAT-MODEL no longer says a v0/v1 helper chain may survive in
serve mode.
…nbounded waits

The leader's environment loses SHELLOPTS, BASHOPTS, BASH_ENV, ENV and every
BASH_FUNC_* variable (bash is /bin/sh on macOS: monitor moves the job out of
the group, errexit skips the status line and the final kill, exported
functions override command, echo, read and kill). The script itself now
tolerates errexit and uses `command --`.

The backup kill program is spawned and waited for at most 2 s. A missing
/bin/sh is reported as that, not as a missing git. The 128 + n hint appears
only in the text of fetches the leader ran, and a 126/127 from the shell
includes git's own stderr.

Tests: a hostile shell environment (set through a test seam on the leader
command), a hanging kill program, the leader must still exist when the
group is signalled, the quiet-writer test waits for the helper's first
write and has a variant without the backup kill, and the perl setsid
probe needs a positive session id. Docs soften the claims about sc
serve dying, say the shell functions are stripped, and note the per-fetch
cost.
…kill is the only group kill

An external kill program waited on by sc kills only that program's own
process, and signalling a group id after the leader is reaped could hit
a reused id. The leader's `kill -s KILL 0`, run when its stdin closes,
is now the one group kill. stop() closes the pipe, gives the leader 5 s
to exit, then kills it alone; terminate() reaps it, settles and measures.

Removed: kill_group, may_signal_group, KILL_WARNED, warn_once,
Tuning.kill, the `reaped` parameters, and the tests that existed only
for the backup (hanging kill, signalled-once ordering, missing kill,
runaway-without-kill). The quiet-writer test now also asserts the leader
is reaped when the call returns. A leader killed from outside first
leaves its group's helpers running: documented in ADR-0060,
THREAT-MODEL and CLAUDE.md, with every claim about a backup kill or a
needed kill program removed.
…d its reap

The status line can say 0 after the leader was killed from outside, with
a helper that closed its pipes still writing. stop() now checks that the
leader is still running right before closing its stdin, and then requires
it to end by SIGKILL, its own `kill -s KILL 0`. A leader that had already
exited, exits any other way, or is still there after the 5 s grace makes
the fetch an error even when git succeeded, and the download is
discarded. The slow leader is killed alone and waited for at most 2 s
more, then left unreaped instead of blocking.

Tests: a leader killed from outside after a successful fetch with a quiet
writer, and a leader (through a new Tuning.leader seam) that exits
normally or ignores the end of input. Two timing-fragile watchdog tests
now put the pid file first and make the budget crossing deterministic.
Docs describe the seen-to-end-by-its-own-kill rule instead of the
reaping order.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T10:09:02.183359Z 645bdeb PR opened
🔒 Security Review ✅ Completed 2026-11-22T10:12:16.954802Z 645bdeb PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Nov 22, 2026

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: cc1af4e7-d0ba-40b4-978b-727dd78acc14
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@tonibergholm-codento
tonibergholm-codento merged commit 7e4915f into main Nov 22, 2026
11 checks passed
@tonibergholm-codento
tonibergholm-codento deleted the feat/sc-git-group-leader branch November 22, 2026 10:22
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.

2 participants