import { execFile, spawn } from "node:child_process"; import dgram from "node:dgram"; import { mkdtemp, mkdir, readFile, rm, symlink, utimes, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import net from "node:net"; import { promisify } from "node:util"; import { afterEach, describe, expect, it } from "vitest"; import { processBirthSignature } from "@devfn/processes"; import { allocateEphemeralPort, FilePortRegistry, isFetchBlockedPort, isPortAvailable, renderPolicyInventory, resolvePolicy, withFileLock } from "../src/index.js"; import { inspectContainerRunning } from "../src/registry.js"; import { temporaryDirectories } from "../../test-support/temp-directories.mjs"; import { caddyAdminLockPath } from "../../test-support/shared-locks.mjs"; // Removed after each test, whether it passes, fails or times out. const tempDir = temporaryDirectories(afterEach); // Fixtures that need the fixed Caddy admin port 127.0.0.1:2019 free share one // machine-wide lock with other DevFn packages' fixtures that bind it. const withCaddyAdminPort = async (action: () => Promise): Promise => await withFileLock(caddyAdminLockPath(), action, { timeoutMs: 120_000 }); describe("FilePortRegistry", () => { for (const protocol of ["tcp", "udp"] as const) { it(`preflights renamed and swapped exact ${protocol} leases by ready invocation and host`, async () => { const dir = await mkdtemp(path.join(tmpdir(), "devfn-replacement-owner-")); let occupied = false; const registry = new FilePortRegistry(path.join(dir, "registry.json"), undefined, async () => !occupied); const first = await allocateEphemeralPort(); let second = await allocateEphemeralPort(); while (second === first) second = await allocateEphemeralPort(); try { await registry.reserve({ projectId: "app", instanceId: "same", invocationId: "old", profile: "default", requests: [{ name: "a", spec: { preferred: first, exact: true, protocol } }, { name: "b", spec: { preferred: second, exact: true, protocol } }] }); await registry.markActive("old"); occupied = true; const requests = [{ name: "renamed-a", spec: { preferred: second, exact: true, protocol } }, { name: "renamed-b", spec: { preferred: first, exact: true, protocol } }]; await registry.assertReplacementAvailable([], "same", requests, async () => undefined, "old"); const swapped = await registry.reserve({ projectId: "app", instanceId: "same", invocationId: "new", replacingInvocationId: "old", profile: "default", requests }); expect(swapped.map((item) => item.port)).toEqual([second, first]); await expect(registry.assertReplacementAvailable([], "same", [{ name: "public", spec: { preferred: first, exact: true, protocol, exposure: "public" } }], async () => undefined, "old")).rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT" }); } finally { await rm(dir, { recursive: true, force: true }); } }); } it("retains an expired cross-protocol proxy claim when an IPv6 listener is hidden from OS inspection", async ({ skip }) => await withCaddyAdminPort(async () => { // The claim retires at the end only when the admin port is provably free. if (!await isPortAvailable(2019, "tcp", "127.0.0.1")) skip("127.0.0.1:2019 is in use, so listener absence cannot be proven on this host."); const dir = await mkdtemp(path.join(tmpdir(), "devfn-ipv6-claim-")); const port = await allocateEphemeralPort(); const registry = new FilePortRegistry(path.join(dir, "registry.json")); let listener = net.createServer(); const previousPath = process.env.PATH; try { await registry.reserve({ projectId: "app", instanceId: "proxy", invocationId: "old", profile: "default", requests: [], proxyListenerPorts: [port] }); await registry.updateInvocation("old", { state: "starting" }); const state = await registry.read(); state.invocations[0].updatedAt = "2020-01-01T00:00:00.000Z"; await writeFile(registry.filePath, JSON.stringify(state)); const birthSignature = await processBirthSignature(process.pid); expect(birthSignature).toBeTruthy(); await writeFile(path.join(dir, "proxy-owner.json"), JSON.stringify({ pid: process.pid, birthSignature })); const toolsDir = path.join(dir, "tools"); await mkdir(toolsDir); await symlink("/bin/ps", path.join(toolsDir, "ps")); process.env.PATH = toolsDir; for (const host of ["::1", "::"]) { listener = net.createServer(); await new Promise((resolve, reject) => listener.once("error", reject).listen({ port, host, ipv6Only: true }, resolve)); await expect(registry.reserve({ projectId: "app", instanceId: "sibling", invocationId: `sibling-${host}`, profile: "default", requests: [{ name: "api", spec: { preferred: port, exact: true, protocol: "udp" } }] })).rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT" }); expect((await registry.read()).invocations[0].state).toBe("starting"); await new Promise((resolve) => listener.close(() => resolve())); } // A live owner whose configuration cannot be read may still listen. await expect(registry.reserve({ projectId: "app", instanceId: "sibling", invocationId: "sibling-owner", profile: "default", requests: [{ name: "api", spec: { preferred: port, exact: true, protocol: "udp" } }] })).rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT" }); await rm(path.join(dir, "proxy-owner.json")); expect((await registry.reserve({ projectId: "app", instanceId: "sibling", invocationId: "sibling-free", profile: "default", requests: [{ name: "api", spec: { preferred: port, exact: true, protocol: "udp" } }] }))[0].port).toBe(port); } finally { process.env.PATH = previousPath; if (listener.listening) await new Promise((resolve) => listener.close(() => resolve())); await rm(dir, { recursive: true, force: true }); } }), 150_000); for (const invocationState of ["planning", "starting", "ready", "stopping"] as const) { it(`reclaims an abandoned ${invocationState} claim with a reused owner PID`, async () => await withCaddyAdminPort(async () => { const dir = await mkdtemp(path.join(tmpdir(), "devfn-reused-proxy-owner-")); const port = 18453; const registry = new FilePortRegistry(path.join(dir, "registry.json"), async () => port + 1, async () => true); const reserve = (invocationId: string, protocol: "tcp" | "udp", exact: boolean) => registry.reserve({ projectId: "app", instanceId: "legacy", invocationId, profile: "default", requests: [{ name: "api", spec: { preferred: port, exact, protocol, range: [port, port + 1] } }], }); try { await registry.reserve({ projectId: "app", instanceId: "abandoned", invocationId: "old", profile: "default", requests: [], proxyListenerPorts: [port] }); await registry.updateInvocation("old", { state: invocationState }); await writeFile(path.join(dir, "proxy-owner.json"), JSON.stringify({ pid: process.pid, birthSignature: `${(await processBirthSignature(process.pid))!.split(":")[0]}:reused-pid` })); const state = await registry.read(); state.invocations[0].updatedAt = "2020-01-01T00:00:00.000Z"; await writeFile(registry.filePath, JSON.stringify(state)); // Normal sibling startup must recover the expired claim by itself. expect((await reserve("tcp-exact", "tcp", true))[0]).toMatchObject({ port, source: "exact" }); expect((await registry.read()).invocations[0]).toMatchObject({ state: "failed", errorCode: "DEVFN_INTERRUPTED" }); await registry.gc(); await registry.release({ invocationId: "tcp-exact" }); expect((await reserve("udp-exact", "udp", true))[0]).toMatchObject({ port, source: "exact" }); await registry.release({ invocationId: "udp-exact" }); expect((await reserve("tcp-preferred", "tcp", false))[0]).toMatchObject({ port, source: "preferred" }); await registry.release({ invocationId: "tcp-preferred" }); expect((await reserve("tcp-stable", "tcp", false))[0]).toMatchObject({ port, source: "stable" }); } finally { await rm(dir, { recursive: true, force: true }); } }), 150_000); } it("bounds stopping and replacement protection to live, recently refreshed lifecycles", async () => { const dir = await mkdtemp(path.join(tmpdir(), "devfn-bounded-protection-")); const registry = new FilePortRegistry(path.join(dir, "registry.json"), undefined, async () => true); const port = await allocateEphemeralPort(); const dead = { pid: 2_147_483_000 }; const age = async (ids: string[]) => { const state = await registry.read(); for (const invocation of state.invocations) if (ids.includes(invocation.id)) invocation.updatedAt = "2020-01-01T00:00:00.000Z"; await writeFile(registry.filePath, JSON.stringify(state)); }; const leaseState = async (id: string) => (await registry.read()).allocations.find((item) => item.invocationId === id)?.state; try { // An interrupted down: a fresh stopping claim stays protected, an // expired one with a verified-dead owner is reconciled. await registry.reserve({ projectId: "app", instanceId: "down", invocationId: "down", profile: "default", requests: [{ name: "api", spec: { preferred: port, exact: true } }] }); await registry.markActive("down", { api: { process: dead } }); await registry.updateInvocation("down", { state: "stopping" }); await registry.reconcile(); expect(await leaseState("down")).toBe("active"); await age(["down"]); await registry.reconcile(); expect(await leaseState("down")).toBe("stale"); expect((await registry.reserve({ projectId: "app", instanceId: "sibling", invocationId: "sibling", profile: "default", requests: [{ name: "api", spec: { preferred: port, exact: true } }] }))[0].port).toBe(port); await registry.release({ invocationId: "sibling" }); // An interrupted replacement protects its predecessor only while its // heartbeat is current. await registry.reserve({ projectId: "app", instanceId: "same", invocationId: "old", profile: "default", requests: [{ name: "api", spec: { preferred: port, exact: true } }] }); await registry.markActive("old", { api: { process: dead } }); await registry.reserve({ projectId: "app", instanceId: "same", invocationId: "new", replacingInvocationId: "old", profile: "default", requests: [{ name: "api", spec: { preferred: port, exact: true } }] }); await registry.updateInvocation("new", { state: "starting" }); await registry.reconcile(); expect(await leaseState("old")).toBe("active"); await age(["new"]); await registry.reconcile(); expect(await leaseState("old")).toBe("stale"); } finally { await rm(dir, { recursive: true, force: true }); } }); it("reclaims an abandoned starting proxy claim after its lease becomes stale", async () => await withCaddyAdminPort(async () => { const dir = await mkdtemp(path.join(tmpdir(), "devfn-abandoned-proxy-")); const file = path.join(dir, "registry.json"); const port = 18445; const registry = new FilePortRegistry(file, async () => port + 1, async () => true); try { await registry.reserve({ projectId: "app", instanceId: "abandoned", invocationId: "old", profile: "default", requests: [{ name: "api", spec: { preferred: port + 1 } }], proxyListenerPorts: [port] }); await registry.updateInvocation("old", { state: "starting" }); const state = JSON.parse(await readFile(file, "utf8")); state.invocations[0].updatedAt = "2020-01-01T00:00:00.000Z"; state.allocations[0].updatedAt = "2020-01-01T00:00:00.000Z"; await writeFile(file, JSON.stringify(state)); const sibling = await registry.reserve({ projectId: "app", instanceId: "legacy", invocationId: "legacy", profile: "default", requests: [{ name: "api", spec: { preferred: port, exact: true, protocol: "udp" } }] }); expect(sibling[0]).toMatchObject({ port, source: "exact", protocol: "udp" }); expect((await registry.read()).invocations[0]).toMatchObject({ state: "failed", errorCode: "DEVFN_INTERRUPTED" }); await registry.release({ invocationId: "legacy" }); await registry.gc(); expect((await registry.read()).invocations).toEqual([]); const tcp = await registry.reserve({ projectId: "app", instanceId: "legacy", invocationId: "legacy-tcp", profile: "default", requests: [{ name: "api", spec: { preferred: port, exact: true, protocol: "tcp" } }] }); expect(tcp[0]).toMatchObject({ port, protocol: "tcp" }); } finally { await rm(dir, { recursive: true, force: true }); } }), 150_000); it("reclaims an abandoned listener claim beside a mixed-case route owned by another instance", async () => await withCaddyAdminPort(async () => { const dir = await mkdtemp(path.join(tmpdir(), "devfn-abandoned-case-route-")); const port = 18450; const registry = new FilePortRegistry(path.join(dir, "registry.json"), async () => port + 1, async () => true); try { await registry.reserve({ projectId: "app", instanceId: "abandoned", invocationId: "old", profile: "default", requests: [{ name: "api", spec: { preferred: port + 1 } }], proxyListenerPorts: [port] }); await registry.updateInvocation("old", { state: "starting" }); const state = JSON.parse(await readFile(registry.filePath, "utf8")); state.invocations[0].updatedAt = "2020-01-01T00:00:00.000Z"; state.allocations[0].updatedAt = "2020-01-01T00:00:00.000Z"; await writeFile(registry.filePath, JSON.stringify(state)); await writeFile(path.join(dir, "proxy-routes.pending.json"), JSON.stringify({ version: 1, routes: [{ id: "sibling", instanceId: "sibling", hostname: "Sibling.localhost", targetHost: "127.0.0.1", targetPort: port + 2, tls: "off", updatedAt: "now", }] })); await registry.reconcile(); await registry.gc(); expect((await registry.read()).invocations).toEqual([]); const allocation = await registry.reserve({ projectId: "app", instanceId: "legacy", invocationId: "legacy", profile: "default", requests: [{ name: "api", spec: { preferred: port, exact: true, protocol: "udp" } }] }); expect(allocation[0].port).toBe(port); } finally { await rm(dir, { recursive: true, force: true }); } }), 150_000); it("retains a ready proxy claim while an owned route or live Caddy owner remains", async () => await withCaddyAdminPort(async () => { const dir = await mkdtemp(path.join(tmpdir(), "devfn-ready-proxy-")); const file = path.join(dir, "registry.json"); const port = 18446; const registry = new FilePortRegistry(file, async () => port + 1, async () => true); const reserveSibling = () => registry.reserve({ projectId: "app", instanceId: "legacy", invocationId: "legacy", profile: "default", requests: [{ name: "api", spec: { preferred: port, exact: true, protocol: "tcp" } }] }); try { await registry.reserve({ projectId: "app", instanceId: "proxy", invocationId: "ready", profile: "default", requests: [{ name: "api", spec: { preferred: port + 1 } }], proxyListenerPorts: [port] }); await registry.markActive("ready"); const state = JSON.parse(await readFile(file, "utf8")); state.invocations[0].updatedAt = "2020-01-01T00:00:00.000Z"; await writeFile(file, JSON.stringify(state)); const routeFile = path.join(dir, "proxy-routes.json"); await writeFile(routeFile, JSON.stringify({ version: 1, routes: [{ id: "r", instanceId: "proxy", hostname: "app.localhost", targetHost: "127.0.0.1", targetPort: port + 1, tls: "off", updatedAt: new Date().toISOString() }] })); await registry.reconcile(); await registry.gc(); await expect(reserveSibling()).rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT", details: { instanceId: "proxy", action: expect.stringContaining("stop proxy instance proxy"), } }); await rm(routeFile); await writeFile(path.join(dir, "proxy-owner.json"), JSON.stringify({ pid: process.pid })); await registry.reconcile(); expect((await registry.read()).invocations[0].state).toBe("ready"); await rm(path.join(dir, "proxy-owner.json")); const pendingFile = path.join(dir, "proxy-routes.pending.json"); await writeFile(pendingFile, JSON.stringify({ version: 1, routes: [{ id: "pending", instanceId: "proxy", hostname: "app.localhost", targetHost: "127.0.0.1", targetPort: port + 1, tls: "off", updatedAt: new Date().toISOString() }] })); await registry.reconcile(); expect((await registry.read()).invocations[0].state).toBe("ready"); await writeFile(pendingFile, "{}"); await registry.reconcile(); expect((await registry.read()).invocations[0].state).toBe("ready"); await rm(pendingFile); await registry.reconcile(); await registry.gc(); expect((await reserveSibling())[0].port).toBe(port); } finally { await rm(dir, { recursive: true, force: true }); } }), 150_000); it("keeps a port targeted by committed or pending proxy routes from sibling reservations", async () => { const dir = await mkdtemp(path.join(tmpdir(), "devfn-routed-target-")); const registry = new FilePortRegistry(path.join(dir, "registry.json"), undefined, async () => true); const port = await allocateEphemeralPort(); const request = (exact: boolean) => [{ name: "api", spec: { preferred: port, exact } }]; try { for (const name of ["proxy-routes.json", "proxy-routes.pending.json"]) { // An interrupted replacement can leave a route on a port whose lease // was already released; the route still sends traffic there. await writeFile(path.join(dir, name), JSON.stringify({ version: 1, routes: [{ id: "routed:app", instanceId: "routed", hostname: "app.localhost", targetHost: "127.0.0.1", targetPort: port, tls: "off", updatedAt: new Date().toISOString() }] })); await expect(registry.reserve({ projectId: "app", instanceId: "sibling", invocationId: `exact-${name}`, profile: "default", requests: request(true) })) .rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT", details: { port, instanceId: "routed" } }); await expect(registry.assertReplacementAvailable([], "sibling", request(true), async () => undefined)) .rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT", details: { port, instanceId: "routed" } }); const moved = await registry.reserve({ projectId: "app", instanceId: "sibling", invocationId: `preferred-${name}`, profile: "default", requests: request(false) }); expect(moved[0].port).not.toBe(port); await registry.release({ invocationId: `preferred-${name}` }); // The routed instance itself repairs the route onto this port. expect((await registry.reserve({ projectId: "app", instanceId: "routed", invocationId: `owner-${name}`, profile: "default", requests: request(true) }))[0].port).toBe(port); await registry.release({ invocationId: `owner-${name}` }); await rm(path.join(dir, name)); } expect((await registry.reserve({ projectId: "app", instanceId: "sibling", invocationId: "unrouted", profile: "default", requests: request(true) }))[0].port).toBe(port); } finally { await rm(dir, { recursive: true, force: true }); } }); it("never renews a replacement's own lease on a port that another instance's route targets", async () => { const dir = await mkdtemp(path.join(tmpdir(), "devfn-routed-replacement-")); const registry = new FilePortRegistry(path.join(dir, "registry.json"), undefined, async () => true); const port = await allocateEphemeralPort(); const request = [{ name: "api", spec: { preferred: port, exact: true } }]; try { // The ready lifecycle already leases the port another instance's // route still sends its traffic to. await registry.reserve({ projectId: "app", instanceId: "same", invocationId: "ready", profile: "default", requests: request }); await registry.markActive("ready"); await registry.updateInvocation("ready", { state: "ready" }); await writeFile(path.join(dir, "proxy-routes.json"), JSON.stringify({ version: 1, routes: [{ id: "routed:app", instanceId: "routed", hostname: "app.localhost", targetHost: "127.0.0.1", targetPort: port, tls: "off", updatedAt: new Date().toISOString() }] })); await expect(registry.assertReplacementAvailable([], "same", request, async () => undefined, "ready")) .rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT", details: { port, instanceId: "routed" } }); await expect(registry.reserve({ projectId: "app", instanceId: "same", invocationId: "replacement", profile: "default", replacingInvocationId: "ready", requests: request })) .rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT", details: { port, instanceId: "routed" } }); const moved = await registry.reserve({ projectId: "app", instanceId: "same", invocationId: "moved", profile: "default", replacingInvocationId: "ready", requests: [{ name: "api", spec: { preferred: port } }] }); expect(moved[0].port).not.toBe(port); } finally { await rm(dir, { recursive: true, force: true }); } }); it("keeps an abandoned listener claim when route or owner evidence is malformed", async () => await withCaddyAdminPort(async () => { const dir = await mkdtemp(path.join(tmpdir(), "devfn-claim-invalid-state-")); const registry = new FilePortRegistry(path.join(dir, "registry.json"), async () => 18449, async () => true); const port = 18448; const sibling = (protocol: "tcp" | "udp") => registry.reserve({ projectId: "legacy", instanceId: "legacy", invocationId: `legacy-${protocol}`, profile: "default", requests: [{ name: "listener", spec: { preferred: port, exact: true, protocol } }] }); try { await registry.reserve({ projectId: "app", instanceId: "abandoned", invocationId: "old", profile: "default", requests: [{ name: "api", spec: { preferred: port + 1 } }], proxyListenerPorts: [port] }); await registry.updateInvocation("old", { state: "starting" }); const state = JSON.parse(await readFile(registry.filePath, "utf8")); state.invocations[0].updatedAt = "2020-01-01T00:00:00.000Z"; state.allocations[0].updatedAt = "2020-01-01T00:00:00.000Z"; await writeFile(registry.filePath, JSON.stringify(state)); for (const name of ["proxy-routes.json", "proxy-routes.pending.json"]) { await writeFile(path.join(dir, name), JSON.stringify({ version: 1, routes: [{ id: "", instanceId: "other", hostname: "other.localhost", targetHost: "127.0.0.1", targetPort: port + 1, tls: "off", updatedAt: new Date().toISOString() }] })); await registry.reconcile(); await registry.gc(); expect((await registry.read()).invocations[0].state).toBe("starting"); // Unreadable routes cannot prove which target ports they still use. for (const protocol of ["tcp", "udp"] as const) { await expect(sibling(protocol)).rejects.toMatchObject({ code: "DEVFN_REGISTRY_INVALID" }); } await rm(path.join(dir, name)); } await writeFile(path.join(dir, "proxy-owner.json"), JSON.stringify({ pid: 999999, birthSignature: 42 })); await registry.gc(); await expect(sibling("tcp")).rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT", details: { instanceId: "abandoned" } }); await rm(path.join(dir, "proxy-owner.json")); await registry.reconcile(); await registry.gc(); expect((await sibling("udp"))[0].port).toBe(port); } finally { await rm(dir, { recursive: true, force: true }); } }), 150_000); it("claims proxy listeners atomically across TCP and UDP and releases failed claims", async () => await withCaddyAdminPort(async () => { const dir = tempDir("devfn-registry-proxy-"); const port = 18443; const registry = new FilePortRegistry(path.join(dir, "registry.json"), async () => port + 1, async () => true); const request = (instanceId: string, invocationId: string, protocol: "tcp" | "udp", exact = true) => registry.reserve({ projectId: "app", instanceId, invocationId, profile: "default", requests: [{ name: "api", spec: { preferred: port, exact, protocol } }] }); expect((await request("legacy", "legacy-exact", "tcp"))[0]).toMatchObject({ port, source: "exact" }); await registry.release({ invocationId: "legacy-exact" }); expect((await request("legacy", "legacy-preferred", "udp", false))[0]).toMatchObject({ port, source: "preferred" }); await expect(registry.reserve({ projectId: "app", instanceId: "proxy", invocationId: "blocked", profile: "default", requests: [], proxyListenerPorts: [port] })).rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT" }); expect((await registry.read()).invocations.map((item) => item.id)).not.toContain("blocked"); await registry.release({ invocationId: "legacy-preferred" }); expect((await request("legacy", "legacy-stable", "udp", false))[0]).toMatchObject({ port, source: "stable" }); await registry.release({ invocationId: "legacy-stable" }); await registry.reserve({ projectId: "app", instanceId: "proxy", invocationId: "proxy-starting", profile: "default", requests: [], proxyListenerPorts: [port] }); await expect(request("sibling", "blocked-tcp", "tcp")).rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT" }); await expect(request("sibling", "blocked-udp", "udp")).rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT" }); expect((await registry.reserve({ projectId: "app", instanceId: "sibling", invocationId: "reallocated", profile: "default", requests: [{ name: "api", spec: { preferred: port, protocol: "udp", range: [port, port + 1] } }] }))[0].port).toBe(port + 1); await registry.release({ invocationId: "reallocated" }); await registry.recoverInterrupted("proxy"); expect((await request("legacy", "recovered", "tcp"))[0].port).toBe(port); }), 150_000); it("keeps an interrupted or failed lifecycle's listener claim until its routes are removed", async () => await withCaddyAdminPort(async () => { const dir = await mkdtemp(path.join(tmpdir(), "devfn-interrupted-claim-")); const registry = new FilePortRegistry(path.join(dir, "registry.json"), undefined, async () => true); const port = await allocateEphemeralPort(); const routeFile = path.join(dir, "proxy-routes.json"); const sibling = (id: string) => registry.reserve({ projectId: "app", instanceId: "sibling", invocationId: id, profile: "default", requests: [{ name: "api", spec: { preferred: port, exact: true, protocol: "udp" } }] }); try { // An activation committed its route, then its command was interrupted. await registry.reserve({ projectId: "app", instanceId: "proxy", invocationId: "interrupted", profile: "default", requests: [], proxyListenerPorts: [port] }); await writeFile(routeFile, JSON.stringify({ version: 1, routes: [{ id: "proxy:app", instanceId: "proxy", hostname: "app.localhost", targetHost: "127.0.0.1", targetPort: port + 1, tls: "off", updatedAt: new Date().toISOString() }] })); await expect(registry.recoverInterrupted("proxy")).resolves.toBe(1); expect((await registry.read()).invocations[0]).toMatchObject({ state: "failed", errorCode: "DEVFN_INTERRUPTED", proxyClaimRetained: true }); await expect(sibling("after-recovery")).rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT", details: { instanceId: "proxy" } }); // The next lifecycle fails before it reconciles those routes. await registry.reserve({ projectId: "app", instanceId: "proxy", invocationId: "unprepared", profile: "default", requests: [] }); await registry.release({ invocationId: "unprepared", errorCode: "DEVFN_ENDPOINT_RESOLUTION_FAILED" }); await registry.gc(); await expect(sibling("after-preparation-failure")).rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT", details: { instanceId: "proxy" } }); // A claiming lifecycle that fails while its routes remain keeps its claim too. await registry.reserve({ projectId: "app", instanceId: "proxy", invocationId: "rollback", profile: "default", requests: [], proxyListenerPorts: [port] }); await registry.release({ invocationId: "rollback", errorCode: "DEVFN_PROXY_RELOAD_FAILED" }); expect((await registry.read()).invocations.find((item) => item.id === "rollback")).toMatchObject({ state: "failed", proxyClaimRetained: true }); await expect(registry.reserve({ projectId: "app", instanceId: "proxy", invocationId: "own-exact", profile: "default", requests: [{ name: "api", spec: { preferred: port, exact: true } }] })).rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT", details: { instanceId: "proxy", action: expect.stringContaining("devfn down") } }); await rm(routeFile); expect((await sibling("after-routes-removed"))[0]).toMatchObject({ port, protocol: "udp" }); expect((await registry.read()).invocations.filter((item) => item.proxyClaimRetained)).toEqual([]); } finally { await rm(dir, { recursive: true, force: true }); } }), 150_000); it("serializes a proxy claim against a concurrent sibling listener reservation", async () => { const dir = tempDir("devfn-registry-proxy-race-"); const file = path.join(dir, "registry.json"); const port = 18444; const proxy = new FilePortRegistry(file, async () => port + 1, async () => true); const sibling = new FilePortRegistry(file, async () => port + 1, async () => true); const results = await Promise.allSettled([ proxy.reserve({ projectId: "app", instanceId: "proxy", invocationId: "proxy", profile: "default", requests: [], proxyListenerPorts: [port] }), sibling.reserve({ projectId: "app", instanceId: "sibling", invocationId: "sibling", profile: "default", requests: [{ name: "udp", spec: { preferred: port, exact: true, protocol: "udp" } }] }), ]); expect(results.filter((result) => result.status === "fulfilled")).toHaveLength(1); expect(results.filter((result) => result.status === "rejected")[0]).toMatchObject({ reason: { code: "DEVFN_PORT_CONFLICT" } }); const state = await proxy.read(); expect(state.invocations).toHaveLength(1); expect(state.allocations.some((allocation) => allocation.port === port)).toBe(state.invocations[0].instanceId === "sibling"); }); it("gives concurrent worktrees distinct deterministic allocations", async () => { const dir = tempDir("devfn-registry-"); const registry = new FilePortRegistry(path.join(dir, "registry.json")); const reserve = (instanceId: string, invocationId: string) => registry.reserve({ projectId: "app", instanceId, invocationId, profile: "default", requests: [{ name: "app", spec: { range: [43000, 43100] } }] }); const [first, second] = await Promise.all([reserve("worktree-a", "one"), reserve("worktree-b", "two")]); expect(first[0].port).not.toBe(second[0].port); expect((await registry.read()).revision).toBe(2); const freshDir = tempDir("devfn-registry-"); const repeated = await new FilePortRegistry(path.join(freshDir, "registry.json")).reserve({ projectId: "app", instanceId: "worktree-a", invocationId: "repeat", profile: "default", requests: [{ name: "app", spec: { range: [43000, 43100] } }] }); expect(repeated[0].port).toBe(first[0].port); }); it("fails closed when an exact reservation is already leased", async () => { const dir = tempDir("devfn-registry-"); const registry = new FilePortRegistry(path.join(dir, "registry.json")); const exactPort = await allocateEphemeralPort(); const input = { projectId: "app", profile: "default", requests: [{ name: "oauth", spec: { preferred: exactPort, exact: true } }] } as const; await registry.reserve({ ...input, instanceId: "a", invocationId: "one" }); await expect(registry.reserve({ ...input, instanceId: "b", invocationId: "two" })).rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT" }); }); it("tracks TCP and UDP leases independently", async () => { let exactPort = 0; let udp: Awaited> | undefined; for (let attempt = 0; attempt < 20 && !udp; attempt += 1) { const dir = tempDir("devfn-registry-"); const registry = new FilePortRegistry(path.join(dir, "registry.json")); exactPort = await allocateEphemeralPort(); try { await registry.reserve({ projectId: "app", instanceId: "tcp", invocationId: `tcp-${attempt}`, profile: "default", requests: [{ name: "tcp", spec: { preferred: exactPort, exact: true, protocol: "tcp" } }] }); udp = await registry.reserve({ projectId: "app", instanceId: "udp", invocationId: `udp-${attempt}`, profile: "default", requests: [{ name: "udp", spec: { preferred: exactPort, exact: true, protocol: "udp" } }] }); } catch (error) { if ((error as { code?: string }).code !== "DEVFN_PORT_CONFLICT") throw error; // Retry only when another process claims either protocol between probes. } } expect(udp?.[0]).toMatchObject({ port: exactPort, protocol: "udp" }); }); it("applies project policy ranges and renders them for humans", () => { const policy = { version: 1 as const, ports: [{ name: "app-range", range: [45000, 45099] as [number, number], kind: "preferred" as const, project: "app" }] }; expect(resolvePolicy(policy, "app").preferredRange).toEqual([45000, 45099]); expect(renderPolicyInventory(policy)).toContain("45000-45099"); }); it("preserves contiguous exact blocks", async () => { const dir = tempDir("devfn-registry-"); const registry = new FilePortRegistry(path.join(dir, "registry.json")); let firstPort = 0; let allocations: Awaited> | undefined; for (let attempt = 0; attempt < 20 && !allocations; attempt += 1) { firstPort = await allocateEphemeralPort(); if (firstPort >= 65535 || !await isPortAvailable(firstPort) || !await isPortAvailable(firstPort + 1)) continue; allocations = await registry.reserve({ projectId: "oauth", instanceId: "one", invocationId: `block-${attempt}`, profile: "oauth", requests: [ { name: "callback", spec: { preferred: firstPort, exact: true, block: "oauth" } }, { name: "issuer", spec: { preferred: firstPort + 1, exact: true, block: "oauth" } }, ], }).catch(() => undefined); } expect(allocations).toBeDefined(); expect(allocations!.map((item) => item.port)).toEqual([firstPort, firstPort + 1]); expect(allocations!.every((item) => item.source === "exact")).toBe(true); }); it("honors contiguous preferred ports for reallocatable blocks", async () => { const dir = tempDir("devfn-registry-"); const registry = new FilePortRegistry(path.join(dir, "registry.json"), undefined, async () => true); const firstPort = 45_670; const allocations = await registry.reserve({ projectId: "oauth", instanceId: "preferred", invocationId: "preferred-block", profile: "default", requests: [ { name: "callback", spec: { preferred: firstPort, block: "oauth" } }, { name: "issuer", spec: { preferred: firstPort + 1, block: "oauth" } }, ], }); expect(allocations.map((item) => item.port)).toEqual([firstPort, firstPort + 1]); expect(allocations.every((item) => item.source === "preferred")).toBe(true); }); it("rejects blocks whose assigned ports cannot satisfy every member range", async () => { const dir = tempDir("devfn-registry-"); const registry = new FilePortRegistry(path.join(dir, "registry.json"), undefined, async () => true); await expect(registry.reserve({ projectId: "oauth", instanceId: "incompatible", invocationId: "incompatible-block", profile: "default", requests: [ { name: "callback", spec: { range: [5000, 5001], block: "oauth" } }, { name: "issuer", spec: { range: [6000, 6001], block: "oauth" } }, ], })).rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT" }); }); it("skips registry-leased ports returned by the OS ephemeral allocator", async () => { const dir = tempDir("devfn-registry-"); const leased = await allocateEphemeralPort(); let replacement = await allocateEphemeralPort(); while (replacement === leased) replacement = await allocateEphemeralPort(); const candidates = [leased, replacement]; const registry = new FilePortRegistry(path.join(dir, "registry.json"), async () => candidates.shift()!); await registry.reserve({ projectId: "app", instanceId: "one", invocationId: "exact", profile: "default", requests: [{ name: "exact", spec: { preferred: leased, exact: true } }] }); const ephemeral = await registry.reserve({ projectId: "app", instanceId: "two", invocationId: "ephemeral", profile: "default", requests: [{ name: "ephemeral", spec: { ephemeral: true } }] }); expect(ephemeral[0]).toMatchObject({ port: replacement, source: "ephemeral" }); }); it("releases planned leases left by an interrupted instance", async () => { const dir = tempDir("devfn-registry-"); const registry = new FilePortRegistry(path.join(dir, "registry.json")); await registry.reserve({ projectId: "app", instanceId: "one", invocationId: "interrupted", profile: "default", requests: [{ name: "app", spec: { range: [45600, 45699] } }] }); await expect(registry.recoverInterrupted("one")).resolves.toBe(1); const state = await registry.read(); expect(state.allocations[0].state).toBe("released"); expect(state.invocations[0]).toMatchObject({ state: "failed", errorCode: "DEVFN_INTERRUPTED" }); }); it("fails interrupted planning invocations that reserved no ports", async () => { const dir = tempDir("devfn-registry-"); const registry = new FilePortRegistry(path.join(dir, "registry.json")); await registry.reserve({ projectId: "app", instanceId: "one", invocationId: "empty", profile: "default", requests: [] }); await expect(registry.recoverInterrupted("one")).resolves.toBe(1); expect((await registry.read()).invocations[0]).toMatchObject({ state: "failed", errorCode: "DEVFN_INTERRUPTED" }); }); it("recovers ownerless stale lock directories", async () => { const dir = tempDir("devfn-lock-"); const lockPath = path.join(dir, "registry.lock"); await mkdir(lockPath); // Only past the bound every release waits for a creator to record itself. await expect(withFileLock(lockPath, async () => "acquired", { staleMs: 1, timeoutMs: 300 })).rejects.toMatchObject({ code: "DEVFN_REGISTRY_LOCK_TIMEOUT" }); await utimes(lockPath, new Date(Date.now() - 301_000), new Date(Date.now() - 301_000)); await expect(withFileLock(lockPath, async () => "acquired", { staleMs: 1, timeoutMs: 1000 })).resolves.toBe("acquired"); }); it("loses no reservation when concurrent commands recover the locks of a holder killed while holding them", async () => { const dir = await mkdtemp(path.join(tmpdir(), "devfn-registry-")); const registry = new FilePortRegistry(path.join(dir, "registry.json")); const holder = spawn(process.execPath, ["-e", "setInterval(() => undefined, 1000)"], { stdio: "ignore" }); try { await new Promise((resolve) => holder.once("spawn", () => resolve())); const owner = { pid: holder.pid, birthSignature: await processBirthSignature(holder.pid!), createdAt: new Date(Date.now() - 3_600_000).toISOString() }; for (const lock of ["routing.lock", "registry.json.lock"]) { await mkdir(path.join(dir, lock)); await writeFile(path.join(dir, lock, "owner.json"), JSON.stringify({ ...owner, token: lock })); } holder.kill("SIGKILL"); await new Promise((resolve) => holder.once("exit", () => resolve())); const instances = Array.from({ length: 6 }, (_, index) => `worktree-${index}`); await Promise.all(instances.map(async (instanceId) => await registry.reserve({ projectId: "app", instanceId, invocationId: instanceId, profile: "default", requests: [{ name: "app", spec: { range: [43000, 43100] } }] }))); const state = await registry.read(); expect(state.invocations.map((invocation) => invocation.instanceId).sort()).toEqual(instances); expect(new Set(state.allocations.map((allocation) => allocation.port)).size).toBe(instances.length); expect(state.revision).toBe(instances.length); } finally { holder.kill("SIGKILL"); await rm(dir, { recursive: true, force: true }); } }); it.skipIf(process.platform !== "darwin")("recovers a stale legacy lock whose PID now belongs to a process started after it", async () => { const dir = await mkdtemp(path.join(tmpdir(), "devfn-lock-")); const unrelated = spawn("sleep", ["30"], { detached: true, stdio: "ignore" }); const saved = process.env.TZ; try { await new Promise((resolve) => unrelated.once("spawn", () => resolve())); const lstart = async (TZ: string) => (await promisify(execFile)("ps", ["-o", "lstart=", "-p", String(unrelated.pid)], { env: { ...process.env, TZ, LC_ALL: "C" } })).stdout.trim(); const born = Date.parse(`${await lstart("UTC0")} UTC`); const birthSignature = `darwin:${await lstart("UTC+1")}`; process.env.TZ = "UTC+1"; // Its legacy text equals this caller's rendering, but the record predates the process. const reusedLock = path.join(dir, "reused.lock"); await mkdir(reusedLock); await writeFile(path.join(reusedLock, "owner.json"), JSON.stringify({ token: "old", pid: unrelated.pid, birthSignature, createdAt: new Date(born - 3_600_000 + 5_000).toISOString() })); await expect(withFileLock(reusedLock, async () => "acquired", { staleMs: 1, timeoutMs: 2_000 })).resolves.toBe("acquired"); // An old lock held by the process that recorded it is never taken, in any time zone. process.env.TZ = "UTC"; const heldLock = path.join(dir, "held.lock"); await mkdir(heldLock); await writeFile(path.join(heldLock, "owner.json"), JSON.stringify({ token: "held", pid: unrelated.pid, birthSignature, createdAt: new Date(born + 5_000).toISOString() })); await expect(withFileLock(heldLock, async () => "acquired", { staleMs: 1, timeoutMs: 300 })).rejects.toMatchObject({ code: "DEVFN_REGISTRY_LOCK_TIMEOUT" }); } finally { if (saved === undefined) delete process.env.TZ; else process.env.TZ = saved; try { process.kill(-unrelated.pid!, "SIGKILL"); } catch { /* already stopped */ } await rm(dir, { recursive: true, force: true }); } }); it.skipIf(process.platform !== "darwin")("judges an earlier release's active lease by its activation time through reconcile and gc", async () => { const dir = await mkdtemp(path.join(tmpdir(), "devfn-legacy-lease-")); const unrelated = spawn("sleep", ["30"], { detached: true, stdio: "ignore" }); const saved = process.env.TZ; try { await new Promise((resolve) => unrelated.once("spawn", () => resolve())); const lstart = async (TZ: string) => (await promisify(execFile)("ps", ["-o", "lstart=", "-p", String(unrelated.pid)], { env: { ...process.env, TZ, LC_ALL: "C" } })).stdout.trim(); const born = Date.parse(`${await lstart("UTC0")} UTC`); // An earlier release recorded the owner in its caller's time zone, with // no record time but the lease's activation updatedAt. const birthSignature = `darwin:${await lstart("UTC+1")}`; process.env.TZ = "UTC+1"; const [reusedPort, heldPort] = [await allocateEphemeralPort(), await allocateEphemeralPort()]; const lease = (instanceId: string, port: number, activatedAt: string) => ({ id: `${instanceId}-app`, projectId: "app", instanceId, service: "app", protocol: "tcp", host: "127.0.0.1", port, invocationId: instanceId, state: "active", source: "exact", process: { pid: unrelated.pid, birthSignature }, createdAt: activatedAt, updatedAt: activatedAt }); const invocation = (id: string, at: string) => ({ id, projectId: "app", instanceId: id, profile: "default", state: "ready", createdAt: at, updatedAt: at }); // The reused lease was activated an hour before its PID's current process started. const reusedAt = new Date(born - 3_600_000).toISOString(); const heldAt = new Date(born + 5_000).toISOString(); await writeFile(path.join(dir, "registry.json"), JSON.stringify({ version: 1, revision: 1, allocations: [lease("reused", reusedPort, reusedAt), lease("held", heldPort, heldAt)], invocations: [invocation("reused", reusedAt), invocation("held", heldAt)] })); const registry = new FilePortRegistry(path.join(dir, "registry.json"), undefined, async () => true); const reconciled = await registry.reconcile(); expect(reconciled.allocations.find((item) => item.instanceId === "reused")).toMatchObject({ state: "stale" }); // The lease held by the process that recorded it stays active and keeps // its activation time as the owner's record time. expect(reconciled.allocations.find((item) => item.instanceId === "held")).toMatchObject({ state: "active", process: { recordedAt: heldAt } }); expect(await registry.instanceMayRun("reused")).toBe(false); expect(await registry.instanceMayRun("held")).toBe(true); await registry.gc(); expect((await registry.read()).allocations.map((item) => item.instanceId)).toEqual(["held"]); const sibling = (port: number) => registry.reserve({ projectId: "other", instanceId: "sibling", invocationId: `sibling-${port}`, profile: "default", requests: [{ name: "app", spec: { preferred: port, exact: true } }] }); expect((await sibling(reusedPort))[0]).toMatchObject({ port: reusedPort, source: "exact" }); await expect(sibling(heldPort)).rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT" }); } finally { if (saved === undefined) delete process.env.TZ; else process.env.TZ = saved; try { process.kill(-unrelated.pid!, "SIGKILL"); } catch { /* already stopped */ } await rm(dir, { recursive: true, force: true }); } }); it("refreshes planned allocations with the lifecycle heartbeat", async () => { const dir = tempDir("devfn-registry-"); const registry = new FilePortRegistry(path.join(dir, "registry.json")); await registry.reserve({ projectId: "app", instanceId: "one", invocationId: "starting", profile: "default", requests: [{ name: "app", spec: { range: [45100, 45199] } }] }); const before = (await registry.read()).allocations[0].updatedAt; await new Promise((resolve) => setTimeout(resolve, 5)); await registry.updateInvocation("starting", { state: "starting" }); expect(Date.parse((await registry.read()).allocations[0].updatedAt)).toBeGreaterThan(Date.parse(before)); }); it("does not refresh planned allocations after an invocation becomes terminal", async () => { const dir = tempDir("devfn-registry-"); const registry = new FilePortRegistry(path.join(dir, "registry.json")); await registry.reserve({ projectId: "app", instanceId: "one", invocationId: "failed", profile: "default", requests: [{ name: "app", spec: { range: [45200, 45299] } }] }); const before = (await registry.read()).allocations[0].updatedAt; await new Promise((resolve) => setTimeout(resolve, 5)); await registry.updateInvocation("failed", { state: "failed" }); const after = await registry.read(); expect(after.invocations[0].state).toBe("failed"); expect(after.allocations[0].updatedAt).toBe(before); }); it("counts only verified-dead recorded owners as lifecycle death evidence", async () => { const dir = await mkdtemp(path.join(tmpdir(), "devfn-owner-evidence-")); const originalPath = process.env.PATH; const dockerState = path.join(dir, "docker-state"); const age = async () => { const state = await registry.read(); for (const item of [...state.invocations, ...state.allocations]) item.updatedAt = "2020-01-01T00:00:00.000Z"; await writeFile(registry.filePath, JSON.stringify(state)); }; const registry = new FilePortRegistry(path.join(dir, "registry.json")); const server = net.createServer(); try { await writeFile(path.join(dir, "docker"), `#!/bin/sh case "$(cat "${dockerState}")" in running) echo true;; gone) echo "Error: No such object: $4" >&2; exit 1;; *) echo "Cannot connect to the Docker daemon" >&2; exit 1;; esac `, { mode: 0o700 }); process.env.PATH = `${dir}${path.delimiter}${originalPath ?? ""}`; const port = await allocateEphemeralPort(); await registry.reserve({ projectId: "app", instanceId: "one", invocationId: "start", profile: "default", requests: [{ name: "api", spec: { preferred: port, exact: true } }] }); await registry.updateInvocation("start", { state: "starting" }); await age(); // Journaled and launched nothing yet: nothing of it can run. expect(await registry.instanceMayRun("one")).toBe(false); await registry.beginLaunch("start", "api", { projectName: "devfn-app", composeService: "api", preExisting: false, existingContainerIds: [], runningContainerIds: [] }); await age(); // A launch whose identity is not recorded yet is ambiguous. expect(await registry.instanceMayRun("one")).toBe(true); await registry.recordOwners("start", "api", [{ container: { id: "fixture-container" } }]); await age(); await new Promise((resolve) => server.listen(port, "127.0.0.1", resolve)); // An expired planned lease on an occupied port is not stale. expect((await registry.reconcile()).allocations[0].state).toBe("externally-occupied"); for (const [docker, mayRun] of [["running", true], ["unreachable", true], ["gone", false]] as const) { await writeFile(dockerState, docker); expect(await registry.instanceMayRun("one")).toBe(mayRun); } await writeFile(dockerState, "running"); await registry.gc(); expect(await registry.instanceMayRun("one")).toBe(true); // An owner recording both a process and a container is dead only when both are. await registry.reserve({ projectId: "app", instanceId: "two", invocationId: "both", profile: "default", requests: [] }); await registry.updateInvocation("both", { state: "starting" }); await registry.recordOwners("both", "api", [{ process: { pid: 2_147_483_646, birthSignature: "linux:1" }, container: { id: "fixture-container" } }]); await age(); for (const [docker, mayRun] of [["running", true], ["gone", false]] as const) { await writeFile(dockerState, docker); expect(await registry.instanceMayRun("two")).toBe(mayRun); } await writeFile(dockerState, "running"); // A legacy invocation with no owner journal and no recorded owner proves nothing. const state = await registry.read(); state.invocations.push({ id: "legacy", projectId: "app", instanceId: "legacy", profile: "default", state: "ready", createdAt: "2020-01-01T00:00:00.000Z", updatedAt: "2020-01-01T00:00:00.000Z" }); await writeFile(registry.filePath, JSON.stringify(state)); expect(await registry.instanceMayRun("legacy")).toBe(true); } finally { if (server.listening) await new Promise((resolve) => server.close(() => resolve())); if (originalPath === undefined) delete process.env.PATH; else process.env.PATH = originalPath; await rm(dir, { recursive: true, force: true }); } }); it("counts a lease as serving its port only while its owner is verified running", async () => { const dir = await mkdtemp(path.join(tmpdir(), "devfn-serving-owner-")); const dockerState = path.join(dir, "docker-state"); const originalPath = process.env.PATH; const registry = new FilePortRegistry(path.join(dir, "registry.json"), undefined, async () => true); try { await writeFile(path.join(dir, "docker"), `#!/bin/sh case "$(cat "${dockerState}")" in running) echo true;; gone) echo "Error: No such object: $4" >&2; exit 1;; *) echo "Cannot connect to the Docker daemon" >&2; exit 1;; esac `, { mode: 0o700 }); process.env.PATH = `${dir}${path.delimiter}${originalPath ?? ""}`; const port = await allocateEphemeralPort(); await registry.reserve({ projectId: "app", instanceId: "one", invocationId: "ready", profile: "default", requests: [{ name: "api", spec: { preferred: port, exact: true } }] }); await registry.markActive("ready", { api: { container: { id: "fixture-container" } } }); // An unreachable Docker keeps the lifecycle for cleanup but is no evidence that it serves. for (const [docker, serves, mayRun] of [["running", true, true], ["unreachable", false, true], ["gone", false, false]] as const) { await writeFile(dockerState, docker); expect(await registry.instanceServesPort("one", port), docker).toBe(serves); expect(await registry.instanceMayRun("one"), docker).toBe(mayRun); } await writeFile(dockerState, "running"); expect(await registry.instanceServesPort("one", port + 1)).toBe(false); expect(await registry.instanceServesPort("two", port)).toBe(false); } finally { if (originalPath === undefined) delete process.env.PATH; else process.env.PATH = originalPath; await rm(dir, { recursive: true, force: true }); } }); it("refuses malformed serving evidence of the queried instance and never rewrites it", async () => { const dir = await mkdtemp(path.join(tmpdir(), "devfn-serving-malformed-")); const registry = new FilePortRegistry(path.join(dir, "registry.json"), undefined, async () => true); const self = { pid: process.pid, birthSignature: (await processBirthSignature(process.pid))! }; try { const first = await allocateEphemeralPort(); let second = await allocateEphemeralPort(); while (second === first) second = await allocateEphemeralPort(); const ports = { one: first, two: second }; for (const instanceId of ["one", "two"] as const) { await registry.reserve({ projectId: "app", instanceId, invocationId: instanceId, profile: "default", requests: [{ name: "api", spec: { preferred: ports[instanceId], exact: true } }] }); await registry.markActive(instanceId, { api: { process: self } }); } expect(await registry.instanceServesPort("one", ports.one)).toBe(true); const valid = JSON.parse(await readFile(registry.filePath, "utf8")) as { allocations: Array> }; const owners: Array<[string, Record, boolean | "invalid"]> = [ ["pid zero", { process: { ...self, pid: 0 } }, "invalid"], ["pid text", { process: { ...self, pid: String(self.pid) } }, "invalid"], ["empty signature", { process: { ...self, birthSignature: "" } }, "invalid"], ["no signature", { process: { pid: self.pid } }, false], ...[null, 0, false, "", "not-a-time"].map((recordedAt): [string, Record, "invalid"] => [`record time ${String(recordedAt)}`, { process: { ...self, recordedAt } }, "invalid"]), ["legacy owner without record time", { process: self }, true], ["valid record time", { process: { ...self, recordedAt: new Date().toISOString() } }, true], ["future record time", { process: { ...self, recordedAt: new Date(Date.now() + 3_600_000).toISOString() } }, false], ["legacy owner activated in the future", { process: self, updatedAt: new Date(Date.now() + 3_600_000).toISOString() }, false], ["lease without id", { process: self, id: "" }, "invalid"], ["empty container", { container: { id: "" } }, "invalid"], ["container environment", { container: { id: "fixture", dockerEnvironment: { DOCKER_HOST: 1 } } }, "invalid"], ]; for (const [label, owner, outcome] of owners) { const saved = structuredClone(valid); const lease = saved.allocations.find((item) => item.instanceId === "one")!; delete lease.process; Object.assign(lease, owner); const bytes = JSON.stringify(saved); await writeFile(registry.filePath, bytes); if (outcome === "invalid") await expect(registry.instanceServesPort("one", ports.one), label).rejects.toMatchObject({ code: "DEVFN_REGISTRY_INVALID" }); else expect(await registry.instanceServesPort("one", ports.one), label).toBe(outcome); expect(await registry.instanceServesPort("two", ports.two), label).toBe(true); expect(await readFile(registry.filePath, "utf8"), label).toBe(bytes); } } finally { await rm(dir, { recursive: true, force: true }); } }); it("counts only a settled ready lifecycle's own lease whose process is verified running as serving", async () => { const dir = await mkdtemp(path.join(tmpdir(), "devfn-serving-lifecycle-")); const registry = new FilePortRegistry(path.join(dir, "registry.json"), undefined, async () => true); const birthSignature = (await processBirthSignature(process.pid))!; const child = spawn(process.execPath, ["-e", "setTimeout(() => {}, 60000)"], { stdio: "ignore" }); try { const exited = { pid: child.pid!, birthSignature: (await processBirthSignature(child.pid!))! }; const childExit = new Promise((resolve) => child.once("exit", resolve)); child.kill("SIGKILL"); await childExit; const port = await allocateEphemeralPort(); await registry.reserve({ projectId: "app", instanceId: "one", invocationId: "one", profile: "default", requests: [{ name: "api", spec: { preferred: port, exact: true } }] }); await registry.markActive("one", { api: { process: { pid: process.pid, birthSignature } } }); expect(await registry.instanceServesPort("one", port)).toBe(true); type Saved = { invocations: Array>; allocations: Array> }; const valid = JSON.parse(await readFile(registry.filePath, "utf8")) as Saved; const cases: Array<[string, (invocation: Record, lease: Record, saved: Saved) => void, boolean | "invalid"]> = [ ["stopping lifecycle", (invocation) => { invocation.state = "stopping"; }, false], ["legacy invocation without owner journal", (invocation) => { delete invocation.ownerJournal; }, false], ["unresolved launch", (invocation) => { invocation.launching = ["api"]; }, false], ["unresolved compose launch", (invocation) => { invocation.composeLaunches = { api: {} }; }, false], ["malformed compose launches", (invocation) => { invocation.composeLaunches = ["api"]; }, "invalid"], ["malformed lifecycle state", (invocation) => { invocation.state = "serving"; }, "invalid"], ["lease recorded for another lifecycle", (_, lease) => { lease.instanceId = "other"; }, false], ["lease recorded for another project", (_, lease) => { lease.projectId = "other"; }, false], ["absent lifecycle ids", (invocation, lease) => { delete invocation.id; delete lease.invocationId; }, "invalid"], ["absent project ids", (invocation, lease) => { delete invocation.projectId; delete lease.projectId; }, "invalid"], ["lease without project id", (_, lease) => { delete lease.projectId; }, "invalid"], ["lease without lifecycle id", (_, lease) => { delete lease.invocationId; }, "invalid"], ["malformed lease port", (_, lease) => { lease.port = String(port); }, "invalid"], ["null invocation entry", (_, __, saved) => { saved.invocations.push(null as never); }, "invalid"], ["null lease entry", (_, __, saved) => { saved.allocations.push(null as never); }, "invalid"], ["malformed owner of another lifecycle's lease", (_, lease, saved) => { saved.allocations.push({ ...lease, id: "other", invocationId: "other", instanceId: "other", process: "invalid" }); }, true], ["released lease", (_, lease) => { lease.state = "released"; }, false], ["udp lease", (_, lease) => { lease.protocol = "udp"; }, false], ["reused pid", (_, lease) => { lease.process = { pid: process.pid, birthSignature: `${birthSignature.split(":")[0]}:reused-pid` }; }, false], ["unverifiable signature", (_, lease) => { lease.process = { pid: process.pid, birthSignature: "fixture-format:unverifiable" }; }, false], ["exited owner", (_, lease) => { lease.process = exited; }, false], ["malformed owner journal", (invocation) => { invocation.ownerJournal = "yes"; }, "invalid"], ["malformed launching", (invocation) => { invocation.launching = [""]; }, "invalid"], ]; for (const [label, mutate, outcome] of cases) { const saved = structuredClone(valid); mutate(saved.invocations[0]!, saved.allocations[0]!, saved); const bytes = JSON.stringify(saved); await writeFile(registry.filePath, bytes); if (outcome === "invalid") await expect(registry.instanceServesPort("one", port), label).rejects.toMatchObject({ code: "DEVFN_REGISTRY_INVALID" }); else expect(await registry.instanceServesPort("one", port), label).toBe(outcome); expect(await readFile(registry.filePath, "utf8"), label).toBe(bytes); } } finally { child.kill("SIGKILL"); await rm(dir, { recursive: true, force: true }); } }); it.skipIf(process.platform !== "darwin")("keeps a live recorded owner whose birth signature cannot be read as possibly running", async () => { const dir = await mkdtemp(path.join(tmpdir(), "devfn-unverified-owner-")); const originalPath = process.env.PATH; const registry = new FilePortRegistry(path.join(dir, "registry.json")); try { const birthSignature = await processBirthSignature(process.pid); expect(birthSignature).toBeTruthy(); await registry.reserve({ projectId: "app", instanceId: "one", invocationId: "ready", profile: "default", requests: [{ name: "api", spec: { range: [45300, 45399] } }] }); await registry.markActive("ready", { api: { process: { pid: process.pid, birthSignature: birthSignature! } } }); // macOS reads birth signatures through ps; a failing ps leaves the live PID unverifiable. await writeFile(path.join(dir, "ps"), "#!/bin/sh\nexit 1\n", { mode: 0o700 }); process.env.PATH = `${dir}${path.delimiter}${originalPath ?? ""}`; expect(await registry.instanceMayRun("one")).toBe(true); expect((await registry.reconcile()).allocations[0].state).toBe("active"); } finally { if (originalPath === undefined) delete process.env.PATH; else process.env.PATH = originalPath; await rm(dir, { recursive: true, force: true }); } }); it("keeps counting a failed lifecycle's recorded owners through gc until they are verified dead", async () => { const dir = await mkdtemp(path.join(tmpdir(), "devfn-failed-owner-")); const registry = new FilePortRegistry(path.join(dir, "registry.json")); const child = spawn(process.execPath, ["-e", "setInterval(() => undefined, 1000)"], { stdio: "ignore" }); const exited = new Promise((resolve) => child.once("exit", resolve)); try { let birthSignature: string | undefined; for (let attempt = 0; attempt < 50 && !birthSignature; attempt += 1) { birthSignature = await processBirthSignature(child.pid!); if (!birthSignature) await new Promise((resolve) => setTimeout(resolve, 20)); } expect(birthSignature).toBeTruthy(); // The start recorded its node's identity, then stopped before its receipt listed it. await registry.reserve({ projectId: "app", instanceId: "one", invocationId: "interrupted", profile: "default", requests: [] }); await registry.updateInvocation("interrupted", { state: "starting" }); await registry.beginLaunch("interrupted", "api", { projectName: "devfn-app", composeService: "api", preExisting: false, existingContainerIds: [], runningContainerIds: [] }); await registry.recordOwners("interrupted", "api", [{ process: { pid: child.pid!, birthSignature: birthSignature! } }]); expect(await registry.recoverInterrupted("one")).toBe(1); await registry.gc(); expect((await registry.read()).invocations.map((item) => [item.id, item.state])).toEqual([["interrupted", "failed"]]); expect(await registry.instanceMayRun("one")).toBe(true); // A handled failure resolved its own launches before it ended. await registry.reserve({ projectId: "app", instanceId: "two", invocationId: "handled", profile: "default", requests: [] }); await registry.beginLaunch("handled", "api", { projectName: "devfn-app", composeService: "api", preExisting: false, existingContainerIds: [], runningContainerIds: [] }); await registry.release({ invocationId: "handled", errorCode: "DEVFN_START_FAILED" }); expect(await registry.instanceMayRun("two")).toBe(false); child.kill("SIGKILL"); await exited; expect(await registry.instanceMayRun("one")).toBe(false); await registry.gc(); expect((await registry.read()).invocations).toEqual([]); } finally { child.kill("SIGKILL"); await rm(dir, { recursive: true, force: true }); } }); it("keeps the last listener claim through a route-free release while a listener remains on a claimed port", async ({ skip }) => await withCaddyAdminPort(async () => { // Retirement needs conclusive listener evidence, which a busy Caddy admin port denies. if (!await isPortAvailable(2019, "tcp", "127.0.0.1")) skip("127.0.0.1:2019 is in use, so listener absence cannot be proven on this host."); const dir = await mkdtemp(path.join(tmpdir(), "devfn-release-listener-")); const registry = new FilePortRegistry(path.join(dir, "registry.json")); const port = await allocateEphemeralPort(); const listener = dgram.createSocket("udp4"); try { // A Caddy whose exit was never confirmed may still hold its HTTP/3 socket. await new Promise((resolve, reject) => { listener.once("error", reject); listener.bind(port, "127.0.0.1", () => resolve()); }); await registry.reserve({ projectId: "app", instanceId: "proxy", invocationId: "start", profile: "default", requests: [], proxyListenerPorts: [port] }); await registry.release({ invocationId: "start", errorCode: "DEVFN_PROXY_RELOAD_FAILED" }); expect((await registry.read()).invocations[0]).toMatchObject({ state: "failed", proxyClaimRetained: true }); const sibling = async (invocationId: string) => await registry.reserve({ projectId: "app", instanceId: "sibling", invocationId, profile: "default", requests: [{ name: "api", spec: { preferred: port, exact: true } }] }); await expect(sibling("blocked")).rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT", details: { instanceId: "proxy" } }); await new Promise((resolve) => listener.close(() => resolve())); expect((await sibling("accepted"))[0].port).toBe(port); } finally { await new Promise((resolve) => { try { listener.close(() => resolve()); } catch { resolve(); } }); await rm(dir, { recursive: true, force: true }); } })); it("retires a verified-dead lifecycle's listener claim although a foreign process took its leased port", async ({ skip }) => await withCaddyAdminPort(async () => { if (!await isPortAvailable(2019, "tcp", "127.0.0.1")) skip("127.0.0.1:2019 is in use, so listener absence cannot be proven on this host."); const dir = await mkdtemp(path.join(tmpdir(), "devfn-dead-claim-foreign-")); const registry = new FilePortRegistry(path.join(dir, "registry.json")); const foreign = net.createServer(); try { const child = spawn(process.execPath, ["-e", "setTimeout(() => undefined, 200)"], { stdio: "ignore" }); // Listen before the lookup, which can outlast the child. const exited = new Promise((resolve) => child.once("exit", resolve)); const birthSignature = await processBirthSignature(child.pid!); await exited; const appPort = await allocateEphemeralPort(); let listenerPort = await allocateEphemeralPort(); while (listenerPort === appPort) listenerPort = await allocateEphemeralPort(); await registry.reserve({ projectId: "app", instanceId: "dead", invocationId: "dead", profile: "default", requests: [{ name: "api", spec: { preferred: appPort, exact: true } }], proxyListenerPorts: [listenerPort] }); await registry.markActive("dead", { api: { process: { pid: child.pid!, birthSignature: birthSignature ?? "gone" } } }); const state = await registry.read(); for (const item of [...state.invocations, ...state.allocations]) item.updatedAt = "2020-01-01T00:00:00.000Z"; await writeFile(registry.filePath, JSON.stringify(state)); await new Promise((resolve) => foreign.listen(appPort, "127.0.0.1", resolve)); const reconciled = await registry.reconcile(); expect(reconciled.allocations[0].state).toBe("externally-occupied"); expect(reconciled.invocations[0]).toMatchObject({ state: "failed", errorCode: "DEVFN_INTERRUPTED" }); const sibling = await registry.reserve({ projectId: "app", instanceId: "sibling", invocationId: "sibling", profile: "default", requests: [{ name: "api", spec: { preferred: listenerPort, exact: true } }] }); expect(sibling[0].port).toBe(listenerPort); } finally { if (foreign.listening) await new Promise((resolve) => foreign.close(() => resolve())); await rm(dir, { recursive: true, force: true }); } })); it("inspects container owners through their persisted Docker selector", async () => { let observedHost: string | undefined; let observedArgs: string[] = []; await expect(inspectContainerRunning({ id: "remote-id", dockerEnvironment: { DOCKER_HOST: "tcp://remote.example:2376" } }, async (_file, args, options) => { observedArgs = args; observedHost = options.env.DOCKER_HOST; return { stdout: "true\n" }; })).resolves.toBe(true); expect(observedHost).toBe("tcp://remote.example:2376"); expect(observedArgs).toEqual(["inspect", "--format", "{{.State.Running}}", "remote-id"]); }); it("distinguishes a missing container from an unavailable Docker endpoint", async () => { await expect(inspectContainerRunning({ id: "missing" }, async () => { throw Object.assign(new Error("inspect failed"), { stderr: "Error: No such object: missing" }); })).resolves.toBe(false); await expect(inspectContainerRunning({ id: "remote" }, async () => { throw Object.assign(new Error("inspect failed"), { stderr: "Cannot connect to the Docker daemon" }); })).resolves.toBeUndefined(); }); it("escapes backslashes, pipes, and carriage returns in policy tables", () => { const output = renderPolicyInventory({ version: 1, ports: [{ name: "a\\|b\rc", kind: "protected", port: 4100 }] }); expect(output).toContain("a\\\\\\|b c"); }); }); // fetch() and browsers refuse a fetch-blocked port before connecting, so a // lease there fails every HTTP readiness check; 4190 is in the fallback range. describe("fetch-blocked ports", () => { const withRegistry = async (action: (registry: FilePortRegistry) => Promise, ephemeral?: () => Promise): Promise => { const dir = await mkdtemp(path.join(tmpdir(), "devfn-blocked-port-")); // Every candidate binds, so only the allocator's own choice decides. try { return await action(new FilePortRegistry(path.join(dir, "registry.json"), ephemeral, async () => true)); } finally { await rm(dir, { recursive: true, force: true }); } }; const reserve = async (registry: FilePortRegistry, instanceId: string, invocationId: string, requests: Array<{ name: string; spec: Record }>, ranges: { fallbackRange?: [number, number]; preferredRange?: [number, number] } = {}) => Object.fromEntries((await registry.reserve({ projectId: "app", instanceId, invocationId, profile: "default", requests, ...ranges })).map((item) => [item.service, item.port])); it("agrees with this runtime's fetch on every port it names and across the default fallback range", async () => { // fetch() checks the port before it dispatches; this dispatcher fails every // request it receives, so no probe ever opens a connection. let dispatched = 0; const dispatcher = { dispatch() { dispatched += 1; throw new Error("not connecting"); } }; const refuses = async (port: number) => await fetch(`http://127.0.0.1:${port}/`, { dispatcher } as RequestInit) .then(() => false, (error: Error) => (error.cause as Error | undefined)?.message === "bad port"); // One request proves the runtime honours the dispatcher before any other port is probed. expect(await refuses(4191)).toBe(false); expect(dispatched).toBe(1); const named = Array.from({ length: 65_535 }, (_, index) => index + 1).filter(isFetchBlockedPort); for (const port of named) expect({ port, refused: await refuses(port) }).toEqual({ port, refused: true }); const fallback = Array.from({ length: 900 }, (_, index) => 4100 + index); const refused: number[] = []; for (const port of fallback) if (await refuses(port)) refused.push(port); expect(refused).toEqual([4190]); expect(fallback.filter(isFetchBlockedPort)).toEqual(refused); }); it("never chooses one from a range, the policy range, the fallback range, a block or the OS", async () => { for (let index = 0; index < 8; index += 1) { const instanceId = `instance-${index}`; // A range or policy range of only the blocked port falls through to the fallback range. await withRegistry(async (registry) => expect(await reserve(registry, instanceId, "ranged", [{ name: "ranged", spec: { range: [4190, 4190] } }], { fallbackRange: [4191, 4191] })).toEqual({ ranged: 4191 })); await withRegistry(async (registry) => expect(await reserve(registry, instanceId, "policy", [{ name: "policy", spec: {} }], { preferredRange: [4190, 4190], fallbackRange: [4191, 4191] })).toEqual({ policy: 4191 })); // Without the exclusion the seeded offset starts some of these instances at 4190. await withRegistry(async (registry) => expect(await reserve(registry, instanceId, "fallback", [{ name: "fallback", spec: {} }], { fallbackRange: [4190, 4191] })).toEqual({ fallback: 4191 })); await withRegistry(async (registry) => expect(await reserve(registry, instanceId, "block", [{ name: "first", spec: { block: "pair" } }, { name: "second", spec: { block: "pair" } }], { fallbackRange: [4189, 4192] })).toEqual({ first: 4191, second: 4192 })); } const offered = [4190, 4191]; await withRegistry(async (registry) => expect(await reserve(registry, "os", "ephemeral", [{ name: "ephemeral", spec: { ephemeral: true } }])).toEqual({ ephemeral: 4191 }), async () => offered.shift()!); await withRegistry(async (registry) => await expect(reserve(registry, "only", "none", [{ name: "only", spec: {} }], { fallbackRange: [4190, 4190] })) .rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT" })); }, 120_000); it("keeps one configured exactly or as preferred, and renews a lease on one only as its preferred port", async () => { await withRegistry(async (registry) => { expect(await reserve(registry, "exact", "exact", [{ name: "web", spec: { preferred: 4190, exact: true } }])).toEqual({ web: 4190 }); await registry.release({ invocationId: "exact" }); expect(await reserve(registry, "preferred", "preferred", [{ name: "web", spec: { preferred: 4190 } }])).toEqual({ web: 4190 }); await registry.release({ invocationId: "preferred" }); expect(await reserve(registry, "preferred", "renewed", [{ name: "web", spec: { preferred: 4190 } }], { fallbackRange: [4191, 4191] })).toEqual({ web: 4190 }); // Once 4190 is no longer preferred, the earlier lease on it is not renewed, as for one an older DevFn chose from the fallback range. await registry.release({ invocationId: "renewed" }); expect(await reserve(registry, "preferred", "moved", [{ name: "web", spec: {} }], { fallbackRange: [4190, 4191] })).toEqual({ web: 4191 }); }); }); it("keeps a block member's own preferred port while still skipping one chosen for an unpreferred member", async () => { // Not every member prefers a port, so the block is walked from a range; 4190 is the start the walk offers. const partly = [{ name: "first", spec: { block: "pair", preferred: 4190 } }, { name: "second", spec: { block: "pair" } }]; await withRegistry(async (registry) => expect(await reserve(registry, "partly", "partly", partly, { fallbackRange: [4190, 4191] })).toEqual({ first: 4190, second: 4191 })); // The same walk puts 4190 on a member that did not prefer it, so it is still refused. const reversed = [{ name: "first", spec: { block: "pair" } }, { name: "second", spec: { block: "pair", preferred: 4190 } }]; await withRegistry(async (registry) => await expect(reserve(registry, "partly", "reversed", reversed, { fallbackRange: [4190, 4191] })) .rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT" })); // A member preferring a different port gets no exemption for 4190 either. const other = [{ name: "first", spec: { block: "pair", preferred: 4191 } }, { name: "second", spec: { block: "pair" } }]; await withRegistry(async (registry) => expect(await reserve(registry, "partly", "other", other, { fallbackRange: [4189, 4192] })).toEqual({ first: 4191, second: 4192 })); // UDP members were never excluded and are unchanged. const udp = [{ name: "first", spec: { block: "pair", protocol: "udp", preferred: 4190 } }, { name: "second", spec: { block: "pair", protocol: "udp" } }]; await withRegistry(async (registry) => expect(await reserve(registry, "partly", "udp", udp, { fallbackRange: [4189, 4190] })).toEqual({ first: 4189, second: 4190 })); }); it("is a TCP rule: a UDP port is chosen from any range, block or the OS and renewed as before", async () => { const udp = { protocol: "udp" }; // 5060 and 4190 are fetch-blocked; fetch never speaks UDP, so neither is excluded for it. await withRegistry(async (registry) => expect(await reserve(registry, "udp", "ranged", [{ name: "sip", spec: { ...udp, range: [5060, 5060] } }], { fallbackRange: [5062, 5062] })).toEqual({ sip: 5060 })); await withRegistry(async (registry) => { expect(await reserve(registry, "udp", "fallback", [{ name: "game", spec: udp }], { fallbackRange: [4190, 4190] })).toEqual({ game: 4190 }); await registry.release({ invocationId: "fallback" }); expect(await reserve(registry, "udp", "renewed", [{ name: "game", spec: udp }], { fallbackRange: [4190, 4191] })).toEqual({ game: 4190 }); }); await withRegistry(async (registry) => expect(await reserve(registry, "udp", "ephemeral", [{ name: "game", spec: { ...udp, ephemeral: true } }])).toEqual({ game: 4190 }), async () => 4190); // A mixed block excludes a blocked port only for its TCP member. await withRegistry(async (registry) => expect(await reserve(registry, "udp", "block", [{ name: "media", spec: { ...udp, block: "pair" } }, { name: "control", spec: { block: "pair" } }], { fallbackRange: [4190, 4191] })).toEqual({ media: 4190, control: 4191 })); await withRegistry(async (registry) => await expect(reserve(registry, "udp", "reversed", [{ name: "control", spec: { block: "pair" } }, { name: "media", spec: { ...udp, block: "pair" } }], { fallbackRange: [4190, 4191] })).rejects.toMatchObject({ code: "DEVFN_PORT_CONFLICT" })); }); });