Repository navigation
test(mcp-gateway): keep the home pin while the port-chain env is swapped - #40263
Conversation
…me pin _resolve_in and the instance-isolation test ran the port chain under patch.dict(os.environ), which empties the whole process environment before refilling it on exit (and on entry with clear=True). Any other thread that called config_dir() in that gap found no KIROCREW_HOME and memoized the operator's real default home, so the teardown floor failed test_a_stub_that_registers_first_waits_then_gets_a_bound_backend. The environment is now set and restored one key at a time through MonkeyPatch.context(), and KIROCREW_HOME is never removed.
UX Review (Opus 5.5) — ⏭️ skippedRevision |
First Principles Review (Opus 5.5) — ⏭️ skippedRevision |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Test-only change. Setting and restoring keys one at a time removes the moment when the environment is empty, which is when another thread can memoize the real data home. Notes
[DESIGN-REVIEWED] b3f4211 Description read[DESCRIPTION-READ] b2d8010825b7f9a7766e64fbfea02fde65b08a518dd0706157ac0a0f664937bf 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: |
|
Checked whether keeping
CI on head |
Code change approver recommendation: A person should decideWhy: the decider estimates a 5% chance that this change introduces a defect, within the 12% limit for approving on its own, but it is held at tier pending: being reviewed. Test-only change. In this file, Tier: pending: being reviewed. An automated recommendation, not a review: a maintainer still decides. It approves on its own only when the estimated chance of a defect is at most 12% and the change has at most 400 changed lines. Decider v3c (217fe607) · commit |
KiroCrewCaptain
left a comment
There was a problem hiding this comment.
This review checked head b3f4211 and found no blocking issues. The diagnosis is right. patch.dict always clears os.environ and then refills it on exit, even without clear=True. That opened a window with no KIROCREW_HOME, and another thread could resolve the real home in it. The new _environ helper replaces both patch.dict(os.environ, ...) sites in the file. It sets and restores one key at a time and never removes the home pin. All nine _resolve_in assertions expect the env or bound source, and resolve_client_port_src returns at those steps before it reads any home. So keeping the pin cannot change a verdict. The change adds no sleeps, ports or clocks, and the removed patch import has no other use.
Done well: the PR fixes the root cause the way the testing conventions prescribe. It also converts the sibling site that only had the gap on exit, and leaves the teardown floor and its retries unchanged.
Optional:
- Add a small pin test. Enter
_environ({}, exclusive=True), assertKIROCREW_HOMEis still set, and record that it is never removed on entry or exit. Today nothing turns red if the exclusion at line 241 is dropped. exclusive=Truestill removes the other floor pins (KIROCREW_WORKSPACE,KIROCREW_POD_ROOT,KIROCREW_POD_ENV_DIR). It did that before this PR too. Either keep them as well, or change the docstring and harvest line to say only the home pin is kept.
Problem / Motivation
Goal:
test_a_stub_that_registers_first_waits_then_gets_a_bound_backendstops erroring at teardown with "resolved the operator's REAL default data home".The test passes, then the rootdir teardown floor fails it on Linux Backend Tests shard 4:
Logs read (attempt 1 of each, plus the
linux-pytest-progress-4artifact for per-worker order):TestAcquisitionBeforeTheAnnounce::test_a_third_party_server_never_waitsTestAcquisitionBeforeTheAnnounce::test_it_waits_with_keepalives_and_then_proceedsThe preceding tests differ and neither starts a thread, so neither is the offender.
Mechanism, traced on main
cbffcd8fcb:paths._resolved_homeis written only by_resolve_default_home()(src/kiro_crew/config/paths.py:118-129). It is reached fromconfig_dir()(paths.py:778-808) when_valid_override_home()(paths.py:323-342) finds noKIROCREW_HOME. One more caller,shared_kiro_agents_writable()(paths.py:775), is not reachable from this test._resolved_hometoNoneat setup (test/conftest_floor_home.py:514). The evidence is read inpytest_runtest_teardownbefore any fixture unwinds (conftest.py:1109-1111). So the real home was memoized during this test, while every pin was still set.to_thread(daemon/launch.py:1007-1008) and reads the process env, which holds thehomefixture'sKIROCREW_HOME. The resolver copiesKIROCREW_HOMEinto the spawn env, because_scrub_sensitive_envonly drops credential keys (mcp_gateway/manager.py:218) and the control-plane strip only dropsLD_/DYLD_/PYTHON(env.py:1306-1310).resolve_client_port_srcreturns at theboundstep (port_resolution.py:339-343) before it reads any home.KIROCREW_HOMEin the process is_resolve_in(test line 231):patch.dict(os.environ, env, clear=True). Withclear=Trueit emptiesos.environbefore it refills it. On exit_patch_dict._unpatch_dictALWAYS clears and refills, even withoutclear=True. Any other thread that callsconfig_dir()in either gap finds no override and memoizes the real home. The floor then blames the test that is running.I could not tell from the logs WHICH thread resolved: the floor records no stack. The fix does not depend on that. With the gap gone, no thread can see an unpinned home while this test runs. This is the same class #39297 fixed for
test_install_receipt.py(process-wideKIROCREW_HOMEremoval plus a live thread).Why it matters
Each hit fails a shard on an unrelated PR and costs a rerun. Two hits are counted for 11-19 and 11-20.
Not a goal
patch.dict(os.environ, ...)sites in other test files.What changed (motivation → approach → change)
Symptom: a real-home resolution memoized during the test. Root cause: the test empties the process environment for a moment, through
patch.dict(os.environ), while other threads are live. Change: a small_environcontext manager in the test module sets and restores keys one at a time throughpytest.MonkeyPatch.context(), and never removesKIROCREW_HOME._resolve_inuses it withexclusive=True, so the port chain still sees only the given env, minus that one pin. The instance-isolation test at the end of the file had the same exit-time gap, and now uses it too. The unusedpatchimport is dropped.Backwards compatibility
Compatible: test-only change. The tests assert the same values.
Removes nothing: only test helpers change; no product path, record or key is touched.
Tests
The existing tests in
test/test_mcp_policy_instance_binding.pycover this. Their assertions are unchanged.b3f4211754: Backend Tests (3.12, 4) passed. Itslinux-pytest-progress-4artifact shows all 116 tests oftest/test_mcp_policy_instance_binding.pypassed setup, call and teardown, includingtest_a_stub_that_registers_first_waits_then_gets_a_bound_backend. So every_resolve_inport verdict is unchanged.Static gates run locally: flake8, isort, black 26.10.1
--check, comment-history and brand gates. All clean. No local pytest: CI is the test runner.Manual verification
N/A -- test-only change; CI runs the file.
Real-machine verification
N/A -- test-only change to an in-process helper; nothing a separate machine would run differently.
Related Issues
Fixes #39647
Sibling sweep
Swept: git grep -n "patch.dict(os.environ" -- test/test_mcp_policy_instance_binding.py
Sibling: test/test_mcp_policy_instance_binding.py:1912 -- changed here
The wider class is out of this PR's goal: 364
patch.dict(os.environsites in 61 other test files. None of them has a hit for this floor in the logs read here.Pattern harvest
Rule candidate: review-prompt
Pattern:
patch.dict(os.environ, ...)clears the whole process env on exit (and on entry with clear=True); in a test with live threads, use monkeypatch per key so the floor pins never disappearChecklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...); PR Hygiene does not count commitsContribution License Agreement
Contributed under the project's Apache-2.0 license.