Two users on X (v0.1.1549 to v0.1.1557, Windows, Codex routing) see /api/hello answer while /v1/models, Providers and agent requests hang. `magpie goroutines` (b18a6a080, #3362, since v0.1.1404) can capture the stuck process, but its route sat behind lanGuard's allow list (settings.json's file lock) and holdCatalog (provider.Hold, filememo), so a request stuck holding any of them would hang the dump too. dumpFirst serves GET /v1/magpie/goroutines, for callers goroutinesRefused lets in, ahead of lanGuard in Front and ahead of counted/holdCatalog in Handler. It reads only the request, the environment and the runtime. Every refused or other request goes on as before; a caller it lets in is one lanGuard and corsGuard already let in (loopback, unproxied, no browser marks, loopback Host). docs/reference.md now says how to run it from an app install, where magpie isn't on PATH unless Settings > Command line adds it: macOS /Applications/magpie.app/Contents/MacOS/magpie goroutines; Windows PowerShell & (Get-ItemPropertyValue 'HKCU:\Software\Microsoft\Windows\CurrentVersion\App Paths\magpie.exe' '(default)') goroutines | Out-Host (| Out-Host so PowerShell waits for the GUI-subsystem exe), or a portable copy's own path. Run, at this tree onc0985122b: - go test -tags nogui -count=1 -timeout 45m ./internal/cli ./internal/gateway: ok cli 36.2s, ok gateway 388.7s - TestGoroutineDumpAnswersWhileRequestsWait -count=3: ok - gofmt -l: clean; GOOS=linux and GOOS=windows go vet -tags nogui ./internal/...: exit 0 Broken by hand: - Front back to lanGuard(s.Handler()): the new test is red at 20.02s ("the dump didn't answer while settings.json's lock was held"). - Both refusal checks (dumpFirst and goroutineDump) removed: TestGoroutineDumpOnlyToThisComputer red, 25 failure lines. Real use, Windows box: v0.1.1558 magpie-windows-amd64.exe (SHA256 matches SHA256SUMS) serving a sandbox home on :3447; the App Paths one-liner above saved the dump and printed "goroutines 41", exit 0; `& exe goroutines` with and without | Out-Host both saved it. Not tried: a really stuck app; this build on Windows or macOS (the real-use run above was v0.1.1558, without dumpFirst); an interactive PowerShell console without | Out-Host; the Handler-only path has no red test, since provider.held can't be held from the gateway package.
105 lines
4.0 KiB
Go
105 lines
4.0 KiB
Go
package gateway
|
|
|
|
// GET /v1/magpie/goroutines: every goroutine of the process serving the
|
|
// gateway, with its stack and how long it has waited, for a report of
|
|
// magpie stuck at something while it goes on running. An app that kept
|
|
// the partner list it had for three days could only be read by killing
|
|
// it, which ends what was stuck (#3362); `magpie goroutines` asks the
|
|
// running process instead and writes the file the user sends.
|
|
//
|
|
// The dump is runtime/pprof's "goroutine" profile at debug=2, the form a
|
|
// crash prints: each goroutine's state, its wait ("[select, 4320
|
|
// minutes]"), its frames with source lines, and who started it. A frame's
|
|
// arguments are words in hex, never what they point at, so it holds no
|
|
// keys, prompts or messages; it names the source files magpie was built
|
|
// from and the program's path.
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"runtime/pprof"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// GoroutinesPath is where the gateway answers a goroutine dump.
|
|
const GoroutinesPath = "/v1/magpie/goroutines"
|
|
|
|
// GoroutineStatsHeader carries GoroutineStats, as JSON, beside the dump.
|
|
const GoroutineStatsHeader = "X-Magpie-Goroutine-Stats"
|
|
|
|
// GoroutineStats says which process the dump is of.
|
|
type GoroutineStats struct {
|
|
PID int `json:"pid"`
|
|
Program string `json:"program"`
|
|
Version string `json:"version"`
|
|
Window bool `json:"window"` // the app's window or magpie web serves the gateway
|
|
Uptime string `json:"uptime"`
|
|
Goroutines int `json:"goroutines"`
|
|
}
|
|
|
|
// goroutinesRefused says why r may not have the goroutine dump, "" when it
|
|
// may: the same callers as the heap profile (heapRefused).
|
|
func goroutinesRefused(r *http.Request) string {
|
|
return profileRefused(r, "goroutine dump", "magpie goroutines")
|
|
}
|
|
|
|
// dumpFirst answers the goroutine dump ahead of next, for every caller
|
|
// goroutinesRefused lets in; everything else, the dump's refusals too, goes
|
|
// to next as before. The dump is asked for when magpie hangs while it runs,
|
|
// and what the gateway's requests pass through first takes locks that a
|
|
// stuck request can hold: lanGuard's allow list reads settings.json under
|
|
// settings' file lock, holdCatalog takes provider.Hold's and filememo's,
|
|
// and each stats files under them. goroutinesRefused and goroutineDump
|
|
// read only the request, the environment and the runtime, so the dump
|
|
// answers while any of those is stuck, and names who holds it. A caller it
|
|
// lets in is one lanGuard and corsGuard let in too: loopback, which the
|
|
// allow list always passes, unproxied, no Origin or other browser mark, a
|
|
// loopback Host.
|
|
func (s *Server) dumpFirst(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == http.MethodGet && r.URL.Path == GoroutinesPath && goroutinesRefused(r) == "" {
|
|
s.goroutineDump(w, r)
|
|
return
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
// goroutineDump answers GoroutinesPath: the dump as text, with
|
|
// GoroutineStats in GoroutineStatsHeader.
|
|
func (s *Server) goroutineDump(w http.ResponseWriter, r *http.Request) {
|
|
if why := goroutinesRefused(r); why != "" {
|
|
log.Printf("refused %s %s from %s: %s", r.Method, r.URL.Path, r.RemoteAddr, why)
|
|
writeError(w, provider.Chat, http.StatusForbidden, why)
|
|
return
|
|
}
|
|
goroutines := runtime.NumGoroutine()
|
|
var buf bytes.Buffer
|
|
if err := pprof.Lookup("goroutine").WriteTo(&buf, 2); err != nil {
|
|
writeError(w, provider.Chat, http.StatusInternalServerError, "the goroutine dump couldn't be written: "+err.Error())
|
|
return
|
|
}
|
|
exe, _ := os.Executable()
|
|
stats, _ := json.Marshal(GoroutineStats{
|
|
PID: os.Getpid(),
|
|
Program: filepath.Base(exe),
|
|
Version: Version,
|
|
Window: Window,
|
|
Uptime: time.Since(processStarted).Round(time.Second).String(),
|
|
Goroutines: goroutines,
|
|
})
|
|
h := w.Header()
|
|
h.Set("Content-Type", "text/plain; charset=utf-8")
|
|
h.Set("Content-Disposition", `attachment; filename="magpie-goroutines.txt"`)
|
|
h.Set("Cache-Control", "no-store")
|
|
h.Set(GoroutineStatsHeader, string(stats))
|
|
w.Write(buf.Bytes())
|
|
}
|