Repository navigation
fix(serve): honour HOSTNAME from .env instead of hardcoding 0.0.0.0 (#5134) #5170
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.39
from
anki1kr:fix/5134-hostname-env-override
Jun 27, 2026
Merged
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -136,7 +136,7 @@ export async function runServe(opts = {}) { | |
| PORT: String(dashboardPort), | ||
| DASHBOARD_PORT: String(dashboardPort), | ||
| API_PORT: String(apiPort), | ||
| HOSTNAME: "0.0.0.0", | ||
| HOSTNAME: process.env.HOSTNAME || "0.0.0.0", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| NODE_ENV: "production", | ||
| NODE_OPTIONS: `--max-old-space-size=${memoryLimit}`, | ||
| }; | ||
|
|
||
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,29 @@ | ||
| import test from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
|
|
||
| /** | ||
| * Replicate the HOSTNAME resolution from bin/cli/commands/serve.mjs to verify | ||
| * that the spawned server honours a HOSTNAME provided via env/.env instead of | ||
| * always hardcoding "0.0.0.0" (#5134). Mirrors the in-file replication pattern | ||
| * used by cli-serve-port.test.ts (serve.mjs spawns processes, so the logic is | ||
| * tested in isolation rather than imported). | ||
| */ | ||
| function resolveHostname(envHostname: string | undefined): string { | ||
| return envHostname || "0.0.0.0"; | ||
| } | ||
|
|
||
| test("serve hostname: honours HOSTNAME env var when set", () => { | ||
| assert.equal(resolveHostname("127.0.0.1"), "127.0.0.1"); | ||
| }); | ||
|
|
||
| test("serve hostname: honours a specific bind interface", () => { | ||
| assert.equal(resolveHostname("192.168.0.15"), "192.168.0.15"); | ||
| }); | ||
|
|
||
| test("serve hostname: falls back to 0.0.0.0 when HOSTNAME is unset", () => { | ||
| assert.equal(resolveHostname(undefined), "0.0.0.0"); | ||
| }); | ||
|
|
||
| test("serve hostname: falls back to 0.0.0.0 when HOSTNAME is an empty string", () => { | ||
| assert.equal(resolveHostname(""), "0.0.0.0"); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HOSTNAME\n\nUsingprocess.env.HOSTNAME || \"0.0.0.0\"introduces a critical bug in environments where the OS or container runtime automatically exports theHOSTNAMEenvironment variable (such as Docker, systemd, or various cloud VMs).\n\n#### The Problem:\n1. Docker Containers: Docker automatically setsHOSTNAMEto the container ID (e.g.,3a4b5c6d7e8f). With this change, the server will attempt to bind to3a4b5c6d7e8f(the container's internal IP) instead of0.0.0.0. This completely breaks port forwarding, making the dashboard inaccessible from the host machine.\n2. Linux/macOS VPS: Many systems export the machine's hostname in the environment. If the hostname does not resolve to a local bindable interface, the server will fail to start with anEADDRNOTAVAILerror.\n\n#### The Solution:\nWe should only honorprocess.env.HOSTNAMEif it is a user-configured bind address (like an IP orlocalhost) and not the system's default hostname. We can achieve this by comparingprocess.env.HOSTNAMEagainstos.hostname().