Skip to content

fix(server): stop the status forwarder and routing listener on Shutdown - #1941

Merged
Dumbris merged 2 commits into
mainfrom
fix/server-shutdown-stops-listeners
Nov 20, 2026
Merged

Dumbris merged 2 commits into
mainfrom
fix/server-shutdown-stops-listeners

Conversation

@Dumbris

@Dumbris Dumbris commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Problem

NewServer starts two goroutines that Server.Shutdown never stopped:

  • forwardRuntimeStatus ranges over Runtime.StatusChannel(). Nothing closes that channel.
  • listenForRoutingModeRefresh ranges over a SubscribeEvents() channel. Shutdown never unsubscribed it.

In a real process this does no harm because the process exits. But every server that is shut down inside a running process (tests, an in-process restart) leaves two idle goroutines behind for the rest of the process.

Fix (internal/server/server.go)

  • Server now stores the routing subscription (routingEventsCh), and Shutdown unsubscribes it. UnsubscribeEvents closes the channel, which ends the listener's range loop. The listener's own deferred UnsubscribeEvents then finds no subscription left and does nothing.
  • The forwarder now selects on a server-owned listenersDone channel, which Shutdown closes. The runtime's statusCh stays open, so a status send can never hit a closed channel.
  • Both steps run inside the existing s.shutdown guard, so a second Shutdown is still a no-op and listenersDone is closed only once.

Tests

  • New TestShutdown_StopsServerListenerGoroutines (internal/server/shutdown_listeners_test.go): builds a server with NewServer, calls Shutdown, and polls (10s deadline) until neither frame is running above the count taken before NewServer. It then calls Shutdown a second time. It failed before the fix (1/1 of each still running after 10s) and passes after it (-race -count=5).
  • Full go test -race -timeout 60m -skip "E2E|Binary|MCPProtocol|TestInfoEndpoint|TestGracefulShutdownNoPanic|TestSocketInfoEndpoint" ./internal/server/ passes on both trees. Goroutines still running at the end of the run (counted by a local-only TestMain that was not committed):
total forwardRuntimeStatus listenForRoutingModeRefresh
main 58a824e 3194 227 227
this branch 2736 0 0
  • golangci-lint v2 with .github/.golangci.yml, bare and --build-tags server, with --new-from-rev=origin/main: 0 issues.

Not in this PR

The TestMain leak guard from #1936 (leak_guard_main_test.go) is not on main yet. Once both PRs land, it can also check these two frames, since a full run now ends at zero.

Also: E2E job red on main since #1936

The -run TestE2E job has failed on main since #1936 (6251956, 201d373): FAIL: 3 upstream.Manager network-recovery watcher(s) still running. I traced the creators with pprof labels. All three are the socket E2E tests (TestE2E_TrayToCore_UnixSocket, TestE2E_DualListener_Concurrent, TestE2E_SocketPermissions), which deliberately never shut their server down. Each test now shuts its server down in a defer that runs before the socketE2EMutex unlock. The CI E2E command (go test -v -race -timeout 10m ./internal/server -run TestE2E -skip '…') now passes locally (255s), and the three tests pass with -race -count=3.

NewServer starts two goroutines that Shutdown never stopped:
forwardRuntimeStatus ranges over Runtime.StatusChannel(), which nothing
closes, and listenForRoutingModeRefresh ranges over an event subscription
Shutdown never unsubscribed. Each shut-down server kept both idle for the
rest of the process (227 of each by the end of a full internal/server run).

Keep the routing subscription on Server and unsubscribe it in Shutdown,
which closes the channel and ends the listener (its deferred unsubscribe
finds nothing left to do). Stop the forwarder with a server-owned done
channel closed in Shutdown, leaving the runtime's status channel open so
no sender can hit a closed channel. The shutdown flag keeps both one-shot.
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

📦 Build Artifacts

Workflow Run: View Run
Branch: fix/server-shutdown-stops-listeners

Available Artifacts

  • archive-darwin-amd64 (33 MB)
  • archive-darwin-arm64 (30 MB)
  • archive-linux-amd64 (20 MB)
  • archive-linux-arm64 (18 MB)
  • archive-windows-amd64 (33 MB)
  • archive-windows-arm64 (29 MB)
  • frontend-dist-pr (0 MB)
  • installer-dmg-darwin-amd64 (29 MB)
  • installer-dmg-darwin-arm64 (26 MB)
  • smart-mcp-proxymcpproxy-goU7DTPV.dockerbuild (0 MB)

How to Download

Option 1: GitHub Web UI (easiest)

  1. Go to the workflow run page linked above
  2. Scroll to the bottom "Artifacts" section
  3. Click on the artifact you want to download

Option 2: GitHub CLI

gh run download 44554678436 --repo smart-mcp-proxy/mcpproxy-go

Note: Artifacts expire in 14 days.

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 87.50000% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/server/server.go 87.50% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

TestE2E_TrayToCore_UnixSocket, TestE2E_DualListener_Concurrent and
TestE2E_SocketPermissions never shut their server down, so each left its
upstream manager's network-recovery watcher running. Since the TestMain
leak guard (#1936) that fails the -run TestE2E job on main. Shut each
server down in a defer that runs before the socketE2EMutex unlock.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Dumbris added a commit that referenced this pull request Nov 19, 2026
… merge cleanly

#1941 makes the same shutdown fix in socket_e2e_test.go. Taking its file
byte-for-byte means whichever PR lands second merges without a conflict.
The CI E2E command still passes with the leak guard.
Dumbris added a commit that referenced this pull request Nov 19, 2026
… merge cleanly

#1941 makes the same shutdown fix in socket_e2e_test.go. Taking its file
byte-for-byte means whichever PR lands second merges without a conflict.
The CI E2E command still passes with the leak guard.
@Dumbris

Dumbris commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

Independent review (opencode github-copilot/gpt-6.1-sol, variant high) of the merge with main 9a59c08, tree 2dd86e793: VERDICT CLEAN. No double close of listenersDone (guarded by s.shutdown under s.mu). UnsubscribeEvents is safe against concurrent publishers (eventMu). TestShutdown_StopsServerListenerGoroutines passed 5x and 5x under -race. Residual: no test for concurrent Shutdown or busy listeners.

@Dumbris
Dumbris merged commit b88e951 into main Nov 20, 2026
54 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.

2 participants