Repository navigation
fix(apps): serialize installed.json writers with a per-app record lock - #22796
Conversation
UX Review (Opus 5.5) — ⏭️ skippedRevision |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing blocks. The fix works at the cause: no lock spanned a writer's read and its replace. It covers all 18 What this change shipsInventory (7 items) — 7 justifiedIntent: stop two concurrent writers of one app's
[FIRST-PRINCIPLES-REVIEWED] 8bcb205 Description read[DESCRIPTION-READ] 4ac7534742b7ff80e917fbe02fe00beae3d2dff8a93cf921767b66aa93dc75c9 That sha256 names the PR description this verdict read. Recompute it with |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS No design-level concerns: one per-app sidecar lock across each writer's read and replace removes the lost-update cause, and the lock order is consistent. Notes
[DESIGN-REVIEWED] 8bcb205 Description read[DESCRIPTION-READ] 980a9810171b2e1d3f9c0e7393d996a50ab578098ed1b9294bfa18ad30eb1e11 That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
GPT 6.1 Review — ✅ no blocking findingsGPT 6.1 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
Opus 5.5 Review — ✅ no blocking findingsReviewed Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
2e5337d to
049fcdc
Compare
049fcdc to
ec05e0c
Compare
|
Review-ready at ec05e0c (single commit on main cd4d536).
|
ec05e0c to
8bcb205
Compare
buluoray
left a comment
There was a problem hiding this comment.
Reviewed at 8bcb205. The review below was done at ec05e0c; this head is a rebase onto b27a601 and the PR's own diff is identical line for line.
Approve. The lost update is real on main: set_dev_mode re-read the record only under the dev sentinel lock, which update_app never takes, so a dev-mode save could land after a widening update and drop its consent-pending mark. The per-app record lock closes that.
Checked against the platform-compat rules: the lock goes through platform_compat.open_create_or_existing and platform_compat.file_lock (apps/manager_records.py:268-270), on an empty sidecar outside both installed.json and the app tree, and nothing is written through the lock descriptor, so the Windows range lock holds. Holds are released through an ExitStack on return and on error, reentrancy is thread-local so _write_installed never opens a second descriptor, and both refusal paths fail closed. Lock order is consistent: record lock then sentinel lock in set_dev_mode, no sentinel holder writes a record, the async lifecycle lock is always outside the record lock, and the multi-lock holders walk a fixed order. Readers stay lock-free, which is safe because every write is an atomic replace. Enable, disable and set_app_provenance now run off the loop; the on-loop register_external_app refuses at once under contention and the generic except plus the finally restore the checkout.
Ran the record-lock, enable/disable route, dev-mode, manager and registry-install tests (293 passed). Mutations: a no-op hold, a dropped hold in set_dev_mode or update_app, no reentrancy, the enable route back on the loop, and the lock inside the app tree each turn a test red.
Follow-ups, not blocking:
- Test gap: the race test also passes when hold_app_record_lock does nothing, because the dev thread pauses inside atomic_write, which is already under the lock in _write_installed. Pausing it between its re-read and its write would show the lock spans the whole transaction. Removing the lock inside _write_installed is also not caught by any test.
- backfill_default_on_builtins is reached from run_first_run_setup, which _init_services calls on the event loop, so its acquire is a single attempt. On contention it is skipped for that boot with a warning. Running it off the loop would let it wait.
- The comment above the on-loop register_external_app in registry_pipeline/install.py says a contended lock takes the refusal path below; it actually raises and is handled by the generic except.
- Lock sidecars are never removed on uninstall. They are empty and harmless, so this is only housekeeping.
Problem / Motivation
Goal: Two writers of one app's
installed.jsoncan no longer overlap, so a stale save cannot undo another writer's change, such as asessionApprovalconsent-pending mark.Every record writer reads the record, changes it and replaces the whole file. Nothing serialized them: a dev-mode toggle (which takes no
app_lifecycle_lock) or a provenance save could read the record, a wideningupdate_appcould then write it disabled and pending, and the first writer's older copy would land on top asenabled: true, sessionApprovalConsentPending: false.app_can_manage_session_approvalsthen allows session control with no consent moment. The race was already on main; #22067 narrowed it but could not close it.Why it matters
The
sessionApprovalgrant lets an app approve tool calls and send turns in the user's own sessions. A lost pending mark grants that with no consent. The same lost-update shape can drop any other field a concurrent writer set.Not a goal
uninstall_app, which removes the record rather than writing it.app.jsonor the approved-grants file on their own.What changed (motivation → approach → change)
Symptom: a stale record save lands after a widening update. Cause: writers read-check-replace with no exclusion. Change: one per-app, cross-process lock held across each writer's read, checks and replace.
manager_records.py:app_record_lock(name)takesplatform_compat.file_lock(POSIXflock, Windowsmsvcrt.locking, bounded, fail-closed) on a sidecar,<data home>/app-record-locks/<name>.lock. Notinstalled.json, whichos.replaceswaps to a new inode, and not inside the app tree, whichupdate_appmoves aside. It is reentrant within one thread, so a transaction that calls_write_installeddoes not deadlock on a second descriptor.@holds_app_record_locksplushold_app_record_lock(name)let a writer take it right before its first read and keep it until the function returns; outside such a function the call refuses._write_installedtakes it too, around the save rule and the replace.install_app,update_app(before its old-manifest read, across the tree swap and rollback),register_external_app(across its manifest-plus-record transaction and rollback),enable_app,disable_app,mark_session_approval_consent_pending,set_dev_mode(dev_mode.py),set_app_source,set_app_provenance(manager_lookup.py),backfill_default_on_builtinsandregister_builtin_apps(manager_builtins.py).manager.pylists the new names in the facade.handle_enable_app(enable and its rollback disable) andhandle_disable_appinroutes_enable.py, andinstall_from_registry's twoset_app_provenancecalls inregistry_pipeline/install.py. Itsregister_external_appcall stays on the loop: a worker would keep writing after the install task is cancelled while thefinallyrestores the old checkout; on the loop a contended lock refuses before the record is read, and the route already holds the app's lifecycle lock. Gateway first-run setup (backfill_default_on_builtins) stays on the loop too, before readiness; a contended acquire there skips the backfill until the next start.docs/system-specs/modules/app-kit-platform.mddescribes the lock.Backwards compatibility
Compatible: no record field, route or CLI changes. A writer that cannot take the lock within
file_lock's ceiling now raises instead of writing unserialized; on the event-loop thread the acquire is single-shot, asfile_lockis everywhere. Each critical section is a few file reads and one replace, exceptupdate_app, whose lock spans its tree copy.Removes nothing: every write still happens, now one at a time per app.
Tests
test/test_app_record_lock.py:test_a_dev_mode_save_cannot_undo_a_widening_update: a dev-mode save pauses at its record replace; a wideningupdate_appstarts and must wait on the record lock; after release the record ends disabled and pending and the grant is refused. Fails without the lock (the update never waits).test_another_process_cannot_take_a_held_lock: a child process cannot take a held lock and can take a free one, on POSIX and Windows.test_the_lock_is_reentrant_in_one_thread,test_holding_outside_a_locked_writer_refuses,test_the_lock_lives_outside_the_app_tree.test_apps_routes_coverage_enable_disable.py::TestRecordWritersRunOffTheLoop: the enable and disable routes call their writers off the event loop.test_every_record_writer_takes_the_lock_before_it_reads: every function inkiro_crew/appsthat calls_write_installedis decorated and callshold_app_record_lockbefore its first record read or write.Determinism: the file passed 10/10 repeated runs and in reversed order; the interleave waits on events through
kiro_crew.testing.wait.wait_until, not on time.Manual verification
N/A — the cross-process behaviour is covered by the child-process test; there is no UI change.
Related Issues
Closes #22118
Pattern harvest
Rule candidate: review-prompt
Pattern: a read-modify-write of a shared state file with no lock spanning the read and the replace; lock a sidecar, never the replaced file.
Checklist