Skip to content

Commit 4e87693

Browse files
committed
fix(server): report a scope stop that systemctl rejects
1 parent ffc4eeb commit 4e87693

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

‎apps/server/src/process/agentScope.test.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { describe, expect, it } from "@effect/vitest";
88
import * as HostProcess from "@t3tools/shared/HostProcess";
99
import * as Effect from "effect/Effect";
1010
import * as Layer from "effect/Layer";
11+
import * as Logger from "effect/Logger";
1112
import { ChildProcessSpawner } from "effect/process";
1213

1314
import * as ServerConfig from "../config.ts";
@@ -77,7 +78,10 @@ const withFakeSystemd = <A, E>(
7778
readonly cgroupRoot: string;
7879
}) => Effect.Effect<A, E>,
7980
// A user manager older than systemd 253 rejects OOMPolicy on scopes.
80-
{ oomPolicy = true }: { readonly oomPolicy?: boolean } = {},
81+
{
82+
oomPolicy = true,
83+
stopFails = false,
84+
}: { readonly oomPolicy?: boolean; readonly stopFails?: boolean } = {},
8185
) =>
8286
Effect.gen(function* () {
8387
const bin = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-agent-scope-"));
@@ -105,6 +109,13 @@ const withFakeSystemd = <A, E>(
105109
return { ...output(""), code: ChildProcessSpawner.ExitCode(1) };
106110
}
107111
const [, verb, unit = ""] = args;
112+
if (stopFails && verb === "stop") {
113+
return {
114+
...output(""),
115+
stderr: `Failed to stop ${args.at(-1)}: Unit not loaded.`,
116+
code: ChildProcessSpawner.ExitCode(5),
117+
};
118+
}
108119
if (verb === "reset-failed") units.delete(unit);
109120
if (verb !== "show") return output("");
110121
const state = units.get(unit);
@@ -250,6 +261,24 @@ describe("AgentScope service", () => {
250261
),
251262
);
252263

264+
it.effect("reports a scope stop that systemctl rejects", () => {
265+
const warnings: Array<unknown> = [];
266+
const logger = Logger.make(({ message }) => {
267+
warnings.push(Array.isArray(message) ? message[0] : message);
268+
});
269+
return withFakeSystemd(
270+
({ scope, units, calls, cgroupRoot }) =>
271+
Effect.gen(function* () {
272+
const agent = yield* scope.wrap({ command: "agent", args: [], name: "t", threadId: "a" });
273+
exitedAgent(units, cgroupRoot, unitOf(agent), { populated: true, oomKills: 0 });
274+
yield* scope.stop(unitOf(agent));
275+
expect(stopsOf(calls)).toHaveLength(1);
276+
expect(warnings).toContain("Could not stop an exited agent's scope");
277+
}).pipe(Effect.provide(Logger.layer([logger], { mergeWithExisting: false }))),
278+
{ stopFails: true },
279+
);
280+
});
281+
253282
it.effect("leaves a scope alone while its OOM count can't be read", () =>
254283
withFakeSystemd(({ scope, units, calls, cgroupRoot }) =>
255284
Effect.gen(function* () {

‎apps/server/src/process/agentScope.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,14 @@ export const make = Effect.gen(function* () {
337337
state.populated !== false &&
338338
state.activeState !== "deactivating";
339339
if (status !== "running" && !oomKilledWithoutPolicy) return;
340-
yield* run(scope.systemctl, ["--user", "stop", "--no-block", unit]);
340+
const stopped = yield* run(scope.systemctl, ["--user", "stop", "--no-block", unit]);
341+
if (stopped._tag === "None" || stopped.value.code !== 0) {
342+
// The next server start stops whatever is still left in agent scopes.
343+
yield* Effect.logWarning("Could not stop an exited agent's scope", {
344+
unit,
345+
stderr: stopped._tag === "Some" ? stopped.value.stderr.trim() : undefined,
346+
});
347+
}
341348
});
342349

343350
const clear: AgentScopeShape["clear"] = (threadId) =>

0 commit comments

Comments
 (0)