$RodHat_/search
Rod's Tales

The counter said zero and the loop had counted forty.

Published by

This article may contain Amazon affiliate links. I may earn a commission if you buy through them. Disclosure.

Editorial card: the counter said zero and the loop had counted forty.
Editorial card: RodHat
On this page · 5 sections
  1. The loop that counted in a different universe
  2. It is not the same everywhere, which is worse
  3. Proving it
  4. The fixes, in order of how much I trust them
  5. What we changed

The deploy gate had one job: scan the preflight log, count the lines marked FAIL, and refuse to ship if the count was above zero. It printed failures: 0 and shipped. Every time. For months. The log it was reading had forty FAIL lines in it the day I finally opened it.

The gate was eleven lines of shell. I will save you the suspense: it was the pipe.


The loop that counted in a different universe

The script looked like this, give or take the variable names:

fails=0
grep FAIL preflight.log | while read line; do
    fails=$((fails + 1))
done
echo "failures: $fails"
[ "$fails" -eq 0 ] || exit 1

Read it the way a sane person reads it and it is correct. The loop increments fails once per line. The echo happens after. It prints zero anyway.

Each stage of a pipeline is its own process. In bash the while on the right side of the pipe runs in a subshell, a forked child with a copy of the parent’s variables. The child increments its copy to forty, finishes, and exits. The copy goes with it. The parent never saw a thing, and its fails is still the zero it was initialised to. A child process cannot write to its parent’s memory, and that is not a shell quirk, that is how fork(2) has worked since before most of you were born.

The part that makes this a trap is that the loop body works. Add an echo "$fails" inside the loop and you will see 1, 2, 3, all the way to 40, cheerfully correct. The bug only exists on the far side of done.


It is not the same everywhere, which is worse

POSIX does not pin down which element of a pipeline, if any, runs in the current shell. Bash runs all of them in subshells by default. ksh93 and zsh run the last one in the current shell, so the same script works there and fails in bash. I have watched a script get written on a laptop with zsh, pass every test, and fail quietly on the production host with /bin/bash or plain sh. Check your own shell’s man page for how it behaves instead of trusting my memory of a particular version, because the answer differs and has changed over the years.

A gate that works on the author’s machine and silently passes on the server is the worst species of bug, because the failure mode is “everything is fine”. Same family as a pipe into gzip that exited zero over half a database: the shell told the truth about what it did, and what it did was not what you meant.


Proving it

Do not argue with the shell, ask it who it is. Put echo "pid $$ bashpid $BASHPID" inside the loop and again after it. In bash, $$ stays the parent’s PID everywhere but $BASHPID changes inside the subshell. Different BASHPID means you are in a child. That is the whole diagnosis, and it takes ten seconds.


The fixes, in order of how much I trust them

1. Do not pipe into the loop. Redirect into it.

while read line; do
    fails=$((fails + 1))
done < <(grep FAIL preflight.log)

This is bash process substitution. The loop now runs in the current shell and the grep is the one in the child. It is not POSIX, so it will not parse in a strict sh. If the script says #!/bin/sh, believe it and do not use it.

2. The portable version: write to a file, or use a here-document. Run grep into a temp file made with mktemp and done < "$tmp". Boring, portable, works in every Bourne descendant, and gives you something to look at when it breaks. I like things I can look at.

3. shopt -s lastpipe in bash. Makes the last stage run in the current shell. It only takes effect when job control is off, which means it works in a script and does nothing in your interactive terminal. That is how you get the fun of testing it by hand, watching it fail, and concluding the option is broken.

4. Stop counting in a loop at all. Here I have to eat my own grumbling. The loop was never needed. grep -c FAIL preflight.log returns the count directly, and grep -q gives you the yes or no. Eleven lines became two. I wrote a loop to do what grep does with one flag, because I wanted to do something with each line and never did. Years of shell and I still reach for the loop first. The reversal is not fun.

One more: grep -c exits 1 when the count is zero, which will bite you under set -e. Test for the count, not the exit status.


What we changed

The gate now uses grep -c, and it fails closed: if the log is missing or empty the gate refuses to ship, because “no FAIL lines” and “no log” had been the same answer. That is the same lesson as the tail loop that went silent after rotation: a check that cannot distinguish “nothing wrong” from “nothing looked” is not a check. I also covered how pipeline exit statuses lie if you want the other half of this.

Forty failures had been shipping to production under a banner that said zero. Nobody was hurt, mostly because most of the FAIL lines were noise. Mostly.

More like this live in the reliability hub.