Repository navigation
fix(mcp-gateway): cap the initialize-deadline reap memory and key it by a digest - #39878
Conversation
UX Review (Opus 5.5) — ⏭️ skippedRevision |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing to check. What this change shipsInventory (4 items) — 4 justifiedIntent: keep the long-lived gateway's memory of initialize-deadline reaps from growing with caller-chosen pool keys. FIX.
[FIRST-PRINCIPLES-REVIEWED] a50fdf3 Description read[DESCRIPTION-READ] 0aa1612e724149086b02dd2d95c46880809199777e36179ea3d3a824517f6c5e That sha256 names the PR description this verdict read. Recompute it with |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing blocks. The change is a small, reversible in-memory cap with LRU-by-reap eviction, and it takes nothing away. Notes
[DESIGN-REVIEWED] a50fdf3 Description read[DESCRIPTION-READ] 918523760333078ca6653da9fd7ccb429d5c33e564e9f4bbee424ac3eeb6046c 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 |
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: |
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: |
906cba2 to
a50fdf3
Compare
KiroCrewCaptain
left a comment
There was a problem hiding this comment.
Approved. On this head the review checked these things. Both gateway callers hand the same PoolKey to track_initialize and initialize_timeout_for. PoolKey is a frozen dataclass, so its repr digest is stable and equal keys share one slot. The pop and reinsert, the eviction and the ready clear run with no await between them. The 256 cap and the 64-character digest also bound per-entry size, which matters because a register frame can be up to 64 MiB. Mutations of the digest, the eviction bound and the ready arm each turn a test red. PR Readiness is green.
Done well: one pop and reinsert makes dict order equal reap order, so the oldest-first eviction is a two-line loop with no new structure. The test drives the real seams with 4 KB keys.
Optional:
- Pin the move-to-newest step. Reap
keys[0]a second time before the overflow, then assert it stays stretched whilekeys[1]is forgotten. Today a.get()in place of.pop()at admission.py:643 keeps every test green. - Look up with a freshly built equal key (for example, a new
PoolKey.from_register(...)). That pins value equality of the slot, not object identity. - Consider keying by the existing
PoolKey.stable_hash()instead of a second digest helper. Also consider one log line when an entry is evicted past the cap.
Note: release/0.13.0's pick #39598 still carries the unbounded dict, so it needs its own pick of this bound.
Problem / Motivation
Goal: The gateway's memory of initialize-deadline reaps stays bounded, whatever pool keys callers send.
#39546 added
Admission._deadline_reaps, a dict keyed by the raw pool key. A key that is reaped and never becomes ready is never removed. The pool key comes from the stub's register frame, so a caller chooses both how many keys there are and how large each one is. A review lane on the 0.13 pick (#39598) flagged this as unbounded growth.Why it matters
The gateway is a long-lived daemon. A leak that only grows, keyed by caller input, slowly costs memory for the life of the process.
Not a goal
What changed (motivation → approach → change)
Admission._deadline_reapsnow stores a fixed-size SHA-256 digest of the pool key instead of the key itself. It holds at mostINIT_DEADLINE_REAP_KEYS_MAX(256) keys. A new reap moves its key to the newest place; past the cap the key reaped longest ago is dropped first. A dropped key just gets the configured deadline again on its next spawn, as if it had never been reaped.Backwards compatibility
Compatible: under 256 reaped keys nothing changes. Past that, the oldest key falls back to today's first-spawn deadline.
Removes nothing: no setting, field or output goes away.
Tests
test/test_mcp_gateway_spawn_gate.py::TestAcquireBackendOrder::test_reaped_keys_that_never_come_back_ready_are_cappedreaps 261 keys of over 4 KB each that never become ready. The dict ends at 256 entries, each a 64-character digest; the 5 oldest keys are back at the configured deadline and the newer ones are still stretched.Proof: not provable -- other: prove.py reverts the admission.py hunks together, which removes the new constant and errors the test before any assertion; turning only the eviction loop into
while False:turns the test red onassert len(admission._deadline_reaps) == cap.Determinism: the new test passed 20 of 20 runs alone with
-n0, and the whole file passes with-n4. All 84test/test_mcp_gateway*.pyfiles pass (2461 passed).test/test_flake_pattern_ratchet.py, flake8, isort, mypy and the repo semgrep rules pass.Manual verification
N/A -- the change is an in-memory bound inside
Admission; the unit test drives the realtrack_initializeandinitialize_timeout_for.Real-machine verification
N/A -- no user-visible behavior changes below 256 reaped keys, and reaching the cap on a real machine needs 256 distinct slow servers; the unit test covers the bound directly.
Related Issues
Part of #30915
Sibling sweep
Swept:
git grep -n "_deadline_reaps" -- src/Sibling: src/kiro_crew/mcp_gateway/admission.py:609 -- changed here
Pattern harvest
Not generalizable: other per-key gateway state (the pool's backends, spawn locks) is already bounded by the pool's capacity and reaped with the backend; this dict was the one that outlived its backend.
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...); PR Hygiene does not count commits