Skip to content

fix(herdr): exec the server launch so no forked shell waits on it - #1

Merged
onyx-space merged 3 commits into
mainfrom
fm/herdr-server-exec
Sep 10, 2026
Merged

onyx-space merged 3 commits into
mainfrom
fm/herdr-server-exec

Conversation

@onyx-space

@onyx-space onyx-space commented Sep 10, 2026 •

Copy link
Copy Markdown
Owner

Intent

队长原话:「先评估,确定就提 pr」——指的是他工作区里那份未提交的 herdr 改动。

背景:那份改动是本 home 工作区当时唯一的未提交改动(文件时间 2026-09-10 14:50,早于本次会话)。我按队长先前的指示把它原样保存在本地分支 fm/herdr-server-exec-preserve(commit 1f36d88,未推送),才把 main 快进到上游最新(b1ad702)。现在队长要求:评估后确认就提 PR。

firstmate 的评估结论(作为本次 PR 的依据,不代替你在管线里的验证):

  • 只有一处真实行为变更:fm_backend_herdr_server_ensure 启动 server 那一行,从 fm_backend_herdr_cli "$session" server >/dev/null 2>&1 & 改成新函数 fm_backend_herdr_server_exec "$session" >/dev/null 2>&1 &(新函数 exec herdr,不再留一个等待 server 的 bash 包裹 shell)。其余两处是新函数本体与注释订正。
  • 合理性的外部佐证:上游自己在新的远程路径上就是这么做的——bin/fm-remote-herdr-guard.sh:69 正是 exec "$HERDR_BIN" server --session "$SESSION",bin/fm-remote-herdr-owner-lib.sh 还专门处理 server 的 “birth” 归属。这份改动把 server_ensure 的启动形态对齐到同一形态。
  • 需要你验证的点:改动注释里那句机制描述(“bash 把重定向保存的 stdout/stderr 留在高位 fd 上,于是 ssh 通道被占住”)是否就是真实原因。如果实际机制不同(例如是 stdin 继承,或 server 的父进程链影响 birth 归属),按证据改写注释,不要把未验证的机制写进仓库。

What Changed

  • fm_backend_herdr_server_ensure 现在通过新的 fm_backend_herdr_server_exec 启动命名服务器,该函数直接 exec herdr,因此在服务器生命周期内不再有 fork 出来的 bash shell 作为其父进程等待它。
  • 在 bin/backends/herdr.sh 中添加了 fm_backend_herdr_server_exec,镜像 fm_backend_herdr_cli 的客户端选择逻辑,使同一 session 继续使用已选择的客户端。
  • 修正了 herdr-backend.md 的传输描述,并添加了一个 regression test,断言在 server_ensure 之后服务器不再以 fork 出来的 shell 作为父进程。

Risk Assessment

✅ Low: The change is a single, well-bounded behavior change with a matching behavioral test and an accurate, intent-satisfying comment correction; no correctness, conformance, or reachable-failure issues were found.

Testing

围绕用户意图(验证 exec 启动改动的真实机制、以及更正后注释是否准确)做了聚焦验证:运行了新增回归测试与相邻的 scrubs 测试(均通过),并把 herdr.sh 临时回退到 base 后确认新测试会失败(证明它是真回归测试);再用 fake herdr 做进程树对比和 fstat/stdin 实证,确认『遗留 bash 父进程(进程卫生)』才是真实机制、且后台 shell 三个 fd 均为 /dev/null(即确非 fd/ssh 通道问题)。结论:改动正确、注释准确,无需改写,无发现项。

Evidence: 进程树对比(旧 vs 新启动形态)

Source: 进程树对比(旧 vs 新启动形态)

OLD (fm_backend_herdr_cli ... server &): server parent_comm=bash → 遗留 fork shell 空等 server NEW (fm_backend_herdr_server_exec ... &): server parent=1 (/sbin/launchd) → 无遗留 bash

