Skip to content

Piped grep -q under pipefail: 4 live sites, a single-file guard, and a rule scoped too narrowly to find #641

Done

Description

@athvin

Follow-on from #591 / #608. Not a reopening of either — both are merged and the bug they chased is fixed. This is the class of defect they exposed, which is still live in two other scripts and not guarded against anywhere outside one file.

Background in one paragraph

writer | grep -q needle under set -euo pipefail is a race, not a check. grep -q exits the instant it matches, closing the pipe while the writer is still producing output; the writer takes SIGPIPE and pipefail makes the pipeline exit 141 — so a needle that was present reports as absent. It passes whenever the writer finishes first, which is nearly always locally and on an idle runner. #608 fixed the one instance that was actually firing (skill_body | grep -qF, which failed agent skills (lint + drift) on #606 against a tree that did contain the match).

What is still live on main

Verified against origin/main, excluding file-argument forms (grep -qF needle FILE has no writer to signal and is safe) and excluding scripts/ci/skills-check-test.sh, where the pre-fix form appears deliberately as a mutation control:

Site Writer Exposure
scripts/kind_env.sh:49 kind get clusters — an external binary, output not bounded by the script highest of the three; a subprocess whose buffering we do not control
scripts/ci/actionlint.sh:109 head -5 "$wf" small — head exits after 5 lines
scripts/skills-check.sh:166 printf '%s' "$desc" small — description is capped at 1024 chars by the lint above it
scripts/skills-check.sh:625 head -3 "$wf" small — head exits after 3 lines

All five files set pipefail, so the mechanism is armed in each.

Honest severity: low, and that is the problem. Every remaining writer is small enough that it has probably never lost the race, and may never. The reason to do this is not that these four are likely to fire — it is that the two structural gaps below mean the next one is equally invisible, and the one that did fire cost a wrong-looking CI failure on an unrelated PR plus a long detour re-verifying the tree three ways before anyone suspected the plumbing.

The two structural gaps

1. The guard is single-file. scripts/ci/skills-check-test.sh has a regression check for exactly this pattern, and it is good — but it scans only $SCRIPT (skills-check.sh) and allowlists the two small-writer lines with grep -v:

piped_q=$(grep -n '| *grep -q' "$SCRIPT" | grep -v "printf '%s' \"\$desc\"" | grep -cv 'head -3' || true)

kind_env.sh and actionlint.sh are invisible to it.

2. The written rule is scoped too narrowly to be found. .claude/skills/ci-and-supply-chain/SKILL.md:77 already states it:

Every detect job uses grep -c, not grep -q. -q closes the pipe as soon as it matches, which can SIGPIPE the git diff feeding it under pipefail.

That is correct and it predates the bug. But it is phrased as a property of detect jobs, so it does not read as applying to an unrelated shell script — which is exactly why it was not applied when names_skill_in_backticks was written. scripts/ci/tf-helpers-test.sh:150 carries a comment about the same hazard for a third reason again. The rule is true repo-wide and recorded in three places, none of which say so.

Scope

  • Convert the four live sites to a non-piped form. The idiom fix(skills): a found match reported as missing — grep -q SIGPIPEd awk #608 settled on, which needs no subprocess:
    sh body=$(producer) [[ $body == *"$needle"* ]]
    For a filtered intermediate: matched=$(printf '%s\n' "$x" | grep -F "$a" || true); [[ $matched == *"$b"* ]]. grep -c also works — it drains its input — and is what the detect jobs use.
  • Decide explicitly for the two small-writer sites in skills-check.sh: convert them, or keep the allowlist and say in the guard why each is exempt. Either is defensible; the current state is an allowlist with no stated reason, which reads as an oversight.
  • Promote the guard from one file to a repo-wide sweep over *.sh (and consider workflow run: blocks, though infra-ci-plan-apply notes the default run: shell is bash -e with no pipefail, so those are not exposed by default — confirm before widening).
  • Generalise the rule's wording in ci-and-supply-chain so it is about any piped grep -q under pipefail, not about detect jobs specifically, and cross-reference it from the tf-helpers-test.sh comment.

Testing

The method is already established by #608 and should be reused rather than reinvented: a race is made deterministic by forcing the losing side — drive more than one pipe buffer (>64 KiB) past the match so the writer must block and must take SIGPIPE. scripts/ci/skills-check-test.sh does this with a 239 kB body, and carries both a negative control and an assertion that the pre-fix implementation fails the same case, which is what proves the signal is really provoked on the Linux runner.

Plain mutation testing does not catch this class — it exercises the verdict, not the timing. That is the reusable lesson.

Not in scope


Product context (2026-11-26)

Why this matters for the API product: Indirect, it's about merge-gate trust. A piped grep -q under pipefail can report a present match as absent, which turns into a false skip or a false failure in the gates that guard API changes.

Current state: Nothing in scope has landed. No PR references #641. The class has grown since the ticket was filed.

  • The original four sites are still live, three of them at new line numbers: scripts/kind_env.sh:49, scripts/ci/actionlint.sh:126 (was 109), scripts/skills-check.sh:300 (was 166) and scripts/skills-check.sh:1101 (was 625).
  • There are new sites on files that set pipefail:
    • scripts/ci/workflow-shell-check.sh:66 (head -5 | grep -qiE, the same shape as actionlint);
    • scripts/ci/images-detect.sh:25 (printf "$changed" | grep -qE, a detect script, which the existing rule covers explicitly);
    • scripts/cluster_objects.sh:132,138;
    • scripts/worktree-audit.sh:155;
    • scripts/ci/agent-publish.sh:10 and agent-task-ids.sh:24.
  • Test-only sites sit in actionlint-required-test.sh and alert-path-check-test.sh.
  • Every current writer is small (well under 64 KiB), so the practical risk is still low.
  • The guard is still single-file: scripts/ci/skills-check-test.sh:144 greps only $SCRIPT.
  • The rule is still worded for detect jobs only: ci-and-supply-chain/SKILL.md:81.
  • Correct non-piped idioms are already in the tree as examples: tf-static-credentials-check.sh:76 and devcontainer-poststart.sh:82.

Definition of done: As above. Also, the repo-wide sweep must cover the new files listed here, plus any *.sh added later.

Dependencies / sequencing: None.

Size: ~0.5 engineer-weeks: convert about 12 sites, write one repo-wide guard with its >64 KiB control, and reword the skill.

Activity

  1. added
    enhancement
    on Nov 6, 2026
  2. athvin commented on Nov 6, 2026

    @athvin
    OwnerAuthor

    Confirming the scope critique, from the author of #608: the guard I added is single-file by design and your reading of it is correct.

    scripts/ci/skills-check-test.sh's last assertion greps $SCRIPT only — scripts/skills-check.sh — so it cannot see the other live sites. I hardened two instances in that one file (names_skill_in_backticks, and the O4 row-pairing check which had the same shape with a smaller writer) and did not sweep the rest of the tree. So #608 fixed the instance that was firing, not the class.

    Two things from #608 that may save you time here:

    • The deterministic repro. Drive >64 KiB past the match so the writer must block on a full pipe buffer; then SIGPIPE is certain rather than likely. skills-check-test.sh uses a 239 kB body with the match at line 6.
    • Plain mutation tests do not catch it — you note this in your own memory and it held: they exercise the verdict, not the timing. What does catch it is asserting the pre-fix form fails the same input. That assertion passing on the Linux runner is what proved the SIGPIPE was real rather than argued.

    Both are reusable as-is if the fix here ends up being a shared helper plus one suite.

  3. added
    followup
    and removed
    enhancement
    on Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    followup

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions