GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(server): finished pull request sync sweeps no longer stay in memory · pingdotgg/t3code@e0a3936

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src/pullRequest
    • PullRequestReadCache.test.ts
    • PullRequestReadCache.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: apps/server/src/pullRequest/PullRequestReadCache.test.ts
+33Lines 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;
Collapse file

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

Copy file name to clipboardExpand all lines: apps/server/src/pullRequest/PullRequestReadCache.ts
+6-1Lines 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 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.