A Bash function in parentheses made my push rerun its tests

No. 017 · · 6 min read

Slow gate series: Part 1: Hidden failures · Part 2: The repeated run

My test gate passed, and then my push started the same gate again. One attempt on November 25 took 117 minutes and 46 seconds and still failed, and the setting behind it came from my own fix for the previous blocker.

Oct 26: full gate about 70 min Nov 9: an empty .git folder pushes blocked 4+ h, folder removed back on Nov 11, maker unknown Nov 12: 289 commits waiting several blockers at once Nov 22: scratch moves to disk out from under any stray .git but its export lands in a subshell Nov 25: one commit fails 4 times just under 4 h of gate runs next push tests twice: 117 m 46 s, then fails Nov 26: export in the parent the hook reuses the pass each push still about 70 min
One month of pushes, October 26 to November 26: each fix moved the wait somewhere new.

This continues My slow test gate hid its own failures. On October 26 the full gate, the test run every change must pass before it leaves the machine, took about 70 minutes, with a quick nearest-tests pass in front of it (a short run of only the tests closest to the change). Since then the gate has stood between every commit and the remote copy, and it kept finding new ways to stall.

An empty folder stopped the tests

On November 9 an empty folder named .git, sitting at the top of the system's temporary directory, blocked every push for more than four hours. Some tests refuse to run inside a git repository, and that stray folder made every scratch folder in the temporary directory look as though it sat inside one. It had been there since October 30. I still do not know which process created it.

Removing it cleared the failures. But removing a folder does nothing about whatever made it, and by November 11 it was back. On November 12 I counted 289 local commits waiting, with no successful push since November 9. Other blockers were in that pile too, so the folder does not get all the blame, but it was a lot of work sitting on the wrong side of the remote 🙃

The fix that moved scratch to disk

On November 22 the gate's scratch space moved out of the temporary directory, which is memory-backed on this machine, to a private folder on disk. That also took the gate's test folders out from under any stray .git there. The move came with a small helper, renamed here, that picks the folder and exports its path so every test process sees the same root:

set_scratch_root() {
  SCRATCH_ROOT=${SCRATCH_ROOT:-/example/scratch}
  mkdir -p "$SCRATCH_ROOT" || return 1
  export SCRATCH_ROOT
}

The helper was fine. Where I called it was the problem, and it took four days to show. Meanwhile one group of tests failed on November 22 because it expected local data the clean test copy does not have (62 commits waited for that repair), and on November 25 the tests that shared one scratch directory across parallel workers had to be separated, because a new test process could delete files another test was still using.

Four retries tested the same commit

Those parallel-worker repairs were ready on the morning of November 25, but they were not in the commit the gate was testing. So the gate failed the same commit four times in a row, each attempt starting from scratch. These are the recorded times from the attempt logs:

November 25 attempt (UTC start) Recorded time Result
12:33 3 m 34 s Failed in gate
12:36 77 m 35 s Failed in gate
13:54 76 m 24 s Failed in gate
15:11 81 m 00 s Failed in gate

That is just under four hours on one commit that could not pass. A retry is cheap to start and expensive to finish, and a retry of the same commit can only fail the same way while the fix sits somewhere else.

The 16:52 attempt carried the repairs, and its service tests passed. Then the push itself began the whole gate again from its first stage. That second run failed partway, and the attempt ended at 117 minutes and 46 seconds 😩

A function in parentheses is a subshell

The pre-push hook is a script Git runs just before it sends commits to the remote. My push script runs the gate first and saves the pass, so when Git calls the hook a moment later it can reuse that result instead of testing again. The saved pass is found by a cache key, a fingerprint built from the commits plus the environment the tests ran with. The environment is in the key on purpose: the same code can behave differently with different settings, so a pass under one setup should not count for another.

My gate function was written with parentheses instead of braces. In Bash, name() ( ... ) runs the whole body in a subshell, a child copy of the shell whose variable changes vanish when it exits. On November 22 I had put the set_scratch_root call inside that body. The gate saw SCRATCH_ROOT and saved its pass under that environment, the export disappeared with the subshell, and the hook, started later by Git from the parent, built its key without it. The keys differed, the hook found nothing to reuse, and it ran everything again.

A short reproduction, run with no profile or settings files:

paren() ( export SCRATCH_ROOT=example )
brace() { export SCRATCH_ROOT=example; }
unset SCRATCH_ROOT; paren; echo "after paren(): ${SCRATCH_ROOT-unset}"
unset SCRATCH_ROOT; brace; echo "after brace(): ${SCRATCH_ROOT-unset}"
after paren(): unset
after brace(): example

The two functions differ by one pair of brackets. Bash accepts any compound command as a function body, and ( ... ) is the one that runs its commands in a subshell, so the parentheses quietly put every call in a child shell. I had chosen the parentheses on purpose, so the gate's cleanup traps (handlers that tidy up its files if it is interrupted) could not replace the push worker's own, and then forgot what else they keep inside.

before push worker SCRATCH_ROOT unset gate, inside ( ) exports SCRATCH_ROOT saves pass: key A pre-push hook SCRATCH_ROOT unset looks up key B key B is not key A: the tests run again after push worker exports SCRATCH_ROOT first gate, inside ( ) inherits SCRATCH_ROOT saves pass: key A pre-push hook inherits SCRATCH_ROOT finds key A: reuse same environment, same key: one gate run
Before: the export lived and died in the gate's subshell, so the hook looked under a different key. After: the parent sets it first and both see the same environment.

The fix, landed on November 26, moves the set_scratch_root call out of the subshell into the function that starts the gate, so the gate and the later hook inherit the same value. A new test checks that a changed environment still forces a fresh run, so the key did not get looser to make the reuse work.

The hook now reuses the pass

Every push today printed this line just before sending, trimmed:

pre-push: these exact commits passed the full gate 64s ago; reusing that result

The 64 seconds is the age of the saved pass, not the length of the push. Here are today's three pushes beside yesterday's attempt that paid for the gate twice:

Attempt (UTC start) Recorded time Outcome
Nov 25, 16:52 117 m 46 s Failed in the repeated gate
Nov 26, 03:40 68 m 06 s Pushed, pass reused
Nov 26, 05:19 69 m 14 s Pushed, pass reused
Nov 26, 10:01 71 m 48 s Pushed, pass reused

These are different commits, so the table does not give the fix credit for every minute. It does show each push running the gate once, and it shows what is left: about 70 minutes per push is still a long time to wait 🐢 Nobody has found what makes that stray folder either, though the gate no longer works under it. Part three is for the gate's own length, once a change to it is measured.

A function body in parentheses is a subshell, so set shared settings before the run splits and key a saved result on what every later step will see.