athemeroy (WangYeruo on X) runs `magpie serve` as a LaunchAgent with the
magpie-google-ai-studio browser plugin and 11 accounts. Each
`magpie --version`, `version`, `licenses` or `group <id>` run beside it
left a second magpie-bun host with PPID 1, holding the Chrome profiles
the gateway's host wanted ("Chrome exited before starting WebView").
Their analysis was right on each count:
- plugin.Cached starts a background list of the plugins' providers
whenever this process hasn't listed them (provGood is in memory), so
every short command reaching provider.Catalog (startUp's RenameLegacy,
groups, models, ...) started a host.
- Main ended through os.Exit and never stopped it. A host whose stdin
closes leaves only once nothing is pending, and a plugin holding a
browser keeps Bun up meanwhile, so the host outlived magpie, adopted
by init. SIGTERM (launchd stopping serve) orphaned serve's own host
the same way.
- --version and licenses were answered after startUp.
Now:
- Cached asks the plugins in the background only when
plugin.ListInBackground is on; run sets it from servesPages (app,
web, serve, TUI). Other commands read the list on disk.
- Main ends through exit(), which runs plugin.EndHosts first, on
success and error; die does too on SIGINT/SIGTERM/SIGHUP, bounded by
hostsOnSignal (20s, past the 15s a renewing host gets).
- provider test returns exitError instead of os.Exit(1).
- -v/--version/version and licenses answer before any start-up work.
Audit of the os.Exit calls in internal/cli: data_cli.go:135 and the
CheckEnv exit in Main run before anything can start a host;
claudeLetGo already called EndHosts.
Tests (red on the old code, the non-test sources reverted to
14b6b9555, green with the fix):
- TestOneShotCommandsStartNoPluginHost: groups, group fakeco, models,
providers, accounts each started a host (5 reds); version, --version,
-v, licenses and agents start none; Cached in a serving magpie still
lists.
- TestExitEndsPluginHosts: a real host whose stuck usage hook holds it
up (new STUCK_USAGE_HOLD, STUCK_PID in testdata/stuck), the magpie
ended by `version`, by an error and by SIGTERM: all three left the
host at PPID 1 on the old code, none now. 10 of 10 on macOS, 3 of 3
on Linux (WSL), version/error on Windows (no SIGTERM there).
- TestSessionCommandsSkipStartUp: version/--version/-v/licenses don't
run startUp (red on the old code for version). Three tests that used
`version` to reach startUp/makeDirs use `groups`.
Real use, sandbox HOME on mote, a plugin whose provider list takes 90s
holding a timer, signed in with a key, `magpie serve` running:
- old binary: groups, group holdco and models each left a magpie-bun
at PPID 1; after SIGTERM to serve, serve's host was at PPID 1 too
(4 left).
- new binary: only serve's host, and none left after SIGTERM. The
commands take 0.02-0.15s.
Run at the pushed head:
- gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./..., go build: ok (mote).
- go test -count=1 -timeout 45m ./... on mote: 102 packages ok, two
red, each red the same at the base 14b6b9555 (-count=3/5 there):
internal/cli TestHealthcheckFindsTheRunningGatewayWithoutMagpieAddr,
TestUpdateRestartsTheGatewayItFindsWithoutMagpieAddr and
TestUpdateFindsAMagpieThatWroteNoRecord (#6471, #6223: an ssh
session's forward holds 127.0.0.1:3425 on mote), and internal/gateway
TestOpenCodeGoOwnFreeModelGetsNothingOfOpenCodes (#7117).
- Rebased on 598bc4534 (it changes internal/cli's update code), at
the pushed head: go test ./internal/cli ./internal/plugin on mote:
plugin ok; cli ok but for the same three #6471 reds.
Not tried: the reporter's real Google AI Studio plugin with Chrome, and
a real LaunchAgent stop (launchctl); the repro sends serve SIGTERM as
launchd does.
56 lines
1.5 KiB
Go
56 lines
1.5 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// The tests run in a home of their own, never the user's real files, and
|
|
// off the network (testenv.Offline): the plugin tests start plugin hosts,
|
|
// and a host started in a home without a models.dev catalog fetched the
|
|
// real one for up to 30s (#1922).
|
|
//
|
|
// With MAGPIE_CLI_TEST_SERVE set, inside a test's sandbox, the test binary
|
|
// is `magpie serve` of a released version (serve_update_test.go).
|
|
func TestMain(m *testing.M) {
|
|
if os.Getenv("MAGPIE_CLI_TEST_SERVE") != "" {
|
|
if os.Getenv(testenv.Marker) == "" {
|
|
os.Exit(2)
|
|
}
|
|
testenv.Offline()
|
|
version = "0.1.9999"
|
|
if err := serve(); err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
os.Exit(0)
|
|
}
|
|
// With MAGPIE_CLI_TEST_HOST_EXIT set, it starts a plugin host and
|
|
// ends as magpie does (plugin_host_exit_cli_test.go).
|
|
if how := os.Getenv(hostExitEnv); how != "" {
|
|
if os.Getenv(testenv.Marker) == "" {
|
|
os.Exit(2)
|
|
}
|
|
testenv.Offline()
|
|
hostThenExit(how)
|
|
}
|
|
os.Exit(testenv.RunIn(m, func(string) { testenv.Offline() }))
|
|
}
|
|
|
|
// A host start's models.dev fetch is refused at once, never sent (#1922).
|
|
func TestStaysOffTheNetwork(t *testing.T) {
|
|
resp, err := http.Get("https://models.dev/api.json")
|
|
if err == nil {
|
|
resp.Body.Close()
|
|
t.Fatalf("models.dev answered %s: the package's tests reach the network", resp.Status)
|
|
}
|
|
if !strings.Contains(err.Error(), "tests stay off the network") {
|
|
t.Fatalf("models.dev: %v, not testenv's refusal", err)
|
|
}
|
|
}
|