Repository navigation
feat(coder): preserve lifecycle metadata and diagnostics for detached jobs - #103
Merged
aixle-com[bot] merged 12 commits intoAug 15, 2026
Merged
Conversation
added 4 commits
August 14, 2026 11:03
A detached job that was not allowed to finish normally used to lose everything: the wrapper wrote its exit code only after the command returned, so a killed wrapper left `coder_job_status` able to say no more than `died` — no exit code, no signal, no timing, no way to tell a failing test from a workspace that rebooted. The launcher now persists `<job_id>.meta` before it starts anything (job id, command, workspace, pid/pgid, `started_at`), the wrapper traps TERM/INT/HUP/QUIT so an interrupted job still records its signal, `finished_at` and exit code, and a heartbeat dates the death of a job whose wrapper was SIGKILLed and could therefore run no trap. `coder_job_status` returns that metadata plus a `reason` (completed | command_failed | timeout | signaled | runner_vanished) and a one-line `diagnosis`, which is what separates a command failure from a cancellation, a timeout and an infrastructure failure. A job started by the previous wrapper has no metadata file and still reports state, exit code and log tail; its `reason` is derived from the exit code alone.
…njection assertion Also prefer the heartbeat's own timestamp over re-deriving it from its epoch when dating a job that died.
…ob-lifecycle-metadata # Conflicts: # app/services/coder/ssh_runner.rb
added 2 commits
August 14, 2026 20:15
…ob-lifecycle-metadata
… cause Two blocking review findings on PR #103. The detach wrapper's trap recorded the signal it received but always sent TERM to the command, so a HUP was reported as something the command never got and signal-specific cleanup ran for the wrong signal. It now forwards the signal it was sent. INT and QUIT are the exception POSIX forces: a non-interactive shell sets them to ignore in a command it starts in the background, so they cannot reach the command at all — TERM follows them so the job is not orphaned, and `child_signal` records the substitution instead of letting the metadata imply otherwise. (The same rule applies to the wrapper itself, which is why its INT/QUIT traps cannot fire in the detached launch path; TERM and HUP, the ones a teardown sends, can.) The diagnosis for a vanished runner asserted an infrastructure failure, but a hard `kill -9` on the job's process group leaves byte-identical evidence. Nothing in the app hard-kills a detached job, so there is no cancellation intent to persist; the poll now reports the state as indeterminate, names both candidate causes in the diagnosis and carries them as `possible_causes` rather than sending an operator after a phantom infrastructure failure.
added 3 commits
August 14, 2026 21:48
…ob-lifecycle-metadata
A successful `kill` confirms delivery, not termination: a command that traps the signal and keeps working stayed alive while the job already reported `exited`, so the persisted lifecycle state could be false while the log was still changing. The wrapper's trap now forwards the signal as before, then waits for the command and reaps it before writing anything terminal. The wait is bounded by `job_kill_grace_seconds` and ended by a SIGKILL escalation, and what really happened is recorded: `signaled_at` when the cancellation started, `child_exit_code` from the reap, `escalated_to` when the first signal was not what ended it. A command that trapped the signal and exited 0 keeps its own status in `child_exit_code` while the job still reports the conventional 128+signo code — cancelled, not successful. The heartbeat keeps running through the grace window, so a job whose command is still alive keeps reading `running`.
…ob-lifecycle-metadata
`wait` in the job wrapper reaps the `sh <job>.cmd` shell and nothing under it, and both the forwarded signal and the SIGKILL escalation targeted only that pid. A command script blocked on a foreground descendant (`sleep 60; echo done`) therefore returned 143 the moment the shell was signalled while the descendant kept running, and the wrapper published a terminal `.exit` over work that was still consuming the workspace. The command is now started under `setsid`, so it leads a process group of its own and the cancellation reaches the whole tree it built. The group is only ever targeted when it is led by the command itself and differs from the wrapper's group — signalling the wrapper's own group would kill the wrapper and lose the metadata this path exists to write — and a workspace without `setsid` falls back to the previous pid targeting. After the reap the wrapper drains what is left of the group before publishing anything terminal, bounded by the grace window's SIGKILL and a settle cap for what even that cannot reap (`group_survivors`). Signals go out as `kill -NAME <target>`: dash parses the argument after `-s NAME` as another option bundle, so `kill -s TERM -1234` dies with "Illegal option -1". The pgid is read from `/proc/<pid>/stat` with `ps` as the fallback, because the busybox `ps` of a minimal workspace image has no `-p`. `command_pid` and `command_pgid` are recorded, and since a hard kill of the wrapper's group no longer stops the command, the poll reports `command_group_alive` for a vanished runner whose command is still running — with the group to kill.
setsid makes the command the leader of a group of its own, so its group id is its pid. Reading it back out of /proc after the launch instead lost the race against a command that had already exited, and the wrapper then wrote no command_pgid at all — the field a caller cleans up survivors by, and the one command_group_alive is derived from.
Contributor
Author
There was a problem hiding this comment.
Verdict: approved on technical merit. The lifecycle implementation meets the task requirements and follows established Coder service patterns; prior blocking findings are resolved. GitHub does not permit this app identity to formally approve its own PR. One non-blocking framing robustness recommendation is attached inline.
artempartos
added a commit
that referenced
this pull request
Aug 28, 2026
) The detached-job signal tests kill the wrapper themselves and assert the job publishes the signal instead of disappearing. Both outcomes read as `died`, because that state means only "the wrapper is gone and wrote no exit file" — which is the #103 regression, but equally what is left when something outside the test destroys the wrapper mid-run. No trap survives SIGKILL, so a wrapper reclaimed by the environment cannot write the exit file however correct it is. `signaled_at` separates the two: the trap records it before it touches anything else. Present, the wrapper did receive and register the signal the test sent and was ended afterwards by something that is not the subject here, so the case skips with the status attached. Absent, the job really did vanish unrecorded and the assertions fail as before. The terminal assertions also carry the full status now. A failure used to print `Expected "exited", Actual "died"` and nothing else, which cannot distinguish a wrapper that published the wrong thing from one that never got to publish at all. Signed-off-by: Artem Petrov <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
A detached job (
coder_ssh_exec(detach: true)) used to write its exit code only after the command returned normally. If the wrapper or its process group was killed,coder_job_statuscould report no more thandied: no exit code, no signal, no timing, nothing to tell a failing test from a workspace that rebooted. Recovering any of that meant reading file mtimes or Docker events by hand.Launcher (
Coder::SshRunner#detach_script) now persists<job_id>.metabefore it starts anything: job id, workspace, job dir, log path,started_at(+ epoch). A job killed a second later still has its identity on disk.Wrapper (
<job_id>.run):TERM/INT/HUP/QUIT, forwards the signal to the command, and then recordsfinished_at,reason=signaled, the signal name and the128+nexit code — so an interrupted job still writes its exit file instead of looking like it evaporated;waits on it, because a POSIX shell only runs a trap once the foreground command returns;<job_id>.hb) every 10s, which is what dates the death of a job whose wrapper wasSIGKILLed and could therefore run no trap;pid/pgid, and classifies its own outcome (completed,command_failed,timeoutfor exit 124,signaled).coder_job_statusreturns that metadata alongside the existingstate/exit_code/tail:reason,signal,command,workspace,pid,pgid,started_at,finished_at(+finished_at_estimatedwhen it comes from the last heartbeat),elapsed_seconds,last_heartbeat_at,heartbeat_age_seconds,log_path,log_bytes— plus a one-linediagnosisthat names the four outcomes apart: the command's own failure, a timeout, a cancellation/outside kill, and an infrastructure failure.The workspace name is sanitised before it is recorded, because it lands inside a double-quoted shell string.
Why the shape
key=valuefile, last line wins: the wrapper appends as it learns, and a reader never needs a second source.started_atandmeta_pathare also returned from the launch call, so a caller knows them without polling.Compatibility
A job started by the previous wrapper has no
.metafile. It still reportsstate,exit_codeandtail;reasonis then derived from the exit code alone, and every metadata-derived field is simply absent rather than null. Covered by a test.How to test
New coverage in
test/services/coder/ssh_runner_test.rb:AIXLE_JOB_DIRin a tmpdir,coder sshswapped forsh -c): success,exit 3, and aSIGTERMmid-run that must recordreason=signaled/signal=TERM/143 instead of vanishing.Board task: #581.
🤖 Generated with Claude Code