Repository navigation
fix: add native lifecycle-aware health endpoint #7852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
diegosouzapw
merged 2 commits into
cheaper-inference:release/v3.8.49
from
RaviTharuma:fix/7848-native-healthz
Jul 20, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { getServerLifecyclePhase } from "@/lib/serverLifecycle"; | ||
|
|
||
| export const dynamic = "force-dynamic"; | ||
|
|
||
| const HEALTH_BODIES = { | ||
| ready: "ok\n", | ||
| starting: "starting\n", | ||
| stopping: "stopping\n", | ||
| } as const; | ||
|
|
||
| function createHealthResponse(method: "GET" | "HEAD"): Response { | ||
| const phase = getServerLifecyclePhase(); | ||
| const body = HEALTH_BODIES[phase]; | ||
|
|
||
| return new Response(method === "HEAD" ? null : body, { | ||
| status: phase === "ready" ? 200 : 503, | ||
| headers: { | ||
| "Cache-Control": "no-store", | ||
| "Content-Length": String(body.length), | ||
| "Content-Type": "text/plain; charset=utf-8", | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| export function GET(): Response { | ||
| return createHealthResponse("GET"); | ||
| } | ||
|
|
||
| export function HEAD(): Response { | ||
| return createHealthResponse("HEAD"); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| export type ServerLifecyclePhase = "starting" | "ready" | "stopping"; | ||
|
|
||
| declare global { | ||
| var __omnirouteServerLifecycle: ServerLifecyclePhase | undefined; | ||
| } | ||
|
|
||
| export function getServerLifecyclePhase(): ServerLifecyclePhase { | ||
| return globalThis.__omnirouteServerLifecycle ?? "starting"; | ||
| } | ||
|
|
||
| export function markServerStarting(): void { | ||
| globalThis.__omnirouteServerLifecycle = "starting"; | ||
| } | ||
|
|
||
| export function markServerReady(): void { | ||
| if (getServerLifecyclePhase() !== "stopping") { | ||
| globalThis.__omnirouteServerLifecycle = "ready"; | ||
| } | ||
| } | ||
|
|
||
| export function markServerStopping(): void { | ||
| globalThis.__omnirouteServerLifecycle = "stopping"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import assert from "node:assert/strict"; | ||
| import test from "node:test"; | ||
| import fs from "node:fs"; | ||
| import { createRequire } from "node:module"; | ||
|
|
||
| import { | ||
| getServerLifecyclePhase, | ||
| markServerReady, | ||
| markServerStopping, | ||
| } from "../../src/lib/serverLifecycle.ts"; | ||
|
|
||
| const routeModule = await import("../../src/app/healthz/route.ts"); | ||
| const require = createRequire(import.meta.url); | ||
| const { getMiddlewareMatchers } = require("next/dist/build/analysis/get-page-static-info.js"); | ||
| const { | ||
| getMiddlewareRouteMatcher, | ||
| } = require("next/dist/shared/lib/router/utils/middleware-route-matcher.js"); | ||
|
|
||
| test("/healthz follows the native server lifecycle without static caching", async () => { | ||
| assert.equal(routeModule.dynamic, "force-dynamic"); | ||
| assert.equal(getServerLifecyclePhase(), "starting"); | ||
|
|
||
| const starting = await routeModule.GET(new Request("http://localhost/healthz")); | ||
| assert.equal(starting.status, 503); | ||
| assert.equal(starting.headers.get("Cache-Control"), "no-store"); | ||
| assert.equal(starting.headers.get("Content-Type"), "text/plain; charset=utf-8"); | ||
| assert.equal(starting.headers.get("Content-Length"), "9"); | ||
| assert.equal(await starting.text(), "starting\n"); | ||
|
|
||
| markServerReady(); | ||
| const ready = await routeModule.GET(new Request("http://localhost/healthz")); | ||
| assert.equal(ready.status, 200); | ||
| assert.equal(ready.headers.get("Content-Length"), "3"); | ||
| assert.equal(await ready.text(), "ok\n"); | ||
|
|
||
| const readyHead = await routeModule.HEAD( | ||
| new Request("http://localhost/healthz", { method: "HEAD" }) | ||
| ); | ||
| assert.equal(readyHead.status, 200); | ||
| assert.equal(readyHead.headers.get("Content-Length"), "3"); | ||
| assert.equal(await readyHead.text(), ""); | ||
|
|
||
| markServerStopping(); | ||
| const stopping = await routeModule.GET(new Request("http://localhost/healthz")); | ||
| assert.equal(stopping.status, 503); | ||
| assert.equal(stopping.headers.get("Content-Length"), "9"); | ||
| assert.equal(await stopping.text(), "stopping\n"); | ||
|
|
||
| const stoppingHead = await routeModule.HEAD( | ||
| new Request("http://localhost/healthz", { method: "HEAD" }) | ||
| ); | ||
| assert.equal(stoppingHead.status, 503); | ||
| assert.equal(stoppingHead.headers.get("Content-Length"), "9"); | ||
| assert.equal(await stoppingHead.text(), ""); | ||
|
|
||
| markServerReady(); | ||
| assert.equal(getServerLifecyclePhase(), "stopping"); | ||
| }); | ||
|
|
||
| test("native startup and shutdown hooks drive the health lifecycle", () => { | ||
| const startupSource = fs.readFileSync("src/instrumentation-node.ts", "utf8"); | ||
| const registerStart = startupSource.indexOf("export async function registerNodejs"); | ||
| const markStarting = startupSource.indexOf("markServerStarting();", registerStart); | ||
| const firstStartupAwait = startupSource.indexOf("await ", registerStart); | ||
| const markReady = startupSource.lastIndexOf("markServerReady();"); | ||
|
|
||
| assert.ok(markStarting > registerStart && markStarting < firstStartupAwait); | ||
| assert.ok(markReady > firstStartupAwait); | ||
| assert.equal(startupSource.slice(markReady).trim(), "markServerReady();\n}"); | ||
|
|
||
| const shutdownSource = fs.readFileSync("src/lib/gracefulShutdown.ts", "utf8"); | ||
| const drainingFlag = shutdownSource.indexOf("state.shuttingDown = true;"); | ||
| const markStopping = shutdownSource.indexOf("markServerStopping();", drainingFlag); | ||
| const drainAwait = shutdownSource.indexOf("await waitForDrain();", drainingFlag); | ||
|
|
||
| assert.ok(markStopping > drainingFlag && markStopping < drainAwait); | ||
| }); | ||
|
|
||
| test("/healthz bypasses the centralized auth proxy matcher", () => { | ||
| const proxySource = fs.readFileSync("src/proxy.ts", "utf8"); | ||
| const matcherBlock = proxySource.match(/matcher:\s*\[([\s\S]*?)\]/)?.[1]; | ||
| assert.ok(matcherBlock, "proxy matcher configuration must remain discoverable"); | ||
|
|
||
| const configuredMatchers = Array.from( | ||
| matcherBlock.matchAll(/[\"']([^\"']+)[\"']/g), | ||
| (match) => match[1] | ||
| ); | ||
| const matcher = getMiddlewareRouteMatcher(getMiddlewareMatchers(configuredMatchers, "/")); | ||
|
|
||
| assert.equal(matcher("/healthz", { headers: {} }), false); | ||
| assert.equal(matcher("/api/system/version", { headers: {} }), true); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.