Skip to content

fix(bin): make fm_exec_timed owner check safe on Bash 3.2 without BASHPID - #5849

Closed
aminry wants to merge 3 commits into
kunchenguid:mainfrom
aminry:fm/fm-bashpid-timeout-lib-b5
Closed

aminry wants to merge 3 commits into
kunchenguid:mainfrom
aminry:fm/fm-bashpid-timeout-lib-b5

Conversation

@aminry

@aminry aminry commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Intent

Fix the Bash 3.2 regression in bin/fm-timeout-lib.sh, filed upstream as #5847. The PR body must contain the line: Closes #5847

Since #5770, fm_exec_timed reads a bare $BASHPID under set -u (bin/fm-timeout-lib.sh:209: [ "$owner" != "$BASHPID" ] || owner=$PPID). Stock macOS Bash 3.2 has no BASHPID, so every caller dies with "BASHPID: unbound variable". In practice bin/fm-spawn.sh on a stock Mac cannot move the backlog item to In flight and aborts, leaving the launched agent window running. The rest of bin/ uses the portable ${BASHPID:-$$} form, and #3789 covers Bash 3.2 subshell-ownership semantics. The issue asks to make the owner check Bash 3.2 safe without changing its meaning on Bash 5, with a test that runs fm_exec_timed under /bin/bash 3.2 when available.

What Changed

  • Added fm_exec_timed_self_pid to bin/fm-timeout-lib.sh. It returns the current process's own pid, including inside a subshell. It uses BASHPID when it is set. On Bash 3.2, which has no BASHPID, it starts a child sh directly (not through command substitution) and reads that child's $PPID from a temp file. A command substitution would run in a forked subshell and report the wrong pid.
  • fm_exec_timed now does its owner check against this helper instead of reading a bare $BASHPID. Callers no longer die with BASHPID: unbound variable under set -u on stock macOS Bash 3.2. On Bash 5 the check means the same thing as before.
  • Tests in tests/fm-timeout-lib.test.sh, covering /bin/bash, Homebrew bash and the bash on PATH where each exists:
    • One test runs fm_exec_timed under set -u, both in a subshell and at top level.
    • Another compares fm_exec_timed_self_pid against the parent pid that ps reports for a background child. It does this at top level, in subshells, in nested subshells and inside functions, with a PATH that has no sh.
    • The existing tests that read $BASHPID now use the helper.

Closes #5847

🤖 Generated with Claude Code

Risk Assessment

✅ Low: The change is small and bounded. On Bash 5 it still uses BASHPID, so the owner check means the same thing as before. On Bash 3.2 it gets the process's own pid from a child sh started directly by that process, which writes its PPID to a temp file, as the user asked in the review-1 fix. That pid is correct at top level, in subshells and in functions. The new tests run under /bin/bash and compare the resolved pid with ps's PPID, so they would catch both the original unbound-variable crash and the previous command-substitution bug.

Testing

I ran the targeted fm-timeout-lib test file under both /bin/bash 3.2.57 and Homebrew Bash 5, and all cases pass on both. I then drove the real fm-spawn backlog path, a bounded tasks-axi start that moves the item to In flight, under /bin/bash 3.2. The base commit dies with "BASHPID: unbound variable" and the task stays queued. HEAD moves the task to in_flight with status 0. In an adversarial run the owner dies before the watchdog starts, at top level under Bash 3.2. With HEAD the bounded command ended about 2s later, well before its 20s deadline. With the rejected command-substitution form it was still running after 12s, so the check can tell the two apart. There is no UI surface, so the evidence is CLI transcripts. I did not start a full fm-spawn with a live agent window in a lab, because the timeout lib call it depends on was driven directly with the real tasks-axi. The self-pid and Bash 5 regression scenarios were covered only by unit tests, so they are recorded as untested for live validation.

  • Live validation: ✅ go - 2 of 4 scenarios driven live against the product
Scenario Result Live Evidence
On stock macOS /bin/bash 3.2 under set -u, a bounded tasks-axi call (the one fm-spawn uses to move a backlog item to In flight) runs, and the item ends up in_flight ✅ pass live tasks-axi-bash32-before-after.txt: base 30ef650 prints 'line 209: BASHPID: unbound variable' and the task stays queued. HEAD prints 'ok: start demo-a1 -> In flight', status=0, and the state is in_flig…
Adversarial: under Bash 3.2 at top level, if fm_exec_timed's parent dies before the watchdog reads getppid, the bounded command is terminated long before its deadline ✅ pass live owner-dies-before-watchdog-bash32.txt: HEAD ended the command 2s after the owner died (deadline 20s). The counterfactual $(sh -c 'echo $PPID') form left it running after 12s
The self-pid resolves to the real current process at top level, in a function, in a subshell, and in a nested subshell, under Bash 3.2 and Bash 5 ⏸️ untested no The prior payload established this only through the unit test tests/fm-timeout-lib.test.sh, not a live product run, so no live result was established.
The owner check and timeout behavior are unchanged on Bash 5 (status passthrough, TERM/KILL escalation, owner-death handling, exec replaces caller) ⏸️ untested no The prior payload established this only by running the unit test file under Homebrew Bash 5, not a live product run, so no live result was established.
Evidence: tasks-axi move to In flight under Bash 3.2: base crashes, fix succeeds

