Skip to content

Commit e0a3936

Browse files
committed
fix(server): finished pull request sync sweeps no longer stay in memory
The one-minute pull request sync sweep reads through PullRequestReadCache. A cache entry keeps the fiber that filled it, and that fiber keeps the caller's lazy stack frames, which close over the whole sweep: every thread and link it loaded. With most pull requests merged or quiet, each sweep adds only a few entries, so the 512-entry cache held on to hundreds of old sweeps and the server grew by about 25 MB a minute on a database with 18,736 links. get and invalidate now detach their stack frames first, as GitManager.getPrLookup already does for its cache. Stack traces are kept as text.
1 parent 3ca62a5 commit e0a3936

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎apps/server/src/pullRequest/PullRequestReadCache.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as NodeV8 from "node:v8";
2+
import * as NodeVM from "node:vm";
13
import { assert, it } from "@effect/vitest";
24
import * as NodeServices from "@effect/platform-node/NodeServices";
35
import { PullRequestOperationError } from "@t3tools/contracts";
@@ -8,6 +10,9 @@ import * as FileSystem from "effect/FileSystem";
810
import * as KeyValueStore from "effect/persistence/KeyValueStore";
911
import * as PullRequestReadCache from "./PullRequestReadCache.ts";
1012

13+
NodeV8.setFlagsFromString("--expose-gc");
14+
const collectGarbage = NodeVM.runInNewContext("gc") as () => void;
15+
1116
const cacheLayer = (directory: string) =>
1217
PullRequestReadCache.make.pipe(Effect.provide(KeyValueStore.layerFileSystem(directory)));
1318

@@ -147,6 +152,34 @@ it.layer(NodeServices.layer)("PR filesystem cache", (it) => {
147152
}).pipe(Effect.provide(KeyValueStore.layerMemory)),
148153
);
149154

155+
it.effect("does not keep a caller's data alive after its reads and invalidations", () =>
156+
Effect.gen(function* () {
157+
const cache = yield* PullRequestReadCache.make;
158+
const sweeps: Array<WeakRef<object>> = [];
159+
// Like a sync sweep: an Effect.fn made per run that closes over that run's data.
160+
const sweep = Effect.fn("sweep")(function* (index: number) {
161+
const groups = new Map<string, number>(
162+
Array.from({ length: 1_000 }, (_, id) => [`group-${id}`, id]),
163+
);
164+
sweeps.push(new WeakRef(groups));
165+
const read = Effect.fn("read")(function* (pr: string) {
166+
const size = groups.size;
167+
yield* cache.invalidate(pr);
168+
const answer = yield* cache.get(`${pr}-${index}`, Effect.succeed("answer"), [pr]);
169+
return size + answer.length;
170+
});
171+
yield* Effect.forEach(["pr-1", "pr-2"], read, { discard: true });
172+
});
173+
for (let index = 0; index < 5; index++) yield* sweep(index);
174+
// WeakRef targets stay alive until the current job ends, so yield between collections.
175+
for (let pass = 0; pass < 3; pass++) {
176+
yield* Effect.yieldNow;
177+
collectGarbage();
178+
}
179+
assert.strictEqual(sweeps.filter((sweepData) => sweepData.deref() !== undefined).length, 0);
180+
}).pipe(Effect.provide(KeyValueStore.layerMemory)),
181+
);
182+
150183
it.effect("cancels abandoned reads without blocking invalidation", () =>
151184
Effect.gen(function* () {
152185
const cache = yield* PullRequestReadCache.make;

‎apps/server/src/pullRequest/PullRequestReadCache.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import * as Persistable from "effect/persistence/Persistable";
2121
import * as PersistedCache from "effect/persistence/PersistedCache";
2222
import * as Persistence from "effect/persistence/Persistence";
2323
import * as ServerConfig from "../config.ts";
24+
import { detachStackFrame } from "../git/detachStackFrame.ts";
2425
import { forkParked } from "../serverActivation.ts";
2526

2627
const CONCURRENT_READS = 512;
@@ -124,6 +125,9 @@ export const make = Effect.gen(function* () {
124125
}),
125126
{ capacity: CONCURRENT_READS, timeToLive: () => Duration.zero },
126127
);
128+
// A cache entry keeps the fiber that filled it, and that fiber keeps the caller's lazy stack
129+
// frames. Called from a sync sweep, those frames hold the whole sweep's thread data until the
130+
// entry is evicted, so every read and invalidation here detaches its frames first.
127131
return PullRequestReadCache.of({
128132
get: Effect.fn("PullRequestReadCache.get")(function* (key, lookup, scopes = []) {
129133
if (!enabled) return yield* lookup;
@@ -151,7 +155,7 @@ export const make = Effect.gen(function* () {
151155
}),
152156
lock.withPermits(1),
153157
);
154-
}),
158+
}, detachStackFrame),
155159
invalidate: Effect.fn("PullRequestReadCache.invalidate")(
156160
function* (scope) {
157161
const now = clock.currentTimeMillisUnsafe();
@@ -170,6 +174,7 @@ export const make = Effect.gen(function* () {
170174
}),
171175
Effect.uninterruptible,
172176
lock.withPermits(CONCURRENT_READS),
177+
detachStackFrame,
173178
),
174179
});
175180
});

0 commit comments

Comments
 (0)