KBench / tools /factory /shared /PROCESS_MANAGEMENT.md
ZMC2019's picture
Reorganise: group 313 tasks into 17 families under tasks/, generators under tools/ (part 10)
0f775e2 verified
|
Raw
History Blame Contribute Delete
4.59 kB

Managing background processes — READ THIS before you kill anything

You may start and stop background processes while iterating — a server, a long build or training run, or an ncu / nsys profiling session. The way you stop one can silently kill your own agent process and end the whole run. This has happened repeatedly, including via a hand-rolled /proc scan — not just pkill. Read this once and follow the safe recipe.

The trap: matching task text against process command lines

You were launched as roughly:

claude --print -- '<the entire task instruction text>'

The full task instruction is on your own process's command line, and it contains the file names and commands the task mentions (e.g. server.py, build.sh, train.py, solution.cu, /app/...). So any command that finds processes by matching those strings will match — and can kill — the agent itself:

pkill -9 -f "train.py"                              # matches the claude agent -> kills YOU
pgrep -f "server.py" | xargs kill                   # same: the pid list includes claude
for p in $(pgrep -f build.sh); do kill -9 $p; done  # same
for pid in /proc/*; do case "$(cmdline)" in *solution*) kill -9 $pid;; esac; done   # same

It is not about pkill — it is about matching a string that appears in your prompt. pgrep, a /proc scan, fuser, or nvidia-smi-pid loops all have the same failure if the match pattern (or the surviving pid list) includes the claude process. When you kill -9 yourself the run dies immediately (NonZeroAgentExitCodeError) and you are graded on whatever was already in place — usually worse than what you were about to finish.

The one safe rule: kill only the exact PID you recorded

Start a background job and record its PID in a file:

nohup <your command> > /tmp/job.log 2>&1 &
echo $! > /tmp/job.pid

Stop / restart by that exact PID — never by a name pattern:

kill "$(cat /tmp/job.pid)" 2>/dev/null      # SIGTERM: lets it release the GPU / flush cleanly
sleep 3
kill -9 "$(cat /tmp/job.pid)" 2>/dev/null   # force only if it did not exit

If the job spawns workers, run it in its own process group and signal the group by its (negative) PGID — still an exact id, never a pattern:

setsid <your command> > /tmp/job.log 2>&1 < /dev/null & echo $! > /tmp/job.pid
kill -TERM -- "-$(cat /tmp/job.pid)" 2>/dev/null

If you ever enumerate processes, EXCLUDE the agent

Prefer not to. But if you must scan (e.g. hunting a leaked worker), exclude the claude agent and your own shell, and match on a unique token that is NOT anywhere in this prompt — give your job a unique name and match only that:

# job named e.g. job_7x3q9 — a token that appears NOWHERE in the task text
pgrep -af job_7x3q9 | grep -v -E 'claude|--print|pgrep|grep|bash'   # LOOK FIRST
# only kill pids from THIS filtered list, and never a pid whose cmdline contains 'claude' or '--print'

Do not add a prompt word (a file name, /app, python3, …) to the pattern "just in case" — that single addition re-introduces the self-kill. Match the unique token only.

Freeing the GPU without going nuclear

If VRAM stays high after you stop a GPU job, the usual cause is a child process or a not-yet- collected CUDA context — not a stray process you need to hunt across /proc. Do this instead of a /proc sweep:

  • Kill the job by its recorded PID / PGID (above) and sleep 5–10; the CUDA context frees after the process fully exits.
  • Make the job release cleanly on SIGTERM (drop the model / torch.cuda.empty_cache() / exit) so a single kill $PID is enough.
  • Start it in its own group (setsid) so kill -- -$PGID takes the workers with it.
  • Check with nvidia-smi — if a PID still holds memory, kill that exact numeric PID, after confirming its cmdline is not claude / --print.

Rules of thumb

  • Never match a task-prompt string (server.py, train.py, build.sh, /app, python3, …) with pkill -f, pgrep -f, killall, fuser, or a /proc cmdline scan — they all can hit the agent (its argv holds the prompt).
  • Always kill a background job by the exact PID from its pidfile (or its PGID).
  • If you must enumerate, match a unique token not in the prompt and exclude claude / --print and your own shell; pgrep -af and look before you kill.
  • SIGTERM first for a clean release; -9 only as a last resort on a specific numeric PID.