Repository navigation
Feat: Serve pprof from the agentop viewer - #1608
Conversation
The proxy got pprof in rossoctl#1603; the viewer did not, and it has needed it — agentop has been the larger of the two processes more than once (the rossoctl#1542 stream leak lived here), and nothing on the box could decompose its heap. The viewer has no HTTP server to hang the endpoints on, so AGENTOP_PPROF_ADDR starts one for the viewer's lifetime: AGENTOP_PPROF_ADDR=127.0.0.1:47603 agentop observe go tool pprof http://127.0.0.1:47603/debug/pprof/heap Off unless set: the profiles expose the process's internals, and the viewer's heap holds the session content it is displaying. The handlers carry the same browser defenses as rossoctl#1603 and the session API's erase guards — a non-loopback Host or any Origin is a 403 — copied rather than shared because this is a different binary. A bind failure is reported on stderr and not fatal. The listener starts and is announced before tea takes the alt screen, where stderr stops reaching the user. Verified live: the listener serves while the TUI runs, go tool pprof reads the heap, and both browser guards answer 403. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 32 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
clawgenti
left a comment
There was a problem hiding this comment.
Summary
This is a tidy port of the #1603 pprof surface to the agentop viewer. It is off unless AGENTOP_PPROF_ADDR is set, it uses an explicit guarded mux (no nil Handler, so DefaultServeMux never gets served), and it starts before tea.NewProgram takes the alt screen so the announce line actually reaches the user. The guard and loopbackHost match core/observe/statserver.go and core/sessionapi/clear.go on main exactly. I ran the new tests locally (Go 1.26, GOWORK=off) and they pass.
One suggestion, which doesn't block: unlike #1603, which only turns pprof on when listener.bind_loopback_only holds, nothing here checks that the bind address is loopback. The Host/Origin guards stop browsers, but they do nothing against a client that isn't a browser. Details inline, plus two nits.
Author: huang195 (MEMBER — maintainer)
Areas reviewed: Go, Docs, Tests, Security
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: passing (all required checks green; CodeRabbit and Spellcheck skipped)
| // listener; process exit closes it too, so a caller that forgets the shutdown | ||
| // loses nothing but a port. | ||
| func startPprof(addr string) (shutdown func(), err error) { | ||
| ln, err := net.Listen("tcp", addr) |
There was a problem hiding this comment.
suggestion: The bind address is never checked to be loopback. #1603 only enables pprof when the stats port is loopback-only (WithPprof(cfg.Listener.BindLoopbackOnly)), but here AGENTOP_PPROF_ADDR=:47603 (the usual Go spelling) or 0.0.0.0:47603 will listen on every interface. The Host and Origin guards only stop browsers. Anyone on the LAN can run curl -H 'Host: localhost' http://<box>:47603/debug/pprof/heap and get back a heap that, as the doc comment says, holds the session content being displayed. They could also start CPU or trace profiling.
The README says to use a loopback address, but nothing in the code enforces it. A cheap way to enforce it is to refuse anything that didn't land on loopback after binding:
ln, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
if tcp, ok := ln.Addr().(*net.TCPAddr); !ok || !tcp.IP.IsLoopback() {
ln.Close()
return nil, fmt.Errorf("%s is not a loopback address; pprof is served on loopback only", addr)
}That rejects :port and 0.0.0.0:port, still accepts localhost:port, and goes through the existing "stderr + continue" path.
| fmt.Fprintf(os.Stderr, "agentop: not serving pprof on %s: %v\n", addr, err) | ||
| return nil | ||
| } | ||
| fmt.Fprintf(os.Stderr, "agentop: serving pprof on http://%s/debug/pprof/\n", addr) |
There was a problem hiding this comment.
nit: This announces the address as configured, not as bound. With AGENTOP_PPROF_ADDR=127.0.0.1:0 the user sees :0 and never learns the port. If startPprof also returned ln.Addr().String(), the message would always print an address you can paste.
| // the viewer's job does not depend on being profiled. The caller owns the | ||
| // returned shutdown and defers it — deferring here would stop the listener the | ||
| // moment this helper returns. | ||
| func maybeServePprof() (shutdown func()) { |
There was a problem hiding this comment.
nit: The tests cover pprofMux, but not startPprof/maybeServePprof. Those hold the property this PR calls out most: the server uses the guarded mux and not DefaultServeMux. A small test that binds 127.0.0.1:0, sends a GET for /debug/pprof/heap with a non-loopback Host, and expects a 403 would pin that, and it would cover the loopback refusal above if you add it.
|
All three review comments landed in #1611 (the merge at 14:47 cut them off before any could): the loopback-bind refusal — checked on |
…elpers The loopback-Host predicate existed as three unexported copies (the session API's erase guards, the stats server's pprof endpoints, the agentop viewer) and the guarded pprof mounting as two. core/listener/ httpx now owns both: LoopbackHost and RegisterPprof, with the guard table and heap-profile shape pinned once in httpx's own tests. agentop keeps one wiring test; observe's and sessionapi's existing tests now exercise the shared code through their own servers. Also merges main into the branch: the PR branched before rossoctl#1603 landed, so the observe half of this dedupe had no code to change without it. Fixes review: PR rossoctl#1608 review finding 1 — a security predicate copied per binary drifts when one copy is fixed and the others are not. Files: cmd/agentop/pprof.go, cmd/agentop/pprof_test.go, core/listener/httpx/pprof.go, core/listener/httpx/pprof_test.go, core/listener/httpx/render.go, core/observe/statserver.go, core/sessionapi/clear.go, core/sessionapi/delete_test.go Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
…address The Host and Origin guards stop browsers, not a curl that sets Host: localhost, so AGENTOP_PPROF_ADDR=:47603 or 0.0.0.0:47603 would hand the heap — the session content the viewer is displaying — to anyone on the network. startPprof now refuses a bind that did not land on loopback, checked on the bound address rather than the string, and returns the address it landed on so a :0 port is announced as its real port. rossoctl#1603 gates its pprof the same way, on the stats port's bind_loopback_only. Fixes review: clawgenti's approve-with-comments on rossoctl#1608 — the unguarded bind address, the :0 announce, and the missing startPprof test, which the returned address makes possible over a real socket. Files: cmd/agentop/pprof.go, cmd/agentop/pprof_test.go, cmd/agentop/README.md Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
What
The proxy got pprof in #1603; the viewer did not — and the viewer has needed it. agentop has been the larger of the two processes more than once (the #1542 stream leak lived there, at ~18 MB/min to 1.5GB), and until now nothing on the box could decompose its heap.
kill -QUITdumps goroutines but kills the TUI.agentop has no HTTP server to hang the endpoints on, so this adds one for the viewer's lifetime, behind an env var:
Design
Same shape as #1603, adapted to a binary with no config file for it:
AGENTOP_PPROF_ADDRis set. The profiles expose the process's internals, and the viewer's heap holds the session content it is displaying.Host(DNS rebinding) and any request carryingOrigin(a no-cors GET needs no preflight). Copied rather than shared — different binary, same predicate ascore/observe's unexported twin.Handler:net/http/pprof's init registers onhttp.DefaultServeMux; the server sets its own guarded mux explicitly.tea.NewProgramtakes the alt screen, where stderr stops reaching the user.Verified
127.0.0.1:47603while the viewer ran,go tool pprofread the heap, both browser defenses answered 403.env -u SSL_CERT_FILE,GOWORK=off), vet and gofmt clean.Assisted-By: Claude (Anthropic AI) [email protected]