Skip to content

Guard test backend ready channel against double close - #6363

Merged
jhrozek merged 1 commit into
stacklok:mainfrom
jhrozek:fix-flaky-close-closed-channel
Aug 18, 2026
Merged

Guard test backend ready channel against double close#6363
jhrozek merged 1 commit into
stacklok:mainfrom
jhrozek:fix-flaky-close-closed-channel

Conversation

@jhrozek

@jhrozek jhrozek commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Summary

TestCreateMCPClient_ContinuousListeningGatedOnSink shares one listChangedTestBackend across two t.Parallel() subtests, each of which connects its own MCP client. The backend's OnRegisterSession hook did an unguarded close(b.ready) — but that hook fires once per registered session, not once per backend. When both clients registered, the channel was closed twice and the test binary died with panic: close of closed channel.

Because the panic aborts the whole binary, it surfaced as a failure of whatever unrelated test happened to be mid-run — TestInitAndQueryCapabilities_FatalErrors, TestParsingMiddlewareWithRealMCPClients, a pkg/ignore test — which made it look like several different flakes rather than one bug.

The fix guards the close with a sync.Once, so the helper is correct no matter how many sessions register. The alternative (give each subtest its own backend) fixes this call site but leaves the helper fragile for the next test that shares one.

Closes #6362

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Test plan

  • Unit tests pass

Reproduced first, then verified the fix. The panic needs varied GOMAXPROCS; it does not fire at default parallelism.

Before (fires within ~3 iterations):

go test -race -count=40 -cpu=1,2,8 ./pkg/vmcp/session/internal/backend/
...
panic: close of closed channel
	pkg/vmcp/session/internal/backend/mcp_session_test.go:236
	mcpcompat/server.(*Hooks).registerSession
	mcpcompat/server.(*MCPServer).registerAndSync
	go-sdk/mcp.(*ServerSession).initialized

After: 4 × -count=40 -cpu=1,2,8 (480 runs) all green. A separate 720-run check during investigation was also clean.

Plain -count=60 at default -cpu stayed green both before and after, which is why this only showed up on CI runners.

Does this introduce a user-facing change?

No — test-only change.

Generated with Claude Code

The OnRegisterSession hook fires once per registered session, but
TestCreateMCPClient_ContinuousListeningGatedOnSink shares one backend
across two parallel subtests that each connect a client. The second
registration closed an already-closed channel, panicking and taking the
whole test binary down — so the failure surfaced against whichever
unrelated test happened to be running.

Closes stacklok#6362

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@codecov

codecov Bot commented Aug 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.04%. Comparing base (7a0c0a5) to head (a132ede).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6363      +/-   ##
==========================================
+ Coverage   72.97%   73.04%   +0.06%     
==========================================
  Files         742      742              
  Lines       78398    78491      +93     
==========================================
+ Hits        57208    57330     +122     
+ Misses      17201    17153      -48     
- Partials     3989     4008      +19     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jhrozek
jhrozek merged commit 502cec3 into stacklok:main Aug 18, 2026
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky: close of closed channel in vmcp backend session tests

2 participants