Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
is now accepted, so `answer_dialog` can answer it. Web search and wrapped shell commands stay
refused. Fixtures for the new-file write, web fetch and web search keys and the wrapped shell
dialogs are added under `tests/fixtures/kiro-3.3.0/`.
- Native panes (Linux): the holder reads the process table once per 250 ms for all its panes,
not once per pane, so idle CPU for eight panes drops. The cost is detection latency. A
descendant that detaches with `setsid` and whose parents exit is now seen within about 500 ms
in the nominal case, and at most about 700 ms provided each scan finishes within 250 ms (it was
about 250 ms nominal). On macOS the bound stays about 450 ms. Stops still read the table afresh,
and a failed read never removes a process from a pane's tree.
- Native panes: a process whose command name is not valid UTF-8 is now trackable, so it is found
and, on `remove_pane` or shutdown, signalled. Before, it escaped the descendant tree and survived.

## 0.5.0

Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ which answers a permission dialog may receive.
- **Process-tree cleanup.** `remove_pane`, a program's own exit and Shepherd's
shutdown signal the pane's process group and every descendant Shepherd has seen,
then kill what is left. Descendants are found by looking at the process table
about every 250 ms, so a process that fully detaches faster than that is missed
(see [Status and known limits](#status-and-known-limits)).
about every 250 ms (at most about 450 ms apart), so a process that fully detaches
before the next look is missed. On Linux a look may use a snapshot up to 250 ms
old: a descendant is seen within about 500 ms in the nominal case, and at most
about 700 ms provided each scan finishes within 250 ms. On macOS the bound is
about 450 ms (see [Status and known limits](#status-and-known-limits)).
- **Container image.** The `Dockerfile` builds a Debian image that runs as uid 1000
under tini, with kiro-cli pinned to 3.3.0 (the version the Kiro dialog parser was measured on) and checked against a SHA-256. The image
holds no token and no profiles file. Its default command runs the pane holder
Expand Down Expand Up @@ -220,8 +223,10 @@ is in [docs/adr/restart-survival-and-shared-panes.md](docs/adr/restart-survival-
`deny_once` is answered with Esc; `allow_once` presses Enter only when the styled
screen cells show `Allow once` with a background that differs from the other two options, which match each other.
- **A fast detached descendant escapes cleanup.** A descendant that is re-parented
and fully detaches (new session, no terminal, parents gone) within the 250 ms
between looks at the process table is never seen and survives. macOS has no
and fully detaches (new session, no terminal, parents gone) before the first look
at the process table that would see it is never seen and survives. That look comes
within about 500 ms in the nominal case, and at most about 700 ms on Linux
provided each scan finishes within 250 ms, and about 450 ms on macOS. macOS has no
unprivileged way to close this gap. On Linux the holder is deliberately not a subreaper;
an orphan that escapes the scan is reaped by PID 1 (tini in the image).
- **Panes survive a server restart only under `hold`.** A bare `openab-shepherd` owns
Expand Down
6 changes: 3 additions & 3 deletions docs/adr/native-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ PaneState { vt: vt100::Parser<Hooks>, status, reported, turn, status_ms,
**Reader thread** (one per pane). It loops:

- `poll(master, POLLIN, 200 ms)`; on each wake it checks `shutdown`. `shutdown` is set only after the leader has been reaped (§11), so the terminal is drained for as long as any pane process may still be exiting.
- Every 250 ms (`TREE_POLL`), while the leader runs, it refreshes the descendant tree with `tree.try_lock()` (a stop in progress holds the tree; the reader must not block on it, or it would stop draining).
- Every 250 ms (`TREE_POLL`; the reader wakes every 200 ms, so a look comes at most about 450 ms after the previous one, see the known limit under "Descendant tree"), while the leader runs, it refreshes the descendant tree with `tree.try_lock()` (a stop in progress holds the tree; the reader must not block on it, or it would stop draining). On Linux that refresh may use the holder-wide process-table snapshot, up to `SNAPSHOT_MAX_AGE` (250 ms) old; a stop never does.
- It reads up to 16 KiB.
- `read` returning 0 or `EIO` means the slave side has closed, and the thread exits its loop. `EAGAIN` or `EINTR` means retry. Any other error is logged once (`native: <id>: read failed: <errno>`) and ends the loop.
- For each chunk, under `state`:
Expand Down Expand Up @@ -356,9 +356,9 @@ Nothing else passes, by construction (`env_clear()` first). In particular, `OPEN
- **Removal failures:** `NotFound` for an unknown ref. If `kill` on the group fails with an error other than ESRCH, it is logged at warn level and removal still completes; EPERM after the leader has exited is not logged, because macOS answers EPERM for a group left with only its zombie leader (measured).
- **Descendant tree (issue #10).** Measured on macOS with main `b446018`: kiro-cli starts `bun`, which starts `node …/acp-server.js` in a new session (`ps` STAT `Ss` without `+`: it called `setsid`, so it is a session and group leader with no controlling terminal; Node's `detached: true`). It is found only by the parent-pid walk from `bun`, never through the pane's group or terminal. Killing the pane's group never reached it, and after a pane ended it lived on with ppid 1 for over an hour. `procs::Tree` (in `src/native/procs.rs`) records every descendant as `(pid, start time)`:
- **Found by** parent pid, from the leader and from every member still alive (a breadth-first walk), and by membership of the leader's group and session: on Linux, `/proc/<pid>/stat` fields `ppid`, `pgrp`, `session` and `starttime`, from one scan of `/proc`; on macOS, `proc_listchildpids`, `proc_listpgrppids`, and `proc_listpids(PROC_TTY_ONLY, tdev)` with the leader's controlling terminal (`proc_bsdinfo.e_tdev`; macOS has no list-by-session call, and every process of the session has that terminal while the leader runs), with the start time from `proc_pidinfo(PROC_PIDTBSDINFO)` (`pbi_start_tvsec`/`usec`).
- **Refreshed** every 250 ms by the reader while the leader runs, and at the start of every soft and hard stop. A member is kept across refreshes while its pid still has the recorded start time, so a descendant whose parent exits (reparented to pid 1) stays known. Group and session lookups by the leader's pid run only while that pid is reserved (the leader is unreaped), and the reaper reaps under the tree lock, so no lookup can run on a freed pid.
- **Refreshed** by the reader while the leader runs, at most about 450 ms between looks (target 250 ms, see the known limit), and at the start of every soft and hard stop. A scan that could not read every process is neither cached nor allowed to prune: the tree keeps its members. While a read keeps failing, no scan is kept, so each pane's look takes its own scan until one reads every process. A member is kept across refreshes while its pid still has the recorded start time, so a descendant whose parent exits (reparented to pid 1) stays known. Group and session lookups by the leader's pid run only while that pid is reserved (the leader is unreaped), and the reaper reaps under the tree lock, so no lookup can run on a freed pid.
- **Signalled** only after re-reading the pid's start time and finding it unchanged. A reused pid has a later start time and is skipped. The check and the `kill` are a few microseconds apart; a pid is reused only after the pid space wraps, so a reuse inside that window is not a practical risk. Zombies are skipped (Linux state `Z`/`X`; macOS does not answer `proc_pidinfo` for zombies). Shepherd's own pid and pids 0 and 1 are never members.
- **Known limit (owner decision after review):** a descendant that is re-parented and fully detaches before the next tree poll (about 250 ms) is not seen: it calls `setsid` (no group, session or terminal link to the pane) and every ancestor between it and a known member exits first, as in a daemon's double fork. It survives removal, the program's own exit, shutdown and drop. One that only changes its process group is still found through the session (macOS: the terminal) while the leader runs, and one whose parent lives until the next poll is found by the parent-pid walk. Measured on macOS 15.7 (remote build host, a C probe with a `setsid` double fork): kqueue `EVFILT_PROC` with `NOTE_TRACK` is refused with `EV_ERROR`, data 45 (`ENOTSUP`); every process inherits its parent's coalition ids (`PROC_PIDCOALITIONINFO`), but creating a coalition needs root, so a pane cannot get its own; macOS has no subreaper and no cgroup; `p_puniqueid` (`PROC_PIDUNIQIDENTIFIERINFO`) survives the reparent to pid 1, but it names the intermediate that was never seen, so the chain stays broken. No public unprivileged macOS mechanism closes the gap. Linux can close it with `PR_SET_CHILD_SUBREAPER` in a per-pane supervisor; that is a follow-up to build and test with a Linux build.
- **Known limit (owner decision after review):** a descendant that is re-parented and fully detaches before the first look that would see it is not seen: it calls `setsid` (no group, session or terminal link to the pane) and every ancestor between it and a known member exits first, as in a daemon's double fork. On Linux that look comes at most about 700 ms after the fork, provided each scan finishes within the 250 ms snapshot age: a snapshot up to 250 ms old, plus looks at most about 450 ms apart because the reader wakes every 200 ms. A scan that runs longer, or a look that waits on another pane's scan under the shared lock, adds its own time to that bound. The nominal figure is 500 ms (250 ms snapshot age plus a 250 ms look interval). On macOS the bound is about 450 ms, because macOS caches nothing. It survives removal, the program's own exit, shutdown and drop. One that only changes its process group is still found through the session (macOS: the terminal) while the leader runs, and one whose parent lives until the next look is found by the parent-pid walk. Measured on macOS 15.7 (remote build host, a C probe with a `setsid` double fork): kqueue `EVFILT_PROC` with `NOTE_TRACK` is refused with `EV_ERROR`, data 45 (`ENOTSUP`); every process inherits its parent's coalition ids (`PROC_PIDCOALITIONINFO`), but creating a coalition needs root, so a pane cannot get its own; macOS has no subreaper and no cgroup; `p_puniqueid` (`PROC_PIDUNIQIDENTIFIERINFO`) survives the reparent to pid 1, but it names the intermediate that was never seen, so the chain stays broken. No public unprivileged macOS mechanism closes the gap. Linux can close it with `PR_SET_CHILD_SUBREAPER` in a per-pane supervisor; that is a follow-up to build and test with a Linux build.
- **Alternatives considered.** (a) Signal only the group or session: misses `setsid` children, and neither kernel has kill-by-session. (b) Snapshot the tree only at stop time: misses a descendant whose parent already exited (the measured orphan), so tracking is required. (c) Linux `PR_SET_CHILD_SUBREAPER` would reparent orphans to Shepherd, but macOS has no equivalent and Shepherd would then have to reap foreign children; one mechanism on both platforms is simpler to test. (d) A per-pane cgroup (Linux) or job object does not exist on macOS. (e) pidfds would close the check-to-kill window on Linux only; not worth a second code path given (c).
- **The zombie and the lost exit code (issue #10).** Measured: `remove_pane` on Kiro returned `stopped: killed, exit_code: null`, and the leader stayed `?Es` (exiting, not yet a zombie) with Shepherd as parent. Cause: removal set `shutdown` first, so the reader stopped reading the master. A process that exits with output still queued on its terminal waits in the tty close for it to drain; with the master open and unread, the leader stayed in that state (`?Es`, measured on macOS) and never finished exiting, so `waitid` never returned and no code was recorded. The same unread output is why Kiro, which writes as it restores the terminal, did not exit within the 2 s grace. Fix: the reader drains until the leader is reaped (`shutdown` is set after), and every stop path waits for the reaper. Reproduced in `cargo test` before the fix with `trap '' HUP TERM; exec yes` (killed, `exit_code: None`, `ps` showed `?Es`) and with a shell that writes 400 KB on SIGHUP (killed instead of exiting with its code).
- **Graceful stop for Kiro (decided).** SIGHUP and SIGTERM with the output drained, no keystrokes. Typing `/quit` would need Kiro to be idle and not in a dialog, and would put text into a pane the caller may not expect; Ctrl-D/Esc have the same problem (`interrupt_pane` exists for a clean turn end). Measured with `examples/native_leak_check` on a developer machine (kiro-cli 3.1.0): with the drain fix kiro-cli exited on the signals in 148 to 269 ms with code 1 in 4 of 5 runs (one of them with a 15 s grace); in the other it had not exited after 2 s and was killed (137, reaped, none of its processes left). Callers who want more time pass `remove_pane`'s `timeout`.
Expand Down
34 changes: 25 additions & 9 deletions docs/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -800,19 +800,34 @@ Pane ids are `n:<k>`
`OPENAB_SHEPHERD_BACKEND` and everything else never reach a child. Shepherd's own descriptors are close-on-exec and native spawns are serialized, so one pane's terminal does not reach another pane. Shepherd does not sweep descriptors inherited from its parent; launch it with a clean descriptor table.
- **Process groups and descendants.** Each child is a session and group leader with
the PTY as its controlling terminal. Shepherd also tracks every descendant of the
child, by pid and start time: by parent pid every 250 ms while the child runs and
again before any stop, and by membership of the child's group and session. So a
descendant that moves to its own group or session, or whose parent exits, is
still known. Removing a pane (`remove_pane`) and Shepherd's exit on SIGINT,
child, by pid and start time: by parent pid at each periodic look while the child
runs (every 250 ms as the target; at most about 450 ms apart because the reader
wakes every 200 ms) and again before any stop, and by membership of the child's
group and session. So a descendant that moves to its own group or session, or
whose parent exits, is still known. On Linux the periodic look reads the process
table once for all panes, and a look may use a read up to 250 ms old. A
descendant is seen within about 500 ms in the nominal case, and at most about
700 ms provided each scan finishes within the 250 ms age limit (see Known
limits). A stop always reads it afresh. The snapshot lists every process
on the host, so its memory grows with the host's process count (measured on
the Linux build host at 302 processes: about 720 KiB more than the isolated
base run and about 864 KiB more than the concurrent base run). A scan that
cannot read every process is not kept, and it prunes nothing: under a lasting
read failure a tree keeps a member until a complete scan no longer lists it.
While the failure lasts, no scan is kept, so each pane's look takes its own
scan, as it did before this change. Each signal still re-checks the
start time. Removing a pane (`remove_pane`) and Shepherd's exit on SIGINT,
SIGTERM or SIGHUP send SIGHUP and SIGTERM to the group and the descendants, wait
up to 2 s (for `remove_pane`, its `timeout`), then stop (SIGSTOP) the group and
descendants until no new one appears and SIGKILL them all. When the program exits
on its own, what it left behind gets the same treatment at once. The leader stays
an unreaped zombie only during that stop, so its pid (the group and session id)
cannot be reused while Shepherd signals by it; a descendant is signalled only
while its pid still has the recorded start time. Known limit: a descendant that
is re-parented and fully detaches (new session, no terminal) before the next
look at the process table (about every 250 ms) is never seen and survives
is re-parented and fully detaches (new session, no terminal) before the first
look that would see it (at most about 700 ms after its fork on Linux, provided
each scan finishes within 250 ms; about 450 ms on macOS; see Known limits) is
never seen and survives
(see Known limits). If Shepherd is killed with SIGKILL, children get only the
PTY hangup.
- **Status.** OSC 7501 reports from the program are parsed in the emulator's
Expand Down Expand Up @@ -1649,9 +1664,10 @@ try to update itself at run time).
without cursor save) is not emulated.
- **Native: a fast daemon escapes.** A descendant that is re-parented and fully
detaches (calls `setsid`, so it has no group, session or terminal link to the
pane, and its parents exit) before the next look at the process table, about
every 250 ms, is never seen. It survives `remove_pane`, the program's own exit,
and Shepherd's shutdown. One that only changes process group, or stays under a
pane, and its parents exit) before the first look that would see it, is never
seen. That look comes at most about 700 ms after the fork on Linux, provided each
scan finishes within 250 ms, and about 450 ms on macOS. It survives
`remove_pane`, the program's own exit, and Shepherd's shutdown. One that only changes process group, or stays under a
live parent until the next look, is ended (issue #10). macOS offers no
unprivileged way to close this: kqueue `NOTE_TRACK` answers `ENOTSUP`, only root
can create a coalition, and there is no subreaper or cgroup (measured on macOS
Expand Down
Loading
Loading