Repository navigation
fix(apps): keep the registry config write off the event loop - #18103
Conversation
The registry PUT handler read and atomically rewrote config.json directly on the event loop, holding only the advisory flock that update_config_locked takes. That left two defects beside the loop stall: a concurrent config writer serialized on the loop-side _get_config_lock could commit from a stale snapshot and silently revert the registry write, and the per-host trust audit emitted by the async caller after the write could be skipped when the request was cancelled mid-write, persisting a trust grant with no record. Route the read-modify-write through run_config_write, the one entry point that holds both config-writer locks and hands the blocking work to a worker, and move the per-host registries.host_trust_granted audit into the worker so it is bound to the committed write and a cancellation cannot persist a grant without auditing it. Co-authored-by: Leon <276034861+leonlaiyc@users.noreply.github.com>
UX Review (Opus 5.5) — ⏭️ skippedRevision |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Check that the existing SEL tests cover [DESIGN-REVIEWED] 45b8752 Description read[DESCRIPTION-READ] e016459361cc5ff68632c6888178e6b65aa81e430c094af44135628ae339bc14 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 |
First Principles Review (Opus 5.5) — 🟡 CONCERNSPremise-level review of First-Principles-Verdict: CONCERNS The new Not justified as shipped
What this change shipsInventory (6 items) — 3 justifiedIntent: saving the registry list in Settings never stalls the gateway, never loses a concurrent config edit, and never skips the trust audit — FIX (#18102).
[FIRST-PRINCIPLES-REVIEWED] 45b8752 Description read[DESCRIPTION-READ] 5dabd741545a7991704d69ac4e7ba6bd81bd830c3958baa32f1dd713e1a86b33 That sha256 names the PR description this verdict read. Recompute it with |
Opus 5.5 Review — ✅ no blocking findingsReviewed Review detailsNo findings that block the merge. FINDING — src/kiro_crew/apps/routes.py:4808 — [OPUS-REVIEWED] 45b8752 Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
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: |
|
self-added: yes
|
|
self-added: yes
|
|
self-added: yes
|
|
self-added: yes
|
|
Self-review (crew-pr-reviewer, read-only): HOLD for a maintainer / security owner This fixes the registry PUT doing its |
There was a problem hiding this comment.
Approve at 45b87529e7fcd211f5bb9750b958e9824b21c066.
Verified at head:
src/kiro_crew/apps/routes.py:4810routes the registry save throughrun_config_write, so theconfig.jsonwrite no longer runs on the event loop.routes.py:4901-4941: the write is anupdate_config_lockedread-modify-write, so a concurrent config writer is no longer overwritten;newly_trustedis diffed against theregistriesread under the lock.- The
host_trust_grantedSEL event is emitted on the worker thread strictly afterupdate_config_lockedreturns. A failed write raises before any grant is announced, and a cancellation after the commit can no longer drop the audit, because the event is bound to the thread that committed.
Fail-closed: a failed write raises before the audit; nothing is announced that did not land. Upgrade impact / audience fit: none; same stored shape, a legacy null is still repaired by PUT.
Mutation (each reverted on its own):
- Worker-side audit loop removed: 4 red, incl.
test_cancelled_registries_write_still_audits_the_trust_grantandTestPutRegistries::test_new_url_host_emits_trust_grant_event. run_config_writeswapped for a bareasyncio.to_thread:test_registries_config_write_holds_the_loop_side_config_lockandtest_registries_config_write_is_not_dispatched_with_a_bare_to_threadred.- Grants audited before the write:
test_failed_registries_write_audits_no_trust_grantred. - Prior-host diff disabled: 3 red, incl.
test_registries_write_audits_each_new_host_exactly_once.
Related suites: 822 passed, 1 skipped.
Non-blocking follow-ups:
ConfigWriteRefused(aValueErrorfrom the publish floor inupdate_config_locked) is not caught here, so it would surface as a bare 500; the theme PUT maps it to400 config_write_refused(dashboard/handlers/core.py:732). Likely unreachable today, but worth matching.docs/system-specs/modules/security.md:2856could say the host diff now happens inside the locked read-modify-write and the event is emitted after the commit, since that ordering is the guarantee this PR adds.
CI: 86 success, 11 skipped, 0 failing. Not run: full suite, Windows/macOS, a real concurrent-write race on a live gateway.
Problem / Motivation
Goal: Saving the app registry list never stalls the gateway, never silently reverts other settings, and always leaves an audit record for every host it trusts.
Saving the registry list in Settings runs the config file read-and-rewrite directly on the event loop, so a slow or contended disk write freezes the whole gateway while it finishes. The same write takes only one of the two config locks, and announces each newly trusted host only after it returns — two bugs that ride on that one on-loop write.
Why it matters
A registry host is a real trust grant: its apps install with gateway privileges and it enters the loosened-sandbox / SSH-clone trust set. Three things can go wrong today. The gateway can stall under a slow write. A config save on another endpoint, running at the same time, can commit from an older snapshot and quietly drop the registry change. And if the save is cancelled part-way — a shutdown, a dropped client — the trust can land on disk while the record that it happened never gets written, leaving an audit gap no one can reconstruct.
Not a goal
_sync_builtin_configwriters in the same file are not folded in: they already run insideapp_lifecycle_lock, so routing them through the same helper needs a lock-ordering proof that is its own change.What changed (motivation → approach → change)
The registry PUT read
config.json, edited it, and wrote it back on the event loop, holding only the advisory file lock. That is why a big write froze the loop, why a concurrent writer on the in-process lock could overwrite the change, and why a cancelled request could persist a trust grant with no audit line.In
src/kiro_crew/apps/routes.py, the read-modify-write now goes throughrun_config_write, the one helper that holds both config locks at once and hands the blocking work to a worker thread, so the loop stays free and no other writer can clobber the result. The per-host trust audit moves into that worker, right after the write commits, so the record is tied to the write itself — a cancellation can no longer land the grant and skip the record. A failed write returns a structured 500 instead of being misreported as a read error.Backwards compatibility
Compatible: the request/response shape is unchanged; the PUT still returns
newlyTrustedHosts, and the sameregistries.update/registries.host_trust_grantedSEL events are emitted, only from the thread that committed the write. A failed write gains an addedcode: registries_config_write_failedfield on its existing 500 body — additive, nothing that works today stops working.Removes nothing: the two unused imports dropped from
apps.routes(atomic_write,read_config_text) have no other reader in that module; a function-localatomic_writeimport elsewhere in the file keeps its own callers working.Tests
test_apps_instances_loop_offload.py: the registry config write runs on a worker thread, not the loop; a cancelled write still audits the trust grant from the worker; the handler no longer emits the per-host event itself; a failed write audits no grant; and the AST ratchet now bans a bareto_threaddispatch of_write_registries_config.test_apps_install_owner_gate.py: the owner-gate side-effect sentinel moves fromatomic_writetoupdate_config_locked, the call the PUT now commits through.Manual verification
N/A — unit coverage exercises the loop-offload, the cancellation audit, and the owner gate directly.
Screenshots / video
N/A — backend-only change; no user-visible frontend surface is touched.
Related Issues
Fixes #18102
Replaces #4550 by @leonlaiyc
Pattern harvest
Rule candidate: review-prompt
Pattern: an async handler doing a config read-modify-write directly on the loop (and holding only one of the two config-writer locks) instead of routing through
run_config_write.