@LeiCheng42 on X reports magpie using the most CPU while Codex runs. As with magpie heap (e085d6e92), Activity Monitor and `sample` show Go's frames as ???, so `magpie cpu [<file>] [--seconds N]` asks the magpie serving the gateway for GET /v1/magpie/cpu?seconds=N: runtime/pprof's CPU profile over 30s (5..120; else 400), gzipped protobuf, with the pid, version, uptime, CPU time used over the window (getrusage / GetProcessTimes) and since start, cores, GOMAXPROCS and goroutines in X-Magpie-CPU-Stats. It tells the user to use magpie as when busy (run Codex) while it records, writes <file> else magpie-cpu-<date>-<time>.pb.gz (0600), refuses a file that is there before recording, and says "is magpie running?" at once when nothing answers. --help writes nothing; an unknown flag, "-", a second file or --seconds out of range are refused before anything is asked. Only the profiling request waits: the gateway serves everything else meanwhile. pprof keeps one CPU profile at a time, so a second ask gets 409 (cpuRecording), and a caller that hangs up stops the recording. The route takes heap's guard: heapRefused's checks move into profileRefused, shared by cpuRefused, so only a loopback, unproxied, browserless call to a loopback Host gets it, with or without a gateway key or lanGuard. Run atbad4508c4+ this: gofmt -l (nothing); go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./... (clean); go test -tags nogui -timeout 45m ./... under a sandbox HOME: every package ok except internal/agent (45m timeout, #2544; again alone, in TestPortMovesEveryAgent) and internal/gateway (TestTranslatedStreamKeepsClientAlive/chat_to_responses, sibling of #2169/#2711, filed as #2737; alone with -timeout 45m only /chat_to_anthropic, #2169; the test passes 5/5 alone). TestCPU* -race -count=3 ok. Broken by hand: cpuRefused returning "" (19 failures in TestCPUProfileOnlyToThisComputer); no one-at-a-time guard and no hang-up stop (TestCPUProfileOneAtATime red each); no seconds bounds, no pre-check of the file, no is-it-running check, no help case (each CLI test red). Rebased on2042123f4: go build ./..., gofmt -l, GOOS=windows and GOOS=linux vet of internal/gateway and internal/cli clean; go test -run 'CPU|Cpu|Heap|Help' ./internal/cli ./internal/gateway ok. Real use: the built binary's `magpie serve` under a sandbox HOME on 127.0.0.1:3493; `magpie cpu --seconds 5` saved a 0600 profile that go tool pprof -top reads and printed the numbers; a concurrent second one got the 409 while GET / answered in ms; Origin: null and a rebound Host got 403; --help wrote nothing; an existing file was refused. Not tried: the GUI app's process, or the Windows box.
216 lines
7.9 KiB
Go
216 lines
7.9 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"compress/gzip"
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// The CPU profile goes to the same callers as the heap profile and no
|
|
// others: a foreign page, Origin null, a page on localhost, a listed page
|
|
// with its key, a rebound Host, another machine with or without a key and
|
|
// a proxy's client are refused with lanGuard and without it, before a
|
|
// second is recorded.
|
|
func TestCPUProfileOnlyToThisComputer(t *testing.T) {
|
|
fresh(t)
|
|
t.Setenv("MAGPIE_ADDR", "")
|
|
t.Setenv("MAGPIE_PUBLIC_URL", "")
|
|
_, secrets := newCaller(t, "Page")
|
|
s := settings.Load()
|
|
s.CORSOrigins = []string{"https://listed.example"}
|
|
if err := settings.Save(s); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
key := secrets[0]
|
|
old := cpuSecond
|
|
cpuSecond = time.Millisecond
|
|
t.Cleanup(func() { cpuSecond = old })
|
|
srv := New()
|
|
chrome := []string{"User-Agent", "Mozilla/5.0 (Macintosh) AppleWebKit/605.1.15 Version/27.2 Safari/605.1.15", "Sec-Fetch-Site", "cross-site", "Sec-Fetch-Mode", "cors"}
|
|
call := func(guarded bool, remote, host string, hdr ...string) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
r := httptest.NewRequest("GET", CPUPath+"?seconds=5", nil)
|
|
r.RemoteAddr = remote
|
|
r.Host = host
|
|
for i := 0; i+1 < len(hdr); i += 2 {
|
|
r.Header.Set(hdr[i], hdr[i+1])
|
|
}
|
|
w := httptest.NewRecorder()
|
|
h := srv.Handler()
|
|
if guarded {
|
|
h = lanGuard(h)
|
|
}
|
|
h.ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
refused := []struct {
|
|
name string
|
|
remote, host string
|
|
hdr []string
|
|
}{
|
|
{"a foreign page", "127.0.0.1:50123", "127.0.0.1:3425", append([]string{"Origin", "https://evil.example"}, chrome...)},
|
|
{"Origin null", "127.0.0.1:50123", "127.0.0.1:3425", append([]string{"Origin", "null"}, chrome...)},
|
|
{"Origin null with a key", "127.0.0.1:50123", "127.0.0.1:3425", append([]string{"Origin", "null", "Authorization", "Bearer " + key}, chrome...)},
|
|
{"a page on localhost", "127.0.0.1:50123", "127.0.0.1:3425", append([]string{"Origin", "http://localhost:5173"}, chrome...)},
|
|
{"a localhost subdomain", "127.0.0.1:50123", "127.0.0.1:3425", append([]string{"Origin", "http://evil.localhost"}, chrome...)},
|
|
{"a listed page with its key", "127.0.0.1:50123", "127.0.0.1:3425", append([]string{"Origin", "https://listed.example", "Authorization", "Bearer " + key}, chrome...)},
|
|
{"a rebound page's GET", "127.0.0.1:50123", "rebind.attacker.example:3425", chrome},
|
|
{"a rebound page with a key", "127.0.0.1:50123", "rebind.attacker.example:3425", append([]string{"Authorization", "Bearer " + key}, chrome...)},
|
|
{"a call to another name", "127.0.0.1:50123", "rebind.attacker.example:3425", []string{"User-Agent", "curl/8"}},
|
|
{"a localhost subdomain as Host", "127.0.0.1:50123", "wails.localhost:3425", []string{"User-Agent", "curl/8"}},
|
|
{"another machine", "192.168.1.20:50123", "192.168.1.10:3425", nil},
|
|
{"another machine with a key", "192.168.1.20:50123", "192.168.1.10:3425", []string{"Authorization", "Bearer " + key}},
|
|
{"a proxy's client", "127.0.0.1:50123", "127.0.0.1:3425", []string{"X-Forwarded-For", "203.0.113.9"}},
|
|
{"a proxy's client with a key", "127.0.0.1:50123", "127.0.0.1:3425", []string{"Cf-Connecting-IP", "203.0.113.9", "Authorization", "Bearer " + key}},
|
|
}
|
|
for _, c := range refused {
|
|
for _, guarded := range []bool{true, false} {
|
|
w := call(guarded, c.remote, c.host, c.hdr...)
|
|
// lanGuard asks another machine without a key for one (401)
|
|
if w.Code != http.StatusForbidden && w.Code != http.StatusUnauthorized || w.Header().Get(CPUStatsHeader) != "" || bytes.HasPrefix(w.Body.Bytes(), []byte{0x1f, 0x8b}) {
|
|
t.Errorf("%s (lanGuard %v): %d %q", c.name, guarded, w.Code, w.Body.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
for _, host := range []string{"127.0.0.1:3425", "localhost:3425", "[::1]:3425"} {
|
|
w := call(true, "127.0.0.1:50123", host, "User-Agent", "Go-http-client/1.1")
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("magpie cpu at %s: %d %q", host, w.Code, w.Body.String())
|
|
}
|
|
zr, err := gzip.NewReader(bytes.NewReader(w.Body.Bytes()))
|
|
if err != nil {
|
|
t.Fatalf("at %s: not gzip: %v", host, err)
|
|
}
|
|
raw, err := io.ReadAll(zr)
|
|
if err != nil || len(raw) == 0 {
|
|
t.Fatalf("at %s: %v, %d bytes", host, err, len(raw))
|
|
}
|
|
// a pprof CPU profile's string table names its sample types
|
|
for _, want := range []string{"samples", "cpu", "nanoseconds"} {
|
|
if !bytes.Contains(raw, []byte(want)) {
|
|
t.Errorf("at %s: the profile names no %s", host, want)
|
|
}
|
|
}
|
|
var st CPUStats
|
|
if err := json.Unmarshal([]byte(w.Header().Get(CPUStatsHeader)), &st); err != nil {
|
|
t.Fatalf("at %s: stats %q: %v", host, w.Header().Get(CPUStatsHeader), err)
|
|
}
|
|
if st.PID != os.Getpid() || st.Seconds <= 0 || st.Goroutines < 1 || st.NumCPU < 1 || st.GOMAXPROCS < 1 || !st.CPUKnown || st.CPUTotal <= 0 || st.CPU < 0 || st.CPUTotal < st.CPU {
|
|
t.Errorf("at %s: stats %+v", host, st)
|
|
}
|
|
if !strings.Contains(w.Header().Get("Cache-Control"), "no-store") {
|
|
t.Errorf("at %s: cacheable: %v", host, w.Header())
|
|
}
|
|
}
|
|
}
|
|
|
|
// seconds out of 5..120, or not a number, is refused before anything is
|
|
// recorded.
|
|
func TestCPUProfileSecondsBounded(t *testing.T) {
|
|
srv := New()
|
|
for _, v := range []string{"4", "121", "0", "-5", "x", "30s", "1e2"} {
|
|
r := httptest.NewRequest("GET", CPUPath+"?seconds="+v, nil)
|
|
r.RemoteAddr = "127.0.0.1:50123"
|
|
r.Host = "127.0.0.1:3425"
|
|
w := httptest.NewRecorder()
|
|
srv.Handler().ServeHTTP(w, r)
|
|
if w.Code != http.StatusBadRequest || w.Header().Get(CPUStatsHeader) != "" {
|
|
t.Errorf("seconds=%s: %d %q", v, w.Code, w.Body.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
// While one CPU profile records, the gateway goes on answering other
|
|
// calls, and a second profile is told one is recording (409), not left to
|
|
// runtime/pprof, which keeps one at a time. A caller that hangs up ends
|
|
// its recording, and the next profile can start.
|
|
func TestCPUProfileOneAtATime(t *testing.T) {
|
|
fresh(t)
|
|
t.Setenv("MAGPIE_ADDR", "")
|
|
srv := New()
|
|
ts := httptest.NewServer(srv.Handler())
|
|
defer ts.Close()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
first := make(chan error, 1)
|
|
go func() {
|
|
// 120 real seconds: it ends when the test hangs up
|
|
req, _ := http.NewRequestWithContext(ctx, "GET", ts.URL+CPUPath+"?seconds=120", nil)
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err == nil {
|
|
res.Body.Close()
|
|
}
|
|
first <- err
|
|
}()
|
|
// wait for the first to hold the recording, not for a duration
|
|
deadline := time.Now().Add(30 * time.Second)
|
|
for !cpuRecording.Load() {
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("the first profile never started")
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
|
|
// the gateway answers meanwhile
|
|
// a minute: far less than the 120 seconds recorded, and room for a busy
|
|
// machine
|
|
c := &http.Client{Timeout: time.Minute}
|
|
res, err := c.Get(ts.URL + "/")
|
|
if err != nil {
|
|
t.Fatalf("the gateway didn't answer while a profile recorded: %v", err)
|
|
}
|
|
res.Body.Close()
|
|
if res.StatusCode != http.StatusOK {
|
|
t.Fatalf("GET / while recording: %s", res.Status)
|
|
}
|
|
|
|
// a second profile is refused, at once
|
|
res, err = c.Get(ts.URL + CPUPath + "?seconds=5")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body, _ := io.ReadAll(res.Body)
|
|
res.Body.Close()
|
|
if res.StatusCode != http.StatusConflict || !strings.Contains(string(body), "recording a CPU profile already") || res.Header.Get(CPUStatsHeader) != "" {
|
|
t.Fatalf("second profile: %s %q", res.Status, body)
|
|
}
|
|
|
|
// hanging up ends the first, and frees the recording
|
|
cancel()
|
|
select {
|
|
case <-first:
|
|
case <-time.After(30 * time.Second):
|
|
t.Fatal("the first call didn't end")
|
|
}
|
|
deadline = time.Now().Add(30 * time.Second)
|
|
for cpuRecording.Load() {
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("a hung-up profile kept recording")
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
old := cpuSecond
|
|
cpuSecond = time.Millisecond
|
|
t.Cleanup(func() { cpuSecond = old })
|
|
res, err = c.Get(ts.URL + CPUPath + "?seconds=5")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
res.Body.Close()
|
|
if res.StatusCode != http.StatusOK {
|
|
t.Fatalf("the next profile: %s", res.Status)
|
|
}
|
|
}
|