\### OLD launch shape (fm_backend_herdr_cli ... server &)
  server pid=73574  parent pid=73573  parent_comm=bash
  => stray forked bash (pid 73573) is the server's parent and stays alive waiting on it

\### NEW launch shape (fm_backend_herdr_server_exec ... &)
  server pid=73616  parent pid=1  parent_comm=/sbin/launchd
  => server reparented to init (pid 1); no forked bash is left behind
Evidence: fd 实证(后台启动 shell 的 0/1/2 均为 /dev/null)

Source: fd 实证(后台启动 shell 的 0/1/2 均为 /dev/null)

fd0=CHR ino=336 /dev/null fd1=CHR ino=336 /dev/null fd2=CHR ino=336 /dev/null

backgrounded subshell fds after `cmd >/dev/null 2>&1 &`:
  fd0=CHR ino=336 /dev/null
  fd1=CHR ino=336 /dev/null
  fd2=CHR ino=336 /dev/null
Evidence: stdin 重定向实证(非交互异步命令 stdin 被 bash 置为 /dev/null)

Source: stdin 重定向实证(非交互异步命令 stdin 被 bash 置为 /dev/null)

async cmd fd0: CHR ino=336 /dev/null

async cmd fd0: CHR ino=336 /dev/null

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - passed

✅ No issues found.

✅ **Test** - passed

✅ No issues found.

  • bash tests/fm-backend-herdr.test.sh (focused) → test_server_ensure_leaves_no_forked_shell_waiting_on_the_server — 通过
  • bash tests/fm-backend-herdr.test.sh (focused) → test_server_ensure_scrubs_home_and_harness_identity — 通过
  • 回归验证:将 bin/backends/herdr.sh 临时回退到 base b1ad702 后重跑新测试 → 失败(『left parented to a forked shell (bash)』),随后恢复目标版本并确认工作树干净
  • 手动进程树对比(旧 vs 新启动形态,fake herdr server 记录 PPID)→ 旧:parent_comm=bash(遗留 fork shell);新:parent=1(重父到 init/launchd)
  • fd 实证:后台启动 subshell 内 fstat(0/1/2) → 三者均为 CHR ino=336 /dev/null(无 caller 描述符)
  • stdin 重定向实证:父进程 stdin 为真实管道时,非交互异步命令 fd0 仍为 /dev/null(印证『bash 分配非交互异步命令 stdin 自 /dev/null』)
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

Backgrounding the long-lived `herdr server` launch through
fm_backend_herdr_cli forked a shell that then did nothing but wait on the
server, so every session carried a stray `bash` as the server's parent for
the server's whole lifetime. Route that launch through the new
fm_backend_herdr_server_exec, which exec's the server in the forked child so
nothing is left behind, and drop the fm_backend_herdr_cli comment that called
the old shape an exec when it was not one.

The wrapper held no caller file descriptor: bash assigns a non-interactive
asynchronous command's stdin from /dev/null, and the caller already redirects
stdout/stderr. This is process hygiene, not the channel-leak the discarded
comment claimed.

Pin the shape in tests/fm-backend-herdr.test.sh with a parent-observing herdr
stub: after fm_backend_herdr_server_ensure returns, the server must not be
left parented to a forked shell. The case fails on the old launch shape and
passes on the new one.
The document step's commit committed an unreferenced three-line stub named
fake_server at the repository root; it was a leftover from the pipeline
agent's own parent-process probing and nothing in the tree refers to it.
Remove it, keeping that commit's docs/herdr-backend.md correction.
@onyx-space onyx-space changed the title fix(herdr): exec server launch so no forked shell waits on it fix(herdr): exec the server launch so no forked shell waits on it Sep 10, 2026
@onyx-space
onyx-space force-pushed the fm/herdr-server-exec branch 2 times, most recently from f35de9f to 9dff146 Compare September 10, 2026 10:08
@onyx-space
onyx-space merged commit 300137d into main Sep 10, 2026
63 of 68 checks passed
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.

1 participant