Skip to content

fix(tray): make Quit stop the core promptly on Windows - #1962

Open
felipecrs wants to merge 6 commits into
smart-mcp-proxy:mainfrom
felipecrs:claude/tray-quit-button-responsiveness-180ff6
Open

felipecrs wants to merge 6 commits into
smart-mcp-proxy:mainfrom
felipecrs:claude/tray-quit-button-responsiveness-180ff6

Conversation

@felipecrs

@felipecrs felipecrs commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Problem

On Windows, clicking Quit in the tray flashed a console window and MCPProxy only exited ~30s later.

Three causes:

  1. The graceful stop never reached the core. The tray used taskkill /PID <pid> /T (no /F), which only sends WM_CLOSE. The core runs with CREATE_NO_WINDOW, so it has no window to receive it. The tray waited out its 30s timeout and then force-killed the core, skipping its cleanup.
  2. Console flash. taskkill/tasklist were started from the GUI (-H windowsgui) tray without CREATE_NO_WINDOW, so each one briefly opened a console.
  3. The core's HTTP shutdown waited for streams. Once the core does get the signal, http.Server.Shutdown waits for every active request. The SSE /events feed and MCP streams held open by clients never end on their own, so it always sat out its 30s timeout (HTTP server forced shutdown due to timeout).

Fix

  • Tray (Windows): the core starts in its own process group (CREATE_NEW_PROCESS_GROUP). Stop() attaches to the core's console and sends CTRL_BREAK_EVENT to that group. Go delivers it as SIGINT, so the core runs its normal graceful shutdown. If the event can't be sent, the tray force-kills right away instead of waiting 45s.
  • Tray (Windows): taskkill/tasklist go through a small monitor.HiddenCommand helper that sets CREATE_NO_WINDOW.
  • Core: request contexts come from a BaseContext that is cancelled 2s after Shutdown starts (RegisterOnShutdown). Ordinary in-flight requests get a short grace period, and stream handlers see the cancellation and return.

Testing

  • New TestStopIsGracefulForWindowlessProcess (Windows): Stop() on a windowless child that exits only on SIGINT finishes in about 0.1s with exit code 0. The child reports when its signal handler is installed, so the test doesn't depend on timing. With the old taskkill /T it fails after 46s with "force killed". Note that PR CI runs unit tests on Ubuntu only, so this test first runs in CI after merge (the main push matrix includes Windows).
  • The core change has no automated test. The existing TestGracefulShutdownNoPanic is the natural place for one, but every CI job skips it by name, so I left it alone. Checked manually on Windows with the real core and an open /events stream: stop time went from 30s to 2.4s.
  • Tried end to end with the installed tray: Quit now exits in a couple of seconds with no console flash.
  • go test ./cmd/mcpproxy-tray/... and go test ./internal/server/ (CI skip regex) pass on Windows. go vet is clean for Windows and Linux. golangci-lint was not run locally.

Not changed

signalProcessTree's graceful path, used only when the tray stops a core it did not start, still uses taskkill /T. Sending CTRL_BREAK to a process group the tray didn't create could reach other processes on that console, such as the user's terminal.

Quit from the tray took ~30s on Windows and flashed a console window:

- The tray asked the core to stop with `taskkill /T` (no /F). That only
  sends WM_CLOSE, which the windowless core never receives, so the tray
  waited out its timeout and force-killed it, skipping the core's cleanup.
  The core now starts in its own process group and the tray sends it
  CTRL_BREAK, which Go delivers as SIGINT, so the normal graceful shutdown
  runs.
- taskkill/tasklist were started without CREATE_NO_WINDOW, so each one
  flashed a console from the GUI tray.
- Once the core got the signal, http.Server.Shutdown still waited the full
  30s for the long-lived SSE (/events) and MCP streams, which never end on
  their own. Request contexts are now cancelled 2s after shutdown starts,
  so ordinary requests get a short grace and the streams return.
The Windows stop test slept 1s before sending CTRL_BREAK; a slow runner
could deliver it before the child's handler was installed, killing the
child outright. The helper now prints "ready" after signal.Notify and
the test waits for it.

