Skip to content

fix(macos): quit the running tray in the PKG preinstall - #2024

Merged
Dumbris merged 3 commits into
mainfrom
claude/issue-2016-investigation-8369d4
Nov 24, 2026
Merged

Dumbris merged 3 commits into
mainfrom
claude/issue-2016-investigation-8369d4

Conversation

@Dumbris

@Dumbris Dumbris commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Description

Closes #2016

After brew upgrade --cask mcpproxy the menu bar showed two MCPProxy icons. A manual PKG upgrade had the related problem from #957: the old version kept running.

Cause. The package's postinstall only ran open -a. The quit-first step written for #957 lived in packaging/macos/postinstall.sh, which no package ever shipped (create-pkg.sh copies scripts/postinstall.sh). That script's process match was also wrong for the installed app: it looked for MCPProxy.app/Contents/MacOS/MCPProxy, while the payload installs mcpproxy.app and pgrep -f is case-sensitive.

Fix. The package now ships scripts/preinstall.sh, which quits the running tray before its bundle is replaced:

  • SIGTERM, wait up to 8s, then SIGKILL. The tray routes SIGTERM through its normal quit path, which stops the managed core.
  • Scoped to the console user (-U <uid>), so other sessions on a shared Mac are left alone.
  • Always exits 0; it never fails an install.
  • The match is case-insensitive and anchored on .app/Contents/MacOS/mcpproxy, so it covers mcpproxy.app, MCPProxy.app and the legacy Go tray, and does not match the core at Contents/Resources/bin/mcpproxy.

postinstall is unchanged apart from a comment: it still sets up the CLI and launches the new tray.

Decisions a reviewer may question

  • Match anchored at the command start. pgrep -f also searches arguments, so the pattern is anchored to the start of the command line. A bundle under a directory with a space in its name is therefore not matched.
  • Preinstall, not postinstall. The old tray stops its core while its own bundle is still on disk, the same order the Sparkle update path uses. The cost: if the install fails after this step, the old app is intact but not running.
  • No AppleScript quit. The [Bug]: old version App still after upgrade #957 script tried it first. Sent from the installer's root context it depends on an Automation consent the user never gave and can stall the install, and SIGTERM already reaches the same quit path.
  • The unshipped script is deleted, not kept in sync, and the three comments that pointed to it now name scripts/postinstall.sh.

Not in this PR

  • The Homebrew cask (smart-mcp-proxy/homebrew-mcpproxy) should also get quit: "com.smartmcpproxy.mcpproxy" in its uninstall stanza, so the tray quits before Homebrew's pkgutil: step deletes the bundle. This PR fixes the upgrade without it.
  • A single-instance guard in the tray (suggested in the issue) is left out: a "second instance exits" rule would keep the old version after an upgrade.

Testing

  • I have tested these changes locally
  • I have added/updated tests that prove my fix is effective or my feature works
  • All existing tests pass

scripts/preinstall.test.sh starts real processes under fake bundle paths in a temp dir and runs the script's quit step against them (matches are confined to the temp dir, so a real tray on the machine is untouched). It covers: nothing running, the shipped lowercase layout, the other spellings, a tray that ignores SIGTERM, and three things that must survive: the core, a longer executable name, and a process that only names the tray path as an argument. It also checks that create-pkg.sh ships the script. 15/15 pass on macOS (bash 3.2) and in an Ubuntu 24.04 container; with the old case-sensitive match restored 4 checks fail, and with the anchor removed 1 fails. It runs as the new macOS PKG Preinstall job in unit-tests.yml.

Checked locally with real packages on macOS (arm64, installed tray 0.75.0):

  • Built a component pkg with pkgbuild from the installed, notarized 0.75.0 bundle and the scripts exactly as create-pkg.sh copies them. After pkgutil --expand, PackageInfo registers both preinstall and postinstall, and both are byte-identical to the sources.
  • Built a second pkg carrying the real preinstall and a dummy payload and installed it with installer -target CurrentUserHomeDirectory, so PackageKit itself ran the script against the live 0.75.0 tray. /var/log/install.log:
    ./preinstall: preinstall: asking the running MCPProxy to quit (SIGTERM)
    ./preinstall: preinstall: the old instance quit
    
    The tray was gone 1 second later and the installer exited successfully. open -a then started exactly one tray.

Not tested: the full signed product pkg installed as root, and brew upgrade (both need the release signing identity and sudo). In the user-domain run the script ran as the user, not root. One observation for the RC check: the old core took about 30 seconds to finish its own shutdown after the tray quit, so the new tray can start while it is still exiting; that case is left to the existing stale-core supersede (Spec 092). The Go and Swift changes are comments only; I ran go build ./internal/launch/ and no other suites.

After `brew upgrade --cask mcpproxy` the menu bar showed two MCPProxy
icons, and a manual PKG upgrade left the old version running (#957).
The package's postinstall only ran `open -a`; the quit-first step added
for #957 lived in packaging/macos/postinstall.sh, which no package
shipped, and matched a process path the installed app never has
(`MCPProxy.app/...` while the payload installs `mcpproxy.app`).

The package now ships scripts/preinstall.sh, which quits the tray before
its bundle is replaced: SIGTERM (the tray routes it through its normal
quit path and stops the core), then SIGKILL after 8s, scoped to the
console user, never failing the install. The match is case-insensitive
and anchored on `.app/Contents/MacOS/mcpproxy`, so it covers every
bundle spelling and leaves the core to the tray.

The unshipped duplicate script is removed. scripts/preinstall.test.sh
runs the quit step against real processes started from fake bundles and
checks that create-pkg.sh ships the script; it runs in unit-tests.yml.

Closes #2016
@cloudflare-workers-and-pages

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

Copy link
Copy Markdown

Review of the preinstall quit step found two real problems.

`pgrep -f` also searches arguments, so the unanchored pattern would have
signalled any process that merely named the tray's path (for example
`less /Applications/mcpproxy.app/Contents/MacOS/MCPProxy`). The pattern
is now anchored at the start of the command line. A bundle under a
directory with a space in its name is no longer matched; the package
installs to /Applications.

The self-test deleted its temp dir in an EXIT trap without checking that
mktemp had succeeded; with an unusable TMPDIR that resolved to the
caller's working directory. The test now stops before the trap exists.

Fixtures are sleep(1) started under the fixture's name (`exec -a`), so
their command lines start with the bundle path like a real tray's, and a
bystander process that only names the tray path must survive.
The self-test passed its temp dir to pgrep/pkill unescaped. Those take
regular expressions, so the "." in a mktemp name could match a process
under a different directory, and the cleanup trap could have killed it.
The path is now escaped and the cleanup match is anchored.

The bystander fixture also left a `sleep 600` child behind after each
run; it is now a single cat(1) blocked on a FIFO.
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

📦 Build Artifacts

Workflow Run: View Run
Branch: claude/issue-2016-investigation-8369d4

Available Artifacts

  • archive-darwin-amd64 (33 MB)
  • archive-darwin-arm64 (30 MB)
  • archive-linux-amd64 (21 MB)
  • archive-linux-arm64 (19 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-goC3M0UD.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 45333148155 --repo smart-mcp-proxy/mcpproxy-go

Note: Artifacts expire in 14 days.

@Dumbris
Dumbris merged commit f2f9f0e into main Nov 24, 2026
58 of 59 checks passed
@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

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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.

[Bug]: brew upgrade leaves two MCPProxy instances in the menu bar (pkg postinstall never quits the old tray)

2 participants