Source: tasks-axi move to In flight under Bash 3.2: base crashes, fix succeeds

\### Scenario: 'tasks-axi start' (move to In flight) through fm_exec_timed, as fm-spawn's bounded fm_tasks_axi does, under /bin/bash 3.2.57(1)-release
--- base lib (30ef650)
/var/folders/wl/vj3pkxrj1y53bnydd3h_c68w0000gn/T/tmp.7x34505Eus/base-lib.sh: line 209: BASHPID: unbound variable
outer rc=1
state after:
task:
  id: demo-a1
  title: demo task
  state: queued
  blocked: no
--- fixed lib (HEAD)
ok: start demo-a1 -> In flight
help[1]:
  - Run `tasks-axi done demo-a1 --pr <url> --file=/var/folders/wl/vj3pkxrj1y53bnydd3h_c68w0000gn/T/tmp.7x34505Eus/BACKLOG.md` when it ships
status=0
outer rc=0
state after:
task:
  id: demo-a1
  title: demo task
  state: in_flight
  blocked: no
Evidence: Owner dies before watchdog starts at top level under Bash 3.2: fix terminates, counterfactual does not

Source: Owner dies before watchdog starts at top level under Bash 3.2: fix terminates, counterfactual does not

\### Adversarial: top-level fm_exec_timed under /bin/bash 3.2.57(1)-release; owner dies before the watchdog reads getppid
HEAD (fix): bounded command ended 2s after its owner died (owner check live; deadline was 20s)
counterfactual $(sh -c 'echo $PPID'): bounded command STILL RUNNING 12s after its owner died (owner check disabled)
Evidence: fm-timeout-lib test file under /bin/bash 3.2 and Homebrew bash 5

Source: fm-timeout-lib test file under /bin/bash 3.2 and Homebrew bash 5

== /bin/bash
ok - fm_exec_timed passes a command's status and output through unchanged
ok - fm_exec_timed_self_pid names this process under every bash, in and out of subshells
ok - fm_exec_timed sends TERM at the bound and a cooperative command ends there
ok - fm_exec_timed kills a TERM-ignoring command once the grace has passed
ok - fm_exec_timed's owner check works without BASHPID
ok - fm_exec_timed replaces the calling shell instead of wrapping it
ok - fm_exec_timed reaps a descendant that would otherwise hold the output past the bound
ok - fm_exec_timed forwards a TERM it receives to the bounded command
ok - fm_exec_timed ends the command when its named owner is already gone
ok - fm_exec_timed ends the command when its owner dies during watchdog startup
ok - fm_exec_timed prefers its perl watchdog over timeout
ok - fm_exec_timed refuses instead of running unbounded when no mechanism exists
ok - fm_exec_timed rejects a zero, padded, non-numeric, or missing bound and a missing command
tests/fm-timeout-lib.test.sh: line 28: 55152 Killed: 9               ( . "$ROOT/bin/fm-timeout-lib.sh"; PATH=$path fm_exec_timed "$@" )
ok - fm_exec_timed's GNU timeout fallback kills a TERM-ignoring command once the grace has passed
ok - fm_timed_out accepts 124 and 137 and nothing else
rc=0
== /opt/homebrew/bin/bash
ok - fm_exec_timed passes a command's status and output through unchanged
ok - fm_exec_timed_self_pid names this process under every bash, in and out of subshells
ok - fm_exec_timed sends TERM at the bound and a cooperative command ends there
ok - fm_exec_timed kills a TERM-ignoring command once the grace has passed
ok - fm_exec_timed's owner check works without BASHPID
ok - fm_exec_timed replaces the calling shell instead of wrapping it
ok - fm_exec_timed reaps a descendant that would otherwise hold the output past the bound
ok - fm_exec_timed forwards a TERM it receives to the bounded command
ok - fm_exec_timed ends the command when its named owner is already gone
ok - fm_exec_timed ends the command when its owner dies during watchdog startup
ok - fm_exec_timed prefers its perl watchdog over timeout
ok - fm_exec_timed refuses instead of running unbounded when no mechanism exists
ok - fm_exec_timed rejects a zero, padded, non-numeric, or missing bound and a missing command
tests/fm-timeout-lib.test.sh: line 28: 56506 Killed: 9                  ( . "$ROOT/bin/fm-timeout-lib.sh"; PATH=$path fm_exec_timed "$@" )
ok - fm_exec_timed's GNU timeout fallback kills a TERM-ignoring command once the grace has passed
ok - fm_timed_out accepts 124 and 137 and nothing else
rc=0

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

