Skip to content

Commit 0120d57

Browse files
committed
fix(server): concurrent index refreshes share one native progress read
Before: each index refresh that waited for a rescan polled fff's scan progress with its own native call. ffi-rs runs a `runInNewThread` call on Node's 4-thread libuv pool, so several refreshes that waited on fff's picker lock at the same time filled the pool. The event loop stayed responsive, but asynchronous file system calls in the server waited until the lock was released. After: the refreshes that wait on one finder share one in-flight progress read, so one finder holds at most two pool threads, one for the rescan call and one for the progress read. Description: - The fff-node patch adds `FileFinder.getScanProgressAsync()`. It returns the pending read when one exists, the same way `scanFilesAsync()` shares the rescan call. `waitForIndexReady` polls through it. - A new test in `WorkspaceEntries.test.ts` runs 8 concurrent refreshes on a real finder and expects at most one progress read in flight. With the sharing disabled it fails with "expected 8 to be 1". - Measured on Windows 11 with fff-node 0.11.0 and ffi-rs 1.3.2: a 272,000-file repository with a content index, a 250,000-file folder moved into it, and one finished run each second. Before, an `fs.promises.stat` waited 25.4 s and 24.1 s in two runs. After, no call waited 100 ms or longer. - Not covered: four finders that wait on their locks at the same time can still fill the pool.
1 parent f3bc16a commit 0120d57

3 files changed

Lines changed: 81 additions & 12 deletions

File tree

‎apps/server/src/workspace/WorkspaceEntries.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,50 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
792792
expect({ finders, rescans }).toEqual({ finders: 1, rescans: 1 });
793793
}),
794794
);
795+
796+
it.effect("shares one native progress read between concurrent refreshes", () =>
797+
Effect.gen(function* () {
798+
const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-refresh-pool-" });
799+
yield* writeTextFile(cwd, "src/notes.ts", "const needle = 1;\n");
800+
// Each read holds a libuv pool thread while it waits on fff's picker lock.
801+
const readsInFlight = new Set<Promise<unknown>>();
802+
let maxReadsInFlight = 0;
803+
const createFinder = FileFinder.create.bind(FileFinder);
804+
vi.spyOn(FileFinder, "create").mockImplementation((options) => {
805+
const created = createFinder(options);
806+
if (created.ok) {
807+
const finder = created.value;
808+
const getScanProgressAsync = finder.getScanProgressAsync.bind(finder);
809+
finder.getScanProgressAsync = () => {
810+
const read = getScanProgressAsync();
811+
if (!readsInFlight.has(read)) {
812+
readsInFlight.add(read);
813+
maxReadsInFlight = Math.max(maxReadsInFlight, readsInFlight.size);
814+
void read.finally(() => readsInFlight.delete(read));
815+
}
816+
return read;
817+
};
818+
}
819+
return created;
820+
});
821+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
822+
yield* workspaceEntries.searchContents({
823+
cwd,
824+
query: "needle",
825+
limit: 100,
826+
caseSensitive: true,
827+
wholeWord: false,
828+
useRegex: false,
829+
});
830+
831+
yield* Effect.all(
832+
Array.from({ length: 8 }, () => workspaceEntries.refresh(cwd)),
833+
{ concurrency: "unbounded" },
834+
);
835+
836+
expect(maxReadsInFlight).toBe(1);
837+
}),
838+
);
795839
});
796840

797841
describe("browse", () => {

‎patches/@[email protected]‎

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ index 0f90eedca20655ccd30b8f28e433b2094341fbdf..c2ef86927d6e3ab0e8584330fa0ac3fd
3030
/** Total number of items (files + dirs) that matched */
3131
totalMatched: number;
3232
diff --git a/dist/finder.d.ts b/dist/finder.d.ts
33-
index 452102d720cc19b0560cfc9e2f1a320687047c92..312046cb4195000ef91f41ffd07cf127e79be232 100644
33+
index 452102d720cc19b0560cfc9e2f1a320687047c92..092162277e7a906f65a559d874fbe19571e62133 100644
3434
--- a/dist/finder.d.ts
3535
+++ b/dist/finder.d.ts
3636
@@ -242,6 +242,11 @@ export declare class FileFinder implements FileFinderApi {
@@ -45,8 +45,20 @@ index 452102d720cc19b0560cfc9e2f1a320687047c92..312046cb4195000ef91f41ffd07cf127
4545
/**
4646
* Check if a scan is currently in progress.
4747
*/
48+
@@ -254,6 +259,11 @@ export declare class FileFinder implements FileFinderApi {
49+
* Get the current scan progress.
50+
*/
51+
getScanProgress(): Result<ScanProgress>;
52+
+ /**
53+
+ * Read scan progress on a native thread. Concurrent callers share one call.
54+
+ * Patched in T3 Code.
55+
+ */
56+
+ getScanProgressAsync(): Promise<Result<ScanProgress>>;
57+
/**
58+
* Wait for the initial file scan to complete.
59+
* Non-blocking: polls `isScanning` and yields to the event loop between checks.
4860
diff --git a/dist/index.js b/dist/index.js
49-
index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..cf3dac957335027ba9cb33621fb8ef1041da261c 100644
61+
index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..9643e1614ff128bd0cef71368b531314dfdee678 100644
5062
--- a/dist/index.js
5163
+++ b/dist/index.js
5264
@@ -1,7 +1,7 @@
@@ -348,16 +360,17 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..cf3dac957335027ba9cb33621fb8ef10
348360
function ffiWaitForScan(handle, timeoutMs) {
349361
return callBoolResult(
350362
"fff_wait_for_scan",
351-
@@ -1429,6 +1510,8 @@ var FileFinder = class _FileFinder {
363+
@@ -1429,6 +1510,9 @@ var FileFinder = class _FileFinder {
352364
handle;
353365
/** Native ids of this instance's active watch subscriptions. */
354366
watchers = /* @__PURE__ */ new Set();
355367
+ pendingScan = null;
368+
+ pendingScanProgress = null;
356369
+ pendingNativeCalls = /* @__PURE__ */ new Set();
357370
constructor(handle) {
358371
this.handle = handle;
359372
}
360-
@@ -1485,6 +1568,16 @@ var FileFinder = class _FileFinder {
373+
@@ -1485,6 +1569,16 @@ var FileFinder = class _FileFinder {
361374
destroy() {
362375
if (this.handle !== null) {
363376
const handle = this.handle;
@@ -374,7 +387,7 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..cf3dac957335027ba9cb33621fb8ef10
374387
ffiDestroy(handle);
375388
this.handle = null;
376389
ffiWatchCleanupAfterDestroy(handle, this.watchers);
377-
@@ -1757,6 +1850,28 @@ var FileFinder = class _FileFinder {
390+
@@ -1757,6 +1851,42 @@ var FileFinder = class _FileFinder {
378391
if (!guard.ok) return guard;
379392
return ffiScanFiles(guard.value);
380393
}
@@ -392,6 +405,20 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..cf3dac957335027ba9cb33621fb8ef10
392405
+ this.pendingScan = scan;
393406
+ return scan;
394407
+ }
408+
+ /**
409+
+ * Read scan progress without blocking the JS thread. Patched in T3 Code: concurrent readers
410+
+ * share one call, because each call holds a libuv pool thread while it waits on the picker lock.
411+
+ */
412+
+ getScanProgressAsync() {
413+
+ const guard = this.ensureAlive();
414+
+ if (!guard.ok) return Promise.resolve(guard);
415+
+ if (this.pendingScanProgress) return this.pendingScanProgress;
416+
+ const read = this.runOnNativeThread(ffiGetScanProgressAsync(guard.value)).finally(() => {
417+
+ if (this.pendingScanProgress === read) this.pendingScanProgress = null;
418+
+ });
419+
+ this.pendingScanProgress = read;
420+
+ return read;
421+
+ }
395422
+ /** Keeps the handle alive until a native-thread call settles. Patched in T3 Code. */
396423
+ runOnNativeThread(call) {
397424
+ const tracked = call.catch((cause) => err(String(cause))).finally(() => {
@@ -403,15 +430,13 @@ index 7c1d722eabc8d8eb98998eb48c5fe9d70b749d10..cf3dac957335027ba9cb33621fb8ef10
403430
/**
404431
* Check if a scan is currently in progress.
405432
*/
406-
@@ -1838,7 +1953,10 @@ var FileFinder = class _FileFinder {
433+
@@ -1838,7 +1968,8 @@ var FileFinder = class _FileFinder {
407434
if (!guard.ok) return guard;
408435
const deadline = Date.now() + timeoutMs;
409436
while (true) {
410437
- const progress = this.getScanProgress();
411438
+ // Patched in T3 Code: the progress read takes the picker lock, so it runs on a native thread.
412-
+ const alive = this.ensureAlive();
413-
+ if (!alive.ok) return alive;
414-
+ const progress = await this.runOnNativeThread(ffiGetScanProgressAsync(alive.value));
439+
+ const progress = await this.getScanProgressAsync();
415440
if (!progress.ok) return progress;
416441
if (!progress.value.isScanning && progress.value.isWarmupComplete) {
417442
return { ok: true, value: true };

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)