Skip to content
TypeScript

perf(server): idle Grok sessions on Linux stop polling /proc every 25 ms (#24502)

Victor Vogel91a9196d6449
diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
index 6f504940509..bc2314201cc 100644
--- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
+++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
@@ -10,6 +10,7 @@ import { AcpRegistrySettings } from "@t3tools/provider-acp-registry/settings";
import { AcpRegistryCatalog } from "@t3tools/provider-acp-registry/server/AcpRegistrySupport";
import { decodeAcpReplayTranscript, makeAcpReplayRuntime } from "./AcpAdapterV2.testkit.ts";
// @effect-diagnostics nodeBuiltinImport:off
+import * as NodeFS from "node:fs";
import * as NodePath from "node:path";
import * as NodeServices from "@effect/platform-node/NodeServices";
@@ -3506,6 +3507,105 @@ describe("AcpAdapterV2", () => {
}).pipe(Effect.provide(layerTest), Effect.scoped),
);
+ it.live("does not poll the process ledger while a cgroup lease owns teardown", () =>
+ Effect.gen(function* () {
+ if ((yield* HostProcess.Platform) !== "linux") return;
+ const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
+ const fileSystem = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const selfInvocation = yield* resolveSelfInvocation();
+ const mockAgentPath = yield* path.fromFileUrl(
+ new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
+ );
+ // A directory stands in for cgroupfs: the join wrapper appends the agent
+ // PID to cgroup.procs and stays in this process's cgroup, which is the
+ // cgroup the lease reports.
+ const leasePath = yield* fileSystem.makeTempDirectoryScoped();
+ const procsPath = path.join(leasePath, "cgroup.procs");
+ yield* fileSystem.writeFileString(procsPath, "");
+ const relativePath = AcpSessionRuntime.parseUnifiedCgroupPath(
+ NodeFS.readFileSync("/proc/self/cgroup", "utf8"),
+ );
+ // No unified cgroup on this host means no lease is possible, so there is
+ // nothing for this test to exercise.
+ if (relativePath === undefined) return;
+ const processTree = AcpSessionRuntime.makePosixProcessTreeController("linux");
+ const leasePids = () =>
+ NodeFS.readFileSync(procsPath, "utf8").split(/\s+/).filter(Boolean).map(Number);
+ const isLive = (pid: number) => {
+ const identity = processTree.identity(pid);
+ return identity !== undefined && !AcpSessionRuntime.posixProcessIsZombie(identity);
+ };
+ let leaseExists = true;
+ const linuxCgroupController: AcpSessionRuntime.AcpLinuxCgroupController = {
+ create: () => ({
+ contains: (pid) => leasePids().includes(pid),
+ exists: () => leaseExists,
+ path: leasePath,
+ relativePath,
+ kill: () => {
+ for (const pid of leasePids().filter(isLive)) process.kill(pid, "SIGKILL");
+ },
+ populated: () => leasePids().some(isLive),
+ remove: () => {
+ leaseExists = false;
+ },
+ }),
+ };
+ let childListReads = 0;
+ const posixProcessTreeController: AcpSessionRuntime.AcpPosixProcessTreeController = {
+ ...processTree,
+ childPidsOf: (pid) => {
+ childListReads += 1;
+ return processTree.childPidsOf(pid);
+ },
+ };
+ const instanceId = ProviderInstanceId.make("acp-test-cgroup-ledger-idle");
+ const adapter = yield* makeAcpAdapterV2({
+ instanceId,
+ flavor: {
+ driver: ACP_TEST_DRIVER,
+ capabilities: AcpProviderCapabilitiesV2,
+ makeRuntime: makeMockRuntime({
+ childProcessSpawner,
+ linuxCgroupController,
+ posixProcessTreeController,
+ mockAgentPath,
+ ownDescendantProcessGroups: true,
+ ownDetachedProcessGroup: true,
+ }),
+ },
+ selfInvocation,
+ });
+ const threadId = ThreadId.make("thread-acp-cgroup-ledger-idle");
+ const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
+ runtimeMode: "full-access",
+ interactionMode: "default",
+ cwd: process.cwd(),
+ });
+ const modelSelection = { instanceId, model: "default" } as const;
+ const sessionScope = yield* Scope.fork(yield* Scope.Scope);
+ const runtime = yield* adapter
+ .openSession({
+ threadId,
+ providerSessionId: ProviderSessionId.make("provider-session-acp-cgroup-ledger-idle"),
+ modelSelection,
+ runtimePolicy,
+ })
+ .pipe(Effect.provideService(Scope.Scope, sessionScope));
+ yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy });
+ // The agent's children are listed once, to capture the root it may
+ // signal at teardown, and never again while the session is open.
+ assert.equal(childListReads, 1);
+
+ const agentPid = leasePids()[0];
+ assert.isDefined(agentPid);
+ yield* Scope.close(sessionScope, Exit.void);
+ assert.isFalse(isLive(agentPid!), "closing the session must still tear the agent down");
+ assert.isFalse(leaseExists, "closing the session must remove the lease");
+ }).pipe(Effect.provide(layerTest), Effect.scoped),
+ );
+
it.effect("negotiates and executes optional native session forks through the ACP runtime", () =>
Effect.gen(function* () {
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
diff --git a/packages/provider-acp/src/server/AcpSessionRuntime.ts b/packages/provider-acp/src/server/AcpSessionRuntime.ts
index 300a9359e40..1cf047370cd 100644
--- a/packages/provider-acp/src/server/AcpSessionRuntime.ts
+++ b/packages/provider-acp/src/server/AcpSessionRuntime.ts
@@ -1504,14 +1504,17 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
catch: (cause) =>
new EffectAcpErrors.AcpSpawnError({ command: options.spawn.command, cause }),
});
- yield* observePosixOwnershipLedgerContinuously({
- childQueues: posixOwnershipChildQueues,
- controller: posixController,
- frontier: posixOwnershipFrontier,
- ledger: posixOwnershipLedger,
- root: posixOwnershipRoot,
- rootPid: Number(child.pid),
- }).pipe(Effect.forkIn(runtimeScope));
+ // Cgroup teardown needs only the root identity captured above.
+ if (linuxCgroupLease === undefined) {
+ yield* observePosixOwnershipLedgerContinuously({
+ childQueues: posixOwnershipChildQueues,
+ controller: posixController,
+ frontier: posixOwnershipFrontier,
+ ledger: posixOwnershipLedger,
+ root: posixOwnershipRoot,
+ rootPid: Number(child.pid),
+ }).pipe(Effect.forkIn(runtimeScope));
+ }
}
const signalOwnedProcessGroup = (signal: NodeJS.Signals) =>