Skip to content

Answer /health from the health monitor's last check - #7001

Merged
jhrozek merged 1 commit into
stacklok:mainfrom
alex-feel:serve-health-from-last-check
Oct 30, 2026
Merged

jhrozek merged 1 commit into
stacklok:mainfrom
alex-feel:serve-health-from-last-check

Conversation

@alex-feel

Copy link
Copy Markdown
Contributor

Summary

The transparent proxy answers /health by pinging the backend while the request waits, so the answer takes as long as the backend's GET /, up to the 5 s ping timeout. The operator's startup and readiness probes give up after 3 s and its liveness probe after 5 s, so a backend that is slow to answer GET / fails the probes, and three liveness failures make the kubelet restart a proxy whose backend is alive. The background monitor already pings the backend on its own interval, so /health can answer from that instead.

  • So that a slow backend cannot hold /health past a probe's timeout, pkg/healthcheck gains an opt-in WithLastResult() option: with it, CheckHealth records a copy of each result and ServeHTTP answers from the last one without pinging. The body shape and the status mapping stay as they are (healthy and degraded answer 200), and NewHealthChecker takes the option as a variadic argument, so the other callers compile unchanged.
  • So that /health has a current answer from the start, the transparent proxy sets the option whenever health checks are enabled, and monitorHealth checks once when it starts and on every tick, before initialization too; failures still count toward shutdown only after the MCP server is initialized.
  • So that operators can see what /health answers and when the monitor pings, docs/arch/03-transport-architecture.md documents both, and two comments that described the old skip are rewritten.

The HTTP-SSE and streamable HTTP proxies (pkg/transport/proxy/httpsse/http_proxy.go:254, pkg/transport/proxy/streamable/streamable_proxy.go:262) keep building their answer per request, since their checks never wait on a backend. The operator's probe values do not change: raising them above the ping timeout would also avoid the restart, but it ties two components' constants together and breaks again when TOOLHIVE_HEALTH_CHECK_PING_TIMEOUT is raised.

Closes #7000

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

  • Unit tests (task test)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

task lint-fix, task lint and task test pass on Linux at this head on top of current main, with golangci-lint v2.15.0, the version lint.yml pins.

New tests: pkg/healthcheck/last_result_test.go covers the answer before any result, a recorded healthy result, and a recorded degraded one, with a pinger that blocks every ping after the first so that an inline ping could not answer in time; that the recorded result is a copy; and concurrent CheckHealth and ServeHTTP calls under -race. pkg/transport/proxy/transparent/health_monitor_test.go starts the real proxy in front of a backend whose GET / takes 7 s and expects /health within 500 ms, expects the first check to be recorded at start with a one-hour interval, and checks that failures before initialization show as degraded, neither stop the proxy nor carry over, and that failures after initialization still shut it down. Against main, TestTransparentProxy_HealthAnswersWhileBackendIsSlow and TestTransparentProxy_HealthCheckFailuresNotCountedBeforeInitialization fail and the pkg/healthcheck tests do not compile. Dropping the start-up check, counting failures before initialization, recording a shallow copy, and pinging inline with the option set each fail at least one of the new tests. The reproduction in the issue passes: every probe gets 200 at once, the 4 s and 7 s backends included.

Changes

File Change
pkg/healthcheck/healthcheck.go WithLastResult option; CheckHealth records a copy of each result and ServeHTTP answers from it
pkg/transport/proxy/transparent/transparent_proxy.go Sets the option with health checks enabled; monitorHealth checks at start and on every tick and counts failures only after initialization
pkg/healthcheck/last_result_test.go, pkg/transport/proxy/transparent/health_monitor_test.go New tests
pkg/transport/proxy/transparent/transparent_test.go Removes the test that expected no check before initialization; two routing tests turn health checks off
docs/arch/03-transport-architecture.md Checks before initialization and what /health answers
test/e2e/health_check_zombie_test.go Comment on what initialization now gates

Does this introduce a user-facing change?

Yes. Before: with health checks enabled (local workloads, and remote ones that set TOOLHIVE_REMOTE_HEALTHCHECKS), each /health request pinged the backend and waited up to the ping timeout, so a backend slow to answer GET / made the operator's 3 s startup and readiness probes time out, and at 5 s the liveness probe too, and the kubelet restarted the proxy. After: /health answers at once from the monitor's last check, which is at most about one interval plus one ping timeout old, or two ping timeouts plus the retry delay after a failed check (15 s either way with the defaults); its timestamp and mcp.last_checked say when that check started, and a backend that missed the ping timeout shows as degraded with HTTP 200, as before. Until the first check completes, /health reports healthy without the mcp member.

