Repository navigation
fix(lib-lock): the timing recorder's failure notice could abort the save it measured - #315
Merged
Merged
Conversation
… 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.shholdssave.lock. That shipped in #244 (9f4fa11) and is more complete than the issue's own proposal: it lives inscripts/lib-lock.shrather thansave-session.sh, sostaging.lockis measured by the same code, and it wrapslock_acquire/lock_releaserather than hooking their return sites, so a later edit to the acquisition logic cannot silently drop it.REMEMBER_LOCK_TIMING=1writes one TSV row per lock use (ts_ms lock event outcome wait_ms held_ms precision pid);scripts/lock-timing-report.shaggregates 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_disclosechose its output channel withcommand -v log. That asks "does anything namedlogexist on this system", not "did the caller sourcelog.sh" — and macOS, the platformlib-lock.shexists for, ships/usr/bin/logon every machine. The branch would then run:Under
set -e(save-session.sh:56) that aborts the caller mid-save withsave.lockheld, leaving the EXIT trap cleaning up after a save that never finished.Latent only because every current sourcer takes
log.shfirst (save-session.sh:63,run-consolidation.sh:41,session-start-hook.sh:79), so a shell function shadows the binary incommand -v. One new caller away from firing — andlib-lock.sh:40documents 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 thelogcall itself, because this function is the last thing that can take a save down andset -edoes not care what the callee is supposed to do.Tests
RED, by stripping the guards off the two
printf >>writes:SAVE_COMPLETEDnever 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_COMPLETEDafter 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:448documents it), so the remaining question is not the constant — it is structural: the summarize model call is still made undersave.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.