Also drop the TestGracefulShutdownNoPanic stream change: every CI job
skips that test, so it added no coverage.
felipecrs added a commit to felipecrs/mcpproxy-go that referenced this pull request Nov 20, 2026
felipecrs added a commit to felipecrs/mcpproxy-go that referenced this pull request Nov 21, 2026
@codecov-commenter

codecov-commenter commented Nov 22, 2026 •

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 16.66667% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/server/server.go 16.66% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

@felipecrs
felipecrs force-pushed the claude/tray-quit-button-responsiveness-180ff6 branch from 4699042 to 52f0baa Compare November 23, 2026 01:41
felipecrs added a commit to felipecrs/mcpproxy-go that referenced this pull request Nov 23, 2026
Stop() with no graceful signal possible uses grace 0, so time.After(0) and a
closed doneCh were both ready and select picked at random: an already-exited
child could be taskkill /T /F'd and reported as force killed. Check doneCh
first, and again when the grace timer fires.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@Dumbris

Dumbris commented Nov 24, 2026

Copy link
Copy Markdown
Member

Thanks @felipecrs, this is a careful diagnosis (WM_CLOSE never reaching a windowless console process, console flashes, SSE holding Shutdown open) and the fix is well scoped. We have no Windows machine on the review side, so below is what we checked and what we'd still like confirmed on your end.

Checked: GOOS=windows build, vet and test-compile are clean for the tray and internal/server; macOS/Linux tray paths are untouched (process_windows.go / main_windows.go are Windows-only); go test ./cmd/mcpproxy-tray/... passes. CI is approved and running on the current head.

One thing to consider, one thing we pushed

  1. internal/server/server.go (BaseContext + RegisterOnShutdown 2 s): this applies on every OS, and in-flight tool calls run on the request context, so a tool call still running 2 s after shutdown starts is now cancelled (before, Shutdown waited up to 30 s for it). We reproduced this with a stdlib-only sketch: a 10 s handler saw its context cancelled at 2.0 s. That looks like the right trade-off to us (Docker cleanup in runtime.Close is not on that context and still runs after), but could you mention it in the PR description / a code comment, and consider a slightly longer grace (e.g. 5 s) so short tool calls can finish?
  2. Stop() with grace = 0 (process_windows.go): if the child had already exited, doneCh was closed and time.After(0) was ready, so select picked at random and could run taskkill /T /F on an exited (possibly recycled) PID and return "force killed". We pushed a small fix for this to your branch (c1301dd, normal push): doneCh is checked first, before the timer is armed. Please take a look and say if you'd rather have it another way.

Smoke test we'd appreciate on Windows (you already did most of this): start the tray with a stdio server using Docker isolation + an open /events stream, click Quit, confirm (a) no console flash, (b) core log shows HTTP server shutdown completed gracefully (not forced shutdown due to timeout) and Docker containers cleaned up, (c) docker ps shows no mcpproxy containers, (d) no mcpproxy.exe left in Task Manager and the tray restarts cleanly without a "database locked" error.

Dumbris and others added 2 commits November 24, 2026 14:57
Raise the post-Shutdown request-context grace from 2s to 5s so short tool
calls can finish, name it requestShutdownGrace, and document that tool calls
on the request context are cancelled after it on every OS.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… comment

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@Dumbris

Dumbris commented Nov 24, 2026

Copy link
Copy Markdown
Member

Pushed three small maintainer commits on top of your branch (your commits and authorship untouched):

  • c1301dde7 Stop(): if the child has already exited, never run taskkill /T /F (with grace == 0 the closed doneCh and the zero timer were both ready, so select picked at random). Windows test added; the repo's gofmt hook also normalised whitespace in process_windows.go.
  • 50e784506 the post-shutdown request-context grace is now 5 s (requestShutdownGrace) instead of 2 s, with a comment that in-flight tool calls on the request context are cancelled after it, on every OS.
  • 911ac67e0 tightened that new test and corrected a comment.

Windows build, vet and test-compile are clean and the tray tests pass. CI for the new head needs approval first.

@Dumbris
Dumbris enabled auto-merge (squash) November 24, 2026 13:06
felipecrs added a commit to felipecrs/mcpproxy-go that referenced this pull request Nov 24, 2026

This branch has not been deployed

No deployments
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.

3 participants