fix(infra): guard os.networkInterfaces errors

This commit is contained in:
Hongpeng Jin 2026-01-29 16:50:21 -05:00
parent 4583f88626
commit 63da3808f4
2 changed files with 54 additions and 15 deletions

View File

@ -0,0 +1,33 @@
import { describe, expect, it, vi } from "vitest";
const realOs = await vi.importActual<typeof import("node:os")>("node:os");
vi.mock("node:os", () => {
const hostname = () => "moltbot-test-host";
const platform = () => "linux";
const networkInterfaces = () => {
throw new Error("uv_interface_addresses returned Unknown system error 1");
};
return {
...realOs,
default: {
...realOs,
hostname,
platform,
networkInterfaces,
},
hostname,
platform,
networkInterfaces,
};
});
describe("system-presence", () => {
it("does not crash when os.networkInterfaces throws", async () => {
vi.resetModules();
const { listSystemPresence } = await import("./system-presence.js");
const entries = listSystemPresence();
expect(entries.length).toBeGreaterThan(0);
expect(entries.some((entry) => entry.host === "moltbot-test-host")).toBe(true);
});
});

View File

@ -39,21 +39,27 @@ function normalizePresenceKey(key: string | undefined): string | undefined {
}
function resolvePrimaryIPv4(): string | undefined {
const nets = os.networkInterfaces();
const prefer = ["en0", "eth0"];
const pick = (names: string[]) => {
for (const name of names) {
const list = nets[name];
const entry = list?.find((n) => n.family === "IPv4" && !n.internal);
if (entry?.address) return entry.address;
}
for (const list of Object.values(nets)) {
const entry = list?.find((n) => n.family === "IPv4" && !n.internal);
if (entry?.address) return entry.address;
}
return undefined;
};
return pick(prefer) ?? os.hostname();
try {
const nets = os.networkInterfaces();
const prefer = ["en0", "eth0"];
const pick = (names: string[]) => {
for (const name of names) {
const list = nets[name];
const entry = list?.find((n) => n.family === "IPv4" && !n.internal);
if (entry?.address) return entry.address;
}
for (const list of Object.values(nets)) {
const entry = list?.find((n) => n.family === "IPv4" && !n.internal);
if (entry?.address) return entry.address;
}
return undefined;
};
return pick(prefer) ?? os.hostname();
} catch {
// Some environments (e.g. certain VPS setups) can throw from os.networkInterfaces().
// Fall back to hostname so the CLI can still boot.
return os.hostname();
}
}
function initSelfPresence() {