Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-6390: expose Go pprof on a loopback port (#7200)

Multica Eveaa6d0dadcf1d
diff --git a/SELF_HOSTING_ADVANCED.md b/SELF_HOSTING_ADVANCED.md
index cd72952b88..57b4289f91 100644
--- a/SELF_HOSTING_ADVANCED.md
+++ b/SELF_HOSTING_ADVANCED.md
@@ -581,6 +581,34 @@ networking, allowlists, NetworkPolicy, or proxy authentication. If you bind
trusted network, for example a host-local mapping such as
`127.0.0.1:9090:9090`.
+## Go Runtime Profiling
+
+The backend exposes all standard Go pprof routes on the fixed loopback-only
+management listener `127.0.0.1:6060`, including CPU, heap, allocs, goroutine,
+block, mutex, threadcreate, symbol, and trace profiles:
+
+```bash
+go tool pprof 'http://127.0.0.1:6060/debug/pprof/profile?seconds=30'
+go tool pprof http://127.0.0.1:6060/debug/pprof/heap
+```
+
+The public API port does not serve `/debug/pprof/`. The listener address is not
+configurable and is never bound to a container or host network interface.
+Profiles can reveal process internals and some captures add CPU or memory
+pressure, so access should remain limited to operators on the same host or in
+the same container network namespace.
+
+A loopback listener inside a container belongs to that container's network
+namespace and is not reachable directly from the host. With the Compose stack,
+capture the profile inside the backend container and copy it out:
+
+```bash
+docker compose -f docker-compose.selfhost.yml exec backend \
+ wget -qO /tmp/heap.pprof http://127.0.0.1:6060/debug/pprof/heap
+docker compose -f docker-compose.selfhost.yml cp backend:/tmp/heap.pprof ./heap.pprof
+go tool pprof ./heap.pprof
+```
+
## Upgrading
```bash
diff --git a/server/cmd/server/main.go b/server/cmd/server/main.go
index 95844fc771..0378cc5c18 100644
--- a/server/cmd/server/main.go
+++ b/server/cmd/server/main.go
@@ -19,6 +19,7 @@ import (
"github.com/multica-ai/multica/server/internal/handler"
"github.com/multica-ai/multica/server/internal/logger"
obsmetrics "github.com/multica-ai/multica/server/internal/metrics"
+ "github.com/multica-ai/multica/server/internal/profiling"
"github.com/multica-ai/multica/server/internal/realtime"
"github.com/multica-ai/multica/server/internal/scheduler"
"github.com/multica-ai/multica/server/internal/service"
@@ -499,6 +500,7 @@ func main() {
Addr: ":" + port,
Handler: r,
}
+ profilingServer := profiling.NewServer()
// Start background workers.
sweepCtx, sweepCancel := context.WithCancel(context.Background())
@@ -599,6 +601,13 @@ func main() {
}()
}
+ go func() {
+ slog.Info("pprof server starting", "addr", profilingServer.Addr)
+ if err := profilingServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
+ slog.Error("pprof server disabled after startup error", "error", err)
+ }
+ }()
+
go func() {
slog.Info("server starting", "port", port)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
@@ -672,5 +681,10 @@ func main() {
}
metricsShutdownCancel()
}
+ profilingShutdownCtx, profilingShutdownCancel := context.WithTimeout(context.Background(), 3*time.Second)
+ if err := profilingServer.Shutdown(profilingShutdownCtx); err != nil {
+ slog.Error("pprof server forced to shutdown", "error", err)
+ }
+ profilingShutdownCancel()
slog.Info("server stopped")
}
diff --git a/server/cmd/server/metrics_test.go b/server/cmd/server/metrics_test.go
index 07ee235bbe..545cee8c10 100644
--- a/server/cmd/server/metrics_test.go
+++ b/server/cmd/server/metrics_test.go
@@ -11,15 +11,19 @@ import (
"github.com/multica-ai/multica/server/internal/realtime"
)
-func TestMainRouterDoesNotExposePrometheusMetrics(t *testing.T) {
+func TestMainRouterDoesNotExposeDiagnostics(t *testing.T) {
router := NewRouter(nil, realtime.NewHub(), events.New(), analytics.NoopClient{}, nil)
- rec := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodGet, "/metrics", nil)
- router.ServeHTTP(rec, req)
+ for _, path := range []string{"/metrics", "/debug/pprof/", "/debug/pprof/heap"} {
+ t.Run(path, func(t *testing.T) {
+ rec := httptest.NewRecorder()
+ req := httptest.NewRequest(http.MethodGet, path, nil)
+ router.ServeHTTP(rec, req)
- if rec.Code != http.StatusNotFound {
- t.Fatalf("main API /metrics status = %d, want %d", rec.Code, http.StatusNotFound)
+ if rec.Code != http.StatusNotFound {
+ t.Fatalf("main API %s status = %d, want %d", path, rec.Code, http.StatusNotFound)
+ }
+ })
}
}
diff --git a/server/internal/profiling/server.go b/server/internal/profiling/server.go
new file mode 100644
index 0000000000..47ee0790d5
--- /dev/null
+++ b/server/internal/profiling/server.go
@@ -0,0 +1,31 @@
+package profiling
+
+import (
+ "net/http"
+ httppprof "net/http/pprof"
+ "time"
+)
+
+const Addr = "127.0.0.1:6060"
+
+func NewHandler() http.Handler {
+ mux := http.NewServeMux()
+ mux.HandleFunc("GET /debug/pprof/", httppprof.Index)
+ mux.HandleFunc("GET /debug/pprof/cmdline", httppprof.Cmdline)
+ mux.HandleFunc("GET /debug/pprof/profile", httppprof.Profile)
+ mux.HandleFunc("GET /debug/pprof/symbol", httppprof.Symbol)
+ mux.HandleFunc("POST /debug/pprof/symbol", httppprof.Symbol)
+ mux.HandleFunc("GET /debug/pprof/trace", httppprof.Trace)
+ return mux
+}
+
+func NewServer() *http.Server {
+ return &http.Server{
+ Addr: Addr,
+ Handler: NewHandler(),
+ ReadHeaderTimeout: 5 * time.Second,
+ IdleTimeout: 30 * time.Second,
+ // CPU profiles and traces run for a caller-selected duration. Leave
+ // ReadTimeout and WriteTimeout unset so long captures are not truncated.
+ }
+}