Skip to content

fix: deterministic MCP authorization repeated-initial-control status - #2085

Merged
jbeda merged 1 commit into
mainfrom
fix/2082-mcp-authorization-control-race
Oct 5, 2026
Merged

jbeda merged 1 commit into
mainfrom
fix/2082-mcp-authorization-control-race

Conversation

@jbeda

@jbeda jbeda commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Summary

  • TestMCPAuthorizationGRPCRejectsRepeatedInitialControl flaked (OK, want InvalidArgument) because the result depended on two races in relayMCPAuthorizationControl:
    1. Both inputs ready: the receive goroutine has already put the InvalidArgument error on controlDone when the continuation closes events. select picks a ready case at random. If the close wins, the loop exits and the already-received error is dropped, so the RPC returns OK.
    2. Frame not yet read: the continuation finishes before the goroutine reads the second frame. The server can't report a frame it hasn't read without blocking on the stream. Blocking would be wrong, because a valid client may keep its send side open until the server ends the RPC.
  • Server (grpc.go): after the relay loop, if the result is otherwise OK, one non-blocking check of controlDone lets an already-reported non-EOF control error become the RPC status. This makes race 1 deterministic. It never waits for unread frames and never cancels the continuation, so the H-K5/H-K23 rule (control-stream errors don't cancel a healthy continuation) is unchanged. CancelMcpAuthorization shares the relay and gets the same behavior.
  • Test: the test now holds the resumed protected call until the fake stream has delivered the repeated frame. It now pins the intended ordering, independent of scheduling: a violation received while the continuation is live is reported.

The only observable difference is the final status code. Run completion, cancellation, and the delivered events are identical in every ordering.

Development stage

  • Plan / Interface — Bounded/Architectural behavioral and exact-interface contract; no implementation
  • Implementation — based on an approved, merged Plan / Interface PR
  • Combined — compact one-task Bounded/Architectural exception; no separate plan PR
  • Spike / Routine / Cleanup — acceptance-plan spine exempt; Spike evidence does not ship as-is

Contract linkage

  • Work classification: Routine
  • Classification rationale: a localized flake fix to an existing protocol rule (a repeated initial frame is InvalidArgument). It makes an already-specified status deterministic. No new interface, persistence, or trust-boundary change.
  • Decision record: None — no design decision; existing H-K5/H-K23 continuation-lifecycle rule preserved
  • Human waiver of spine: No
  • Acceptance plan: N/A (Routine)
  • Human decisions resolved and recorded: N/A (Routine)
  • Plan / Interface PR: N/A
  • Approved commit baseline: N/A
  • Combined/exemption rationale: Routine bug fix

Interface conformance

  • No interface change.

Issue relationship

Fixes #2082

Type of change

  • Behavioral/interface plan
  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation/process
  • Other (describe):

Test plan

Baseline checks

  • Acceptance-plan checker — N/A (Routine)
  • Linting (task lint)
  • Fast offline test suite (task test) — covered by task test:race
  • Pre-review race suite (task test:race)
  • Offline demo (go run ./cmd/mecademo): tool call, permission ask/approval, result
  • Markdown changed: docs generation/link checks (task docs) — N/A
  • User docs/user-facing behavior changed: site build (task site:build) — N/A
  • Guarded engine API affected: compatibility check (task api:check) — N/A
  • Intentional engine API change: task api:update + engine/CHANGELOG.md — N/A
  • Landed plan: strict acceptance trace (task ac-trace-strict) — N/A
  • Final implementation review: /panel-review — not run

Focused verification:

  • go test -race ./internal/adapter/server -run '^TestMCPAuthorizationGRPCRejectsRepeatedInitialControl$' -count=10000: passes (reproduced 1/300 failures before).
  • go test -race ./internal/adapter/server -run MCPAuthorization -count=20: passes, covering the adjacent transport-fault and continuation-lifecycle tests.
  • go test -race ./internal/adapter/server: passes.

Changes

File Change
internal/adapter/server/grpc.go After the relay loop, an already-reported control error beats an otherwise-OK result
internal/adapter/server/mcp_authorization_transport_test.go requestsDrained hook on the fake stream; repeated-initial test holds the continuation until the frame is delivered

User-facing change

None. A misbehaving client that repeats the initial frame now always receives InvalidArgument, not sometimes OK.

Special notes for reviewers

  • The server change has no dedicated test. The updated test also passes without the grpc.go change (0/10000 failures), because holding the continuation leaves the error ready long before events closes. Proving race 1 deterministically would require both channels ready at the same instant. relayMCPAuthorizationControl takes a concrete *agent.Run, so that needs a test seam I chose not to add. The server change is kept because without it the status in race 1 is a coin flip. It can be dropped if you'd rather ship only the test fix.
  • Race 2 is intentionally not covered by the server. A frame received after the continuation has finished is not validated. This is the protocol ordering the test now pins.

The authorization control relay selected between an already-received
control error and the continuation's closing event channel at random, so
a repeated initial frame could end the RPC OK. After the relay loop, a
non-EOF control error already reported on controlDone now wins over an
otherwise-OK result; unread frames are still not awaited and control
errors still never cancel the continuation.

The test now holds the resumed call until the repeated frame is
delivered, pinning that a violation received while the continuation is
live is reported, independent of goroutine scheduling.

Co-authored-by: Mecatl <[email protected]>
@jbeda jbeda added bug Something isn't working flake Intermittent test or CI failure labels Oct 5, 2026
@jbeda
jbeda merged commit 3eee33c into main Oct 5, 2026
48 checks passed
@jbeda
jbeda deleted the fix/2082-mcp-authorization-control-race branch October 5, 2026 02:52
@toolhive-release-app toolhive-release-app Bot mentioned this pull request Oct 7, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working flake Intermittent test or CI failure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky MCP authorization repeated-initial-control test returns OK

1 participant