Skip to content

Feat: Serve pprof from the agentop viewer - #1608

Merged
huang195 merged 1 commit into
rossoctl:mainfrom
huang195:feat/agentop-pprof
Oct 30, 2026
Merged

huang195 merged 1 commit into
rossoctl:mainfrom
huang195:feat/agentop-pprof

Conversation

@huang195

Copy link
Copy Markdown
Member

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 -QUIT dumps 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:

AGENTOP_PPROF_ADDR=127.0.0.1:47603 agentop observe
go tool pprof http://127.0.0.1:47603/debug/pprof/heap

Design

Same shape as #1603, adapted to a binary with no config file for it:

  • Off unless AGENTOP_PPROF_ADDR is set. The profiles expose the process's internals, and the viewer's heap holds the session content it is displaying.
  • The Feat: Serve pprof on the stats server when loopback-only #1603 browser defenses, copied: every handler 403s a non-loopback Host (DNS rebinding) and any request carrying Origin (a no-cors GET needs no preflight). Copied rather than shared — different binary, same predicate as core/observe's unexported twin.
  • Never a nil Handler: net/http/pprof's init registers on http.DefaultServeMux; the server sets its own guarded mux explicitly.
  • Bind failure is stderr + continue, never fatal — the viewer's job doesn't depend on being profiled.
  • Started and announced before tea.NewProgram takes the alt screen, where stderr stops reaching the user.

Verified

  • Unit tests pin both guards (403 rebound Host, 403 with Origin, 200 loopback) and that a passing request returns a real gzipped heap profile.
  • Live check: built the binary, ran the TUI under a pty with the env var — listener up on 127.0.0.1:47603 while the viewer ran, go tool pprof read the heap, both browser defenses answered 403.
  • Full agentop module suite passes (with env -u SSL_CERT_FILE, GOWORK=off), vet and gofmt clean.

Assisted-By: Claude (Anthropic AI) [email protected]

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]>
@huang195
huang195 requested a review from a team as a code owner October 30, 2026 14:28
@coderabbitai

coderabbitai Bot commented Oct 30, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1d5d3f27-f54d-47c1-b33c-331243c70df4
Commits

Reviewing files that changed from the base of the PR and between 951283c and add185d.

Files selected for processing: 4
  • cmd/agentop/README.md
  • cmd/agentop/main.go
  • cmd/agentop/pprof.go
  • cmd/agentop/pprof_test.go
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@clawgenti clawgenti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Comment thread cmd/agentop/pprof.go
// 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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cmd/agentop/pprof.go
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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cmd/agentop/pprof.go
// 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()) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@huang195
huang195 merged commit f5da182 into rossoctl:main Oct 30, 2026
41 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Oct 30, 2026
@huang195
huang195 deleted the feat/agentop-pprof branch October 30, 2026 14:47
@huang195

Copy link
Copy Markdown
Member Author

All three review comments landed in #1611 (the merge at 14:47 cut them off before any could): the loopback-bind refusal — checked on ln.Addr(), so :47603 and 0.0.0.0:47603 now refuse through the stderr-and-continue path — the :0 announce printing the bound address, and the startPprof tests. Thanks for the catch on the bind: the Host/Origin guards really do stop browsers only.

evaline-ju pushed a commit to evaline-ju/cortex that referenced this pull request Oct 30, 2026
…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]>
evaline-ju pushed a commit to evaline-ju/cortex that referenced this pull request Oct 30, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants