investigate: reported 502 on /api/v1/* has no matching code path in the current daemon
Status: needs a captured example before it is actionable
502 responses have been reported on /api/v1/*, but on investigation the current daemon HTTP layer contains no code path that emits 502:
rg -n "StatusBadGateway|502|BadGateway|httputil|ReverseProxy"overbackend/returns nothing in the request path.- When the daemon is down/unreachable, the renderer's
runtimeFetchreturns 503 (daemon_unavailable), and a direct loopback call fails withECONNREFUSED(a network error) — neither is a 502.
So my original hypothesis ("client hits the loopback before the daemon is ready") is not supported by the code and I've removed it.
Plausible remaining sources (unconfirmed)
- A dev-only intermediary (e.g. the Vite dev server proxy in front of
/api/v1) returning 502 while the daemon restarts. - An older daemon version that had a proxy/handler since removed.
- An intermediary outside this repo.
What would make this actionable
A concrete captured 502: the exact request path, method, daemon version, and whether it was a dev or packaged build. Without that, there is no reproducible cause to fix in the current code.
If nobody can produce a captured example, this should be closed as not-reproducible rather than left open.
@Pulkit7070 I wanna take on this issue
Investigated. Your core finding holds: no code path in the Go daemon emits 502, and none ever has. The reports are real, but every 502 comes from a proxy in front of the daemon — which is exactly why grepping backend/ found nothing.
I found two such proxies. One is dev-only; the other ships and is the more likely source of a user report.
1. The Vite dev-server proxy (dev only) — captured
frontend/vite.renderer.config.ts proxies /api and /mux to AO_DEV_API_TARGET ?? "http://127.0.0.1:3001", and AO_DEV_API_TARGET is set nowhere in the repo. Vite's proxy error handler is res.writeHead(502, {"Content-Type":"text/plain"}).end() ([email protected], dist/node/chunks/node.js:17938), so any unreachable target becomes a 502.
Captured on main @ dd47a7547, dev build, with a perfectly healthy daemon running:
$ curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:3002/api/v1/projects
200 # daemon, direct
$ curl -s -o /dev/null -w '%{http_code}' 'http://[::1]:5199/api/v1/projects'
502 # same daemon, through the dev proxy
[vite] http proxy error: /api/v1/projects
Error: connect ECONNREFUSED 127.0.0.1:3001
Positive control: with the daemon on 3001 instead, the same request through the same proxy returns 200.
Method/path/version: GET /api/v1/projects and /api/v1/sessions (any method, anything under /api), daemon dd47a7547, dev build. /healthz returns 200 in the same run because only /api and /mux are proxied — which is why the symptom is specific to /api/v1/*.
Provenance. The proxy default has been 127.0.0.1:3001 since the scaffold (c2c4404c7, #168). f2da70d3b (#2465) later introduced DEV_DAEMON_PORT = 3002 for the Electron dev supervisor and did not touch the Vite config. 3001 is still correct for a standalone daemon (config.DefaultPort), so this only bites when the daemon is on any other port.
This is not limited to npm run dev:web. The renderer's API base in dev is window.location.origin (renderer/lib/api-client.ts:9,13) — the Vite origin — until the daemon reports ready and renderer/lib/daemon-status.ts:6-11 redirects it to the real port. Everything requested before that redirect goes through the proxy, so plain npm run dev has the same exposure during startup. That makes your original hypothesis ("client hits the loopback before the daemon is ready") essentially right; it just wasn't findable in Go, because the 502 is Vite's.
Triage fingerprint: a Vite 502 has an empty body, Content-Type: text/plain, and a Vary: Origin header. The Go daemon never produces that shape.
2. The tailnet proxy AO installs for Connect Mobile (ships) — the likelier source of a report
backend/internal/mobilebridge/tailscaleserve.go:36-40 runs tailscale serve --bg --https=443 http://127.0.0.1:<bridgePort>, putting tailscaled's httputil.ReverseProxy directly in front of /api/v1/* for the phone app (daemon.go:496 puts the same chi router behind that port; packages/mobile/lib/api.ts:124 is literally const API = "/api/v1").
That serve config lives in tailscaled and outlives AO — the code says so itself at controllers/mobile.go:354-358. It is removed only by ShutdownServe, whose sole caller is the graceful-shutdown tail at daemon.go:581. After any ungraceful exit — SIGKILL, panic, OOM, power loss — the tailnet keeps routing :443 to a dead local port.
tailscale's proxy sets only Rewrite and Transport and never an ErrorHandler (ipn/ipnlocal/serve.go:959), so Go's default handler applies, which writes 502 on a dial failure. Reproduced with a faithful local replica of that exact proxy construction:
daemon alive -> 200 OK body={"ok":true,"path":"/api/v1/sessions"}
daemon killed (-9) -> 502 Bad Gateway body=""
http: proxy error: dial tcp 127.0.0.1:37567: connect: connection refused
Not reproduced against a real tailnet — no tailscaled on this machine — so this link is from tailscale's source plus the replica, not a live capture.
Why this is probably where the reports came from: packages/mobile/lib/api.ts:325 reports failures as captureMobileApiError(path, httpCategory(res.status), res.status), i.e. a Sentry message reading /api/v1/... failed: http_5xx (502). And packages/mobile/lib/connectionError.ts:43 already treats 502 as retryable — the client was written expecting them. A 502 arriving via mobile telemetry would look exactly like "502 reported on /api/v1/*" with no daemon code path.
Ruled out (so nobody repeats the search)
- "An older daemon version." Refuted. Exactly one commit in history ever added 502-emitting code:
ecdf0c73e(#1757) addedpackages/web/server/single-port-server.ts, an opt-in (AO_PATH_BASED_MUX=1) Node proxy in the pre-rewrite Next.js dashboard, deleted wholesale at the rewrite graft7a1b3030a. It frontedpackages/web/src/app/api/*—/api/v1did not exist pre-rewrite at all. - The Go HTTP layer — every reachable
http.Status*enumerated; no 502. Your finding confirmed. previewserver,preview,browserruntime,container— none proxies, forwards, or tunnels HTTP.- Electron main — never synthesizes 502; its only synthesized status is 503
daemon_unavailable. - Renderer / cloud-client / product-ui — no client generates a 502 of its own.
- Note
sentryobs.ShouldCaptureStatus(sentryobs.go:98) returns true for any 5xx except 503, so a 502 would be captured — it just never originates in the daemon.
Recommended disposition
Not "close as not-reproducible". Two concrete, distinct causes:
- Fix the dev proxy — PR incoming: resolve the target from the run file the daemon actually wrote (honouring
AO_RUN_FILE, then the dev-scoped file, then the standalone default, thenAO_PORT), reusingparseRunFile/defaultRunFilePath/expectedDaemonPortrather than a fixed port. Removes this whole class of report. - The tailnet proxy leak deserves its own issue —
ShutdownServeon the graceful path only means an ungraceful exit strands:443on a dead port until the next successfulApply. The comment atmobile.go:354already anticipates the hazard; the gap is that nothing re-clears it after a crash. Happy to file that separately with the above evidence if you'd like.
If a reporter can still be reached, the fingerprint in §1 (empty body + text/plain + Vary: Origin) distinguishes a Vite 502 from a tailscaled one in one glance.
Correction to my own comment above, before anyone acts on it.
I wrote that the reports were likely arriving via mobile Sentry telemetry (captureMobileApiError → /api/v1/... failed: http_5xx (502)). That code path exists, but it is inert today: @sentry/react-native is not in packages/mobile/package.json, and packages/mobile/lib/sentry-config.ts:6-10 says so in as many words — "this only takes effect once the @sentry/react-native SDK is installed ... until then sentry.ts stays a no-op." So no mobile Sentry event can have produced these reports. The tailnet-proxy mechanism itself is unaffected; only my guess at the reporting channel was wrong.
For completeness, the current telemetry picture, since it bears on how a 502 could have been observed at all:
- Desktop renderer Sentry is live —
@sentry/[email protected]is a real dependency andfrontend/src/shared/sentry-config.ts:12carries a real DSN. But it is gated on telemetry consent and on being a packaged build:renderer/lib/telemetry.ts:735-737notes the supervisor withholds the bootstrap for "an unpackaged build that has not opted in", and Sentry init rides that same gate (:746). So dev-mode 502s from the Vite proxy do not normally reach Sentry — which is also why fixing that proxy will not show up as a change in Sentry volume. - Events that do get sent carry
build_mode: dev | packaged(telemetry.ts:139), so if any 502 events do exist, filtering on that tag separates a developer's dev-proxy 502 from a real one. - Daemon-side,
sentryobs.ShouldCaptureStatuswould capture a 502 — but as established, the daemon never emits one.
Net effect on disposition: unchanged. Source 1 is captured and fixed in #4446. Source 2 stands on the code plus the proxy replica, not on telemetry — and it is worth noting that once mobile Sentry is switched on, a stranded tailnet proxy would start reporting exactly /api/v1/... failed: http_5xx (502), which is the shape of this issue's original report.
Filed source 2 separately as #4447 (stranded Connect Mobile tailnet proxy), with the reproduction and the ShutdownServe-is-graceful-only trace. Source 1 remains #4446.
That leaves this issue as the investigation record. Happy for it to be closed once #4446 lands, or kept open as the umbrella for both — whichever you prefer.
Closing in favor of #4447, which provides the concrete current-code source and remediation context for the shipped /api/v1/* 502: a persistent tailscale serve --bg proxy outliving an ungraceful AO exit and continuing to target the dead LAN-listener port. That issue is the stronger actionable record for this symptom.