Skip to content

fix(lib-lock): the timing recorder's failure notice could abort the save it measured - #315

Merged
fdaviddpt merged 1 commit into
mainfrom
fix/226-instrument
Aug 5, 2026
Merged

fdaviddpt merged 1 commit into
mainfrom
fix/226-instrument

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Part of #226 — not Closes. See "What this does not do" below.

What #226 asked for already exists

The issue asks for instrumentation to measure how long save-session.sh holds save.lock. That shipped in #244 (9f4fa11) and is more complete than the issue's own proposal: it lives in scripts/lib-lock.sh rather than save-session.sh, so staging.lock is measured by the same code, and it wraps lock_acquire/lock_release rather than hooking their return sites, so a later edit to the acquisition logic cannot silently drop it.

REMEMBER_LOCK_TIMING=1 writes one TSV row per lock use (ts_ms lock event outcome wait_ms held_ms precision pid); scripts/lock-timing-report.sh aggregates per-lock p50/p90/p99/max plus a timeouts count. Nothing needed building, and building a second mechanism would have been wrong.

What was actually missing

Every existing test asserted that the recorder records. None asserted what happens when it cannot. Writing that test found a live latent defect.

_lock_timing_disclose chose its output channel with command -v log. That asks "does anything named log exist on this system", not "did the caller source log.sh" — and macOS, the platform lib-lock.sh exists for, ships /usr/bin/log on every machine. The branch would then run:

/usr/bin/log lock-timing "..."     # exit 64, unknown subcommand

Under set -e (save-session.sh:56) that aborts the caller mid-save with save.lock held, leaving the EXIT trap cleaning up after a save that never finished.

Latent only because every current sourcer takes log.sh first (save-session.sh:63, run-consolidation.sh:41, session-start-hook.sh:79), so a shell function shadows the binary in command -v. One new caller away from firing — and lib-lock.sh:40 documents itself as sourceable standalone.

The sting: the one path it could take down is the recorder's only way of saying it recorded nothing. An instrument whose "I could not measure" notice is the failure is worse than one that stays quiet.

Fixed with declare -F log — a bash builtin, true only for a shell function, correct on the floor bash 3.2.57 — plus a stderr fallback on the log call itself, because this function is the last thing that can take a save down and set -e does not care what the callee is supposed to do.

Tests

RED, by stripping the guards off the two printf >> writes:

AssertionError: a lock use whose timing could not be recorded exited 1
  — the instrument took the save down with it:
  lib-lock.sh: line 519: .../readonly/lock-timing.tsv: Permission denied

SAVE_COMPLETED never printed, lock still held — the production symptom exactly.

GREEN: 17 passed. Full suite on a quiet machine: 1476 passed, 43 skipped.

The two new tests cover genuinely different code paths — a read-only directory fails at file creation, a read-only file passes the cap check and fails only at the append. Both skip as root, since root writes through the mode bits and would be green while exercising nothing. The harness echoes SAVE_COMPLETED after the last release rather than trusting exit status, because an abort inside a hold can still exit 0 through a trap.

What this does not do

The 30s default is untouched, and the issue stays open. The hold has already been measured (issue comments 2026-07-31 / 2026-08-01; README.md:448 documents it), so the remaining question is not the constant — it is structural: the summarize model call is still made under save.lock. Fixing that means applying the NDC pattern to the parent (snapshot → call unlocked → re-acquire → re-validate → commit), because the lock also serialises the summarizers themselves (#168, #204). That is a design change to the save path, not something to slip in under a tuning issue.

… measured

The instrumentation #226 asked for already exists (#244): lib-lock.sh records
one TSV row per lock use under REMEMBER_LOCK_TIMING=1, and
scripts/lock-timing-report.sh aggregates it. Nothing needed building. What was
missing was the guarantee that it cannot harm the path it observes.

_lock_timing_disclose chose its output channel with `command -v log`. That asks
whether anything named `log` exists, not whether the caller sourced log.sh —
and macOS, the platform lib-lock.sh exists for, ships /usr/bin/log on every
machine. The branch would run `/usr/bin/log lock-timing "..."`, which exits 64,
and under `set -e` (save-session.sh:56) that aborts the caller mid-save with
save.lock held. Latent only because every current sourcer takes log.sh first,
so a shell function shadowed the binary; one new caller away from firing, on
the one path whose job is to say the measurement is absent.

`declare -F log` asks the question the comment already claimed to ask. Verified
on the floor bash (3.2.57): `command -v` sees the binary, `declare -F` does not.

Two tests pin the three properties that make the instrument safe — the lock use
completes, no duration is invented, the gap is disclosed once per process —
across both failure paths (uncreatable file, unappendable file). Teeth shown by
mutation: with the write guards stripped, the read-only-directory test exits 1
with SAVE_COMPLETED never printed and the lock still held.

No default changed. REMEMBER_NDC_COMMIT_LOCK_TIMEOUT is still 30s; that number
is the output of the measurement this enables, not part of it.

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt merged commit 0a09504 into main Aug 5, 2026
12 checks passed
@fdaviddpt
fdaviddpt deleted the fix/226-instrument branch August 5, 2026 21:17
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