Skip to content

Commit 25b2858

Browse files
refactor(server): keep provider record cleanup out of Effect try/catch
AI-assisted; verified with the tests and tools listed above.
1 parent 497f31a commit 25b2858

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ const OwnershipRecord = Schema.Struct({
2727
});
2828
const decodeRecord = Schema.decodeUnknownSync(Schema.fromJsonString(OwnershipRecord));
2929

30+
// A concurrent exit can remove the record. Incomplete/unknown records never authorize a signal.
31+
const readRecord = (recordPath: string) => {
32+
try {
33+
return decodeRecord(NodeFS.readFileSync(recordPath, "utf8"));
34+
} catch {
35+
return undefined;
36+
}
37+
};
38+
3039
export const ProviderProcesses = Context.Reference<{
3140
readonly register?: (pid: number) => () => void;
3241
readonly hasOtherLiveOwner?: boolean;
@@ -48,6 +57,14 @@ const make = Effect.gen(function* () {
4857
};
4958
const matches = (expected: typeof ProcessIdentity.Type) =>
5059
identity(expected.pid)?.startTime === expected.startTime;
60+
// A failed signal only matters while the recorded process is still the same one.
61+
const kill = (survivor: typeof ProcessIdentity.Type) => {
62+
try {
63+
controller.signalProcess(survivor.pid, "SIGKILL");
64+
} catch (cause) {
65+
if (matches(survivor)) throw cause;
66+
}
67+
};
5168
const owner = identity(process.pid);
5269
if (owner === undefined) return yield* Effect.die(new Error("Cannot identify provider owner"));
5370
NodeFS.mkdirSync(directory, { recursive: true });
@@ -81,14 +98,8 @@ const make = Effect.gen(function* () {
8198
for (const file of NodeFS.readdirSync(directory)) {
8299
if (!file.endsWith(".json")) continue;
83100
const recordPath = NodePath.join(directory, file);
84-
let record: typeof OwnershipRecord.Type;
85-
try {
86-
record = decodeRecord(NodeFS.readFileSync(recordPath, "utf8"));
87-
} catch {
88-
// A concurrent exit can remove the record. Incomplete/unknown records
89-
// never authorize a signal.
90-
continue;
91-
}
101+
const record = readRecord(recordPath);
102+
if (record === undefined) continue;
92103
if (record.hostname !== hostname || record.stateDir !== stateDir) continue;
93104
if (matches(record.owner)) {
94105
if (record.owner.pid !== process.pid) hasOtherLiveOwner = true;
@@ -99,11 +110,7 @@ const make = Effect.gen(function* () {
99110
// Never infer ownership from argv, executable names, or group membership.
100111
// As in the ACP controller, starttime blocks reuse between observations.
101112
// Node has no pidfd_send_signal, so the final read-to-signal race remains.
102-
try {
103-
controller.signalProcess(survivor.pid, "SIGKILL");
104-
} catch (cause) {
105-
if (matches(survivor)) return yield* Effect.die(cause);
106-
}
113+
kill(survivor);
107114
for (let attempt = 0; matches(survivor); attempt++) {
108115
if (attempt === 100) {
109116
return yield* Effect.die(new Error("Recorded provider survived restart cleanup"));

0 commit comments

Comments
 (0)