Skip to content

The open-source app everyone uses to manage agents at work

fix(server): stop libc detection from reverse-resolving every socket (#16333)

Devin Foley8ffc5748a28b
diff --git a/server/src/__tests__/agent-runner-platform.test.ts b/server/src/__tests__/agent-runner-platform.test.ts
new file mode 100644
index 0000000000..c309d79ddc
--- /dev/null
+++ b/server/src/__tests__/agent-runner-platform.test.ts
@@ -0,0 +1,61 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { localAgentRunnerTarget, resetLocalAgentRunnerTargetCacheForTests } from "../lib/agent-runner-platform.js";
+
+const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform")!;
+const report = process.report as NodeJS.ProcessReport & { excludeNetwork: boolean };
+
+describe("localAgentRunnerTarget", () => {
+ beforeEach(() => {
+ Object.defineProperty(process, "platform", { ...originalPlatform, value: "linux" });
+ resetLocalAgentRunnerTargetCacheForTests();
+ report.excludeNetwork = false;
+ });
+
+ afterEach(() => {
+ Object.defineProperty(process, "platform", originalPlatform);
+ resetLocalAgentRunnerTargetCacheForTests();
+ vi.restoreAllMocks();
+ });
+
+ it("reads glibc from a report that excludes network data, then restores the setting", () => {
+ const excludeNetworkDuringReport: boolean[] = [];
+ vi.spyOn(process.report, "getReport").mockImplementation(() => {
+ excludeNetworkDuringReport.push(report.excludeNetwork);
+ return { header: { glibcVersionRuntime: "2.36" } } as never;
+ });
+
+ expect(localAgentRunnerTarget()).toMatchObject({ platform: "linux", libc: "glibc", glibcVersion: "2.36" });
+ // Network data makes the report reverse-resolve every open socket with
+ // synchronous DNS, which can stall the server.
+ expect(excludeNetworkDuringReport).toEqual([true]);
+ expect(report.excludeNetwork).toBe(false);
+ });
+
+ it("generates the report once per process", () => {
+ const getReport = vi.spyOn(process.report, "getReport")
+ .mockReturnValue({ header: { glibcVersionRuntime: "2.36" } } as never);
+
+ for (let index = 0; index < 5; index += 1) localAgentRunnerTarget();
+
+ expect(getReport).toHaveBeenCalledTimes(1);
+ });
+
+ it("reports an unknown libc once when the report fails, and restores the setting", () => {
+ const getReport = vi.spyOn(process.report, "getReport").mockImplementation(() => {
+ throw new Error("report unavailable");
+ });
+
+ expect(localAgentRunnerTarget()).toMatchObject({ libc: "unknown" });
+ expect(localAgentRunnerTarget()).toMatchObject({ libc: "unknown" });
+ expect(getReport).toHaveBeenCalledTimes(1);
+ expect(report.excludeNetwork).toBe(false);
+ });
+
+ it("does not generate a report off Linux", () => {
+ Object.defineProperty(process, "platform", { ...originalPlatform, value: "darwin" });
+ const getReport = vi.spyOn(process.report, "getReport");
+
+ expect(localAgentRunnerTarget()).toEqual({ driver: "local", platform: "darwin", architecture: process.arch });
+ expect(getReport).not.toHaveBeenCalled();
+ });
+});
diff --git a/server/src/__tests__/dev-runner-native-binary.test.ts b/server/src/__tests__/dev-runner-native-binary.test.ts
index eba039ec3f..a460176f43 100644
--- a/server/src/__tests__/dev-runner-native-binary.test.ts
+++ b/server/src/__tests__/dev-runner-native-binary.test.ts
@@ -2,7 +2,7 @@ import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
-import { localPaperclipRunnerSupported } from "../lib/agent-runner-platform.js";
+import { localPaperclipRunnerSupported, resetLocalAgentRunnerTargetCacheForTests } from "../lib/agent-runner-platform.js";
import {
paperclipRunnerBinaryNeedsBuild,
@@ -16,6 +16,7 @@ const initialArchitecture = Object.getOwnPropertyDescriptor(process, "arch")!;
afterEach(() => {
vi.restoreAllMocks();
+ resetLocalAgentRunnerTargetCacheForTests();
Object.defineProperty(process, "platform", initialPlatform);
Object.defineProperty(process, "arch", initialArchitecture);
for (const root of tempRoots) {
diff --git a/server/src/lib/agent-runner-platform.ts b/server/src/lib/agent-runner-platform.ts
index 1e67b12e4c..3a48fef06d 100644
--- a/server/src/lib/agent-runner-platform.ts
+++ b/server/src/lib/agent-runner-platform.ts
@@ -1,16 +1,45 @@
import { paperclipRunnerSupportsPlatform } from "@paperclipai/shared";
-export function localAgentRunnerTarget(): { driver: "local"; platform: NodeJS.Platform; architecture: string; libc?: "glibc" | "unknown"; glibcVersion?: string } {
- if (process.platform !== "linux") return { driver: "local", platform: process.platform, architecture: process.arch };
- let libc: "glibc" | "unknown" = "unknown";
- let glibcVersion: string | undefined;
+type LinuxLibc = { libc: "glibc" | "unknown"; glibcVersion?: string };
+
+// `excludeNetwork` is part of the Node 24 report API, but the installed Node
+// type definitions do not declare it yet.
+type ProcessReportWithNetworkSetting = NodeJS.ProcessReport & { excludeNetwork: boolean };
+
+let cachedLinuxLibc: LinuxLibc | undefined;
+
+// The C library cannot change while the process runs, so detect it once.
+// `process.report.getReport()` blocks the event loop. Unless `excludeNetwork`
+// is set, it also resolves the endpoints of every open socket with synchronous
+// reverse DNS. A slow resolver then stalls the server for seconds per socket,
+// on every caller, so the report always excludes network data.
+function detectLinuxLibc(): LinuxLibc {
+ if (cachedLinuxLibc) return cachedLinuxLibc;
+ let detected: LinuxLibc = { libc: "unknown" };
+ const report = process.report as ProcessReportWithNetworkSetting;
+ const previousExcludeNetwork = report.excludeNetwork;
try {
- const { header } = process.report.getReport() as { header?: { glibcVersionRuntime?: unknown } };
+ report.excludeNetwork = true;
+ const { header } = report.getReport() as { header?: { glibcVersionRuntime?: unknown } };
if (typeof header?.glibcVersionRuntime === "string" && header.glibcVersionRuntime.trim()) {
- libc = "glibc";
- glibcVersion = header.glibcVersionRuntime.trim();
+ detected = { libc: "glibc", glibcVersion: header.glibcVersionRuntime.trim() };
}
- } catch { /* An unidentified libc must not select a GNU/Linux binary. */ }
+ } catch {
+ /* An unidentified libc must not select a GNU/Linux binary. */
+ } finally {
+ report.excludeNetwork = previousExcludeNetwork;
+ }
+ cachedLinuxLibc = detected;
+ return detected;
+}
+
+export function resetLocalAgentRunnerTargetCacheForTests(): void {
+ cachedLinuxLibc = undefined;
+}
+
+export function localAgentRunnerTarget(): { driver: "local"; platform: NodeJS.Platform; architecture: string; libc?: "glibc" | "unknown"; glibcVersion?: string } {
+ if (process.platform !== "linux") return { driver: "local", platform: process.platform, architecture: process.arch };
+ const { libc, glibcVersion } = detectLinuxLibc();
return { driver: "local", platform: process.platform, architecture: process.arch, libc, ...(glibcVersion === undefined ? {} : { glibcVersion }) };
}
diff --git a/server/src/services/agent-runner-selection.test.ts b/server/src/services/agent-runner-selection.test.ts
index 48734f355e..d893cada79 100644
--- a/server/src/services/agent-runner-selection.test.ts
+++ b/server/src/services/agent-runner-selection.test.ts
@@ -11,12 +11,12 @@ vi.mock("@paperclipai/adapter-utils/ssh", () => ({ runSshCommand: state.ssh }));
import { AGENT_RUNNER_SSH_PLATFORM_COMMAND, agentRunnerAvailability, resolveAgentRunnerTargetForCompany, resolveNewAgentRunner, resolveNewAgentRunnerForCompany } from "./agent-runner-selection.js";
import { buildCodexAcpConfig, resolveCodexExecutionEngine } from "@paperclipai/adapter-codex-local/server";
import { secretService } from "./secrets.js";
-import { localAgentRunnerTarget } from "../lib/agent-runner-platform.js";
+import { localAgentRunnerTarget, resetLocalAgentRunnerTargetCacheForTests } from "../lib/agent-runner-platform.js";
const target = { driver: "local", platform: "linux", architecture: "x64" };
const qualifiedTargets = [target, { driver: "local", platform: "linux", architecture: "arm64" }, { driver: "local", platform: "darwin", architecture: "arm64" }, { driver: "local", platform: "darwin", architecture: "x64" }];
const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform")!;
const originalArchitecture = Object.getOwnPropertyDescriptor(process, "arch")!;
-afterEach(() => { vi.restoreAllMocks(); Object.defineProperty(process, "platform", originalPlatform); Object.defineProperty(process, "arch", originalArchitecture); });
+afterEach(() => { vi.restoreAllMocks(); resetLocalAgentRunnerTargetCacheForTests(); Object.defineProperty(process, "platform", originalPlatform); Object.defineProperty(process, "arch", originalArchitecture); });
describe("server-owned Codex runner selection", () => {
beforeEach(() => { state.disabled = []; state.overridden.clear(); state.settings = {}; state.getEnvironment.mockReset(); state.bindings.mockReset().mockResolvedValue([]); state.managedEnvironment.mockReset(); state.kubernetesEnvironment.mockReset().mockResolvedValue(null); state.ensureKubernetesEnvironment.mockReset(); state.ssh.mockReset().mockResolvedValue({ stdout: "Linux\nx86_64\n" }); state.sandboxQualification.mockReset().mockResolvedValue(null); state.resolveEnvironment.mockReset().mockResolvedValue({ driver: "ssh", config: {} }); });