🔧 **Review** - 1 issue found → auto-fixed ✅
  • 🚨 bin/fm-timeout-lib.sh:212 - On Bash 3.2, self=$(sh -c &#39;echo $PPID&#39;) does not return this process's pid. Bash 3.2 forks a subshell for the command substitution and does not exec the lone simple command inside it, so sh reports that throwaway subshell's pid. I checked this on this host's /bin/bash 3.2.57: with $$=95176 the form returns 95177, and inside a function it returns yet another pid. Bash 5 happens to exec it and gets the right answer. The result is that self never matches owner on Bash 3.2. When fm_exec_timed is the shell's own last command (for example fm_exec_timed 5 1 cmd at top level of fm-spawn.sh), owner stays $$. After exec perl, $$ is the watchdog itself, so kill(0, $owner) always succeeds and the documented 'else the shell's parent' owner check is silently disabled. The case it covers, a parent that dies before the watchdog has read getppid(), goes undetected, and the bounded command can run until its deadline instead of starting the grace escalation. The new comment ('ask a child shell for its parent: that is this process') is also false for this form. Fix: use the exec form already used at bin/fm-wake-lib.sh:39, self=$(exec sh -c &#39;echo $PPID&#39;). I checked on /bin/bash 3.2 that it returns $$ at top level and the subshell's own pid inside ( ... ). The new test only checks for the absence of 'unbound' and the exit codes, so it cannot see this. A test that asserts which owner is chosen at top level under /bin/bash would catch it (for example, kill the parent shell right after launch and expect the command to be terminated before its deadline).

🔧 Fix applied.
✅ Re-checked - no issues remain.

✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 2 of 4 scenarios driven live against the product
Scenario Result Live Evidence
On stock macOS /bin/bash 3.2 under set -u, a bounded tasks-axi call (the one fm-spawn uses to move a backlog item to In flight) runs, and the item ends up in_flight ✅ pass live tasks-axi-bash32-before-after.txt: base 30ef650 prints 'line 209: BASHPID: unbound variable' and the task stays queued. HEAD prints 'ok: start demo-a1 -> In flight', status=0, and the state is in_flig…
Adversarial: under Bash 3.2 at top level, if fm_exec_timed's parent dies before the watchdog reads getppid, the bounded command is terminated long before its deadline ✅ pass live owner-dies-before-watchdog-bash32.txt: HEAD ended the command 2s after the owner died (deadline 20s). The counterfactual $(sh -c 'echo $PPID') form left it running after 12s
The self-pid resolves to the real current process at top level, in a function, in a subshell, and in a nested subshell, under Bash 3.2 and Bash 5 ⏸️ untested no The prior payload established this only through the unit test tests/fm-timeout-lib.test.sh, not a live product run, so no live result was established.
The owner check and timeout behavior are unchanged on Bash 5 (status passthrough, TERM/KILL escalation, owner-death handling, exec replaces caller) ⏸️ untested no The prior payload established this only by running the unit test file under Homebrew Bash 5, not a live product run, so no live result was established.
  • /bin/bash tests/fm-timeout-lib.test.sh (Bash 3.2.57): all 15 cases ok, including the new owner-check and self-pid cases
  • /opt/homebrew/bin/bash tests/fm-timeout-lib.test.sh (Bash 5): all 15 cases ok
  • Before/after run under /bin/bash 3.2 with set -euo pipefail: (fm_exec_timed 10 10 tasks-axi start demo-a1 --file $T/BACKLOG.md) against a real tasks-axi markdown backlog, with base 30ef650 lib vs HEAD lib
  • Adversarial run under /bin/bash 3.2: top-level fm_exec_timed 20 1 ... sleep 300 whose parent (the owner) exits before the watchdog reads getppid (perl start delayed by a wrapper). Run once with HEAD and once with a counterfactual using the rejected $(sh -c &#39;echo $PPID&#39;) form
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

…ataflow stays clean

A dynamic printf -v assignment in the sourced library made ShellCheck treat
every variable, $! included, as modified in any subshell that sources it,
failing the canonical lint partition with SC2031.
@kunchenguid

Copy link
Copy Markdown
Owner

Thanks for this fix, @aminry, and sorry it sat so long. The same BASHPID fix for #5847 landed in #6028 (merge 5838f10), so I'm closing this one as superseded. Your report helped get it over the line. (Kun's firstmate triage)

@kunchenguid kunchenguid closed this Oct 6, 2026
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.

fm-timeout-lib.sh: bare $BASHPID breaks fm-spawn on stock macOS Bash 3.2

2 participants