Operators: the backend now receives health pings only from the monitor: one at start-up, one per interval before and after initialization, and the retry after a failed check once the server is initialized, as before; kubelet probes no longer reach it. The Debug line for a tick before initialization now reads mcp server not initialized yet, not counting health check and carries the check's status.

Special notes for reviewers

  • Before the first check completes, /health answers healthy without mcp, the same answer as with health checks disabled, rather than pinging inline once or answering 503: Kubernetes sees 200 in that window either way, as on main, where degraded also answers 200, and the window lasts only until the monitor's first ping, started with the monitor, returns or times out.
  • TestTransparentProxy_HealthCheckNotRunBeforeInitialization is removed because its premise, no check before initialization, no longer holds; TestTransparentProxy_HealthCheckFailuresNotCountedBeforeInitialization covers the part that still holds, that failures before initialization never stop the proxy. TestPrefixHandlers_MountingAndRouting and TestMetricsNotServedWithoutHandler now pass enableHealthCheck=false: the monitor's start-up ping reached their recording backend and read as a proxied request. Their routing assertions do not change.
  • The vMCP server already answers backend health from its monitor's state instead of probing on request (handleBackendHealth, pkg/vmcp/server/server.go:1514, reading healthMon.GetAllBackendStates() at :1481). This change records inside pkg/healthcheck rather than reusing pkg/vmcp/health, because HealthChecker is shared by the three proxies and already defines the /health body; the option only adds the stored result.
  • Mark failing remote MCP servers unhealthy instead of starting #4844, still open, changes the same pre-initialization check in monitorHealth so that remote workloads count failures before initialization; its condition applies unchanged to the counting check this PR keeps, and whichever PR lands second needs a small rebase.

The transparent proxy answered every /health request by pinging the backend while the request waited, so a backend slow to answer GET / held the answer for up to the 5 s ping timeout.
The operator's startup and readiness probes time out after 3 s and its liveness probe after 5 s, so such a backend failed the probes and the kubelet restarted a proxy whose backend was alive.
With health checks enabled, the health checker now records the result of each background monitor check, and /health answers from the last one without pinging.
Until the first check completes, /health reports the proxy as healthy without the mcp member.
The monitor checks once when it starts and on every tick, also before the MCP server is initialized, and still counts failures toward shutdown only after initialization.
The HTTP-SSE and streamable proxies keep building their answer per request, because their checks never wait on a backend.

Signed-off-by: Aleksandr Filippov <[email protected]>
@codecov

codecov Bot commented Oct 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.33%. Comparing base (20bc1a1) to head (56f973a).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7001      +/-   ##
==========================================
+ Coverage   80.29%   80.33%   +0.04%     
==========================================
  Files         847      848       +1     
  Lines       86142    86334     +192     
==========================================
+ Hits        69165    69354     +189     
- Misses      16972    16975       +3     
  Partials        5        5              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, thanks. Answering /health from the monitor's last check is the right fix, and the tests show it.

One small non-blocking nit in docs/arch/03-transport-architecture.md: the staleness sentence reads as if the bound becomes two ping timeouts plus the retry delay after any failed check, but the retry only happens after initialization. Before that, a failed result just waits for the next tick, so the bound is really the larger of the two (interval + ping timeout vs. 2x ping timeout + retry delay). Both happen to be 15s with the defaults, so it only matters if someone raises the interval. Something like "at most about one interval plus one ping timeout old; once the MCP server is initialized, a failed check that is retried can instead stay current for two ping timeouts plus the retry delay, whichever is longer" would cover it.

@jhrozek
jhrozek merged commit cf5cd34 into stacklok:main Oct 30, 2026
43 checks passed
ChrisJBurns added a commit to slyt3/toolhive that referenced this pull request Nov 25, 2026
Resolves conflicts with stacklok#7001, which made the health monitor check
before the MCP server is initialized so /health answers from the last
check. Remote workloads now count those checks from the start; local
workloads keep counting only after initialization, so the test stacklok#7001
added for that behavior now runs against a local proxy.

Adds the onHealthCheckRecovered constructor argument to the proxy
calls main added, and keeps waitForShutdown for local-proxy shutdown
alongside the remote tests' waitForCallback. Remote health checks now
run by default, so the consent-cookie test's backend ignores the
cookie-less health check requests.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCPServer proxy fails its kubelet probes and is restarted while the backend is slow to answer GET /

2 participants