Skip to content

Commit 1db36c6

Browse files
committed
fix(server): preserve active searches during index cleanup
1 parent 7e131b1 commit 1db36c6

4 files changed

Lines changed: 74 additions & 14 deletions

File tree

‎apps/server/scripts/workspace-search-mock.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ process.on("message", (message) => {
4242
return;
4343
}
4444
if (input.method === "search" && input.query === "crash") process.exit(1);
45+
if (input.method === "search" && input.query === "hold") {
46+
const socket = NodeNet.connect(
47+
Number(process.env.T3_SEARCH_TEST_RECEIPT_PORT),
48+
"127.0.0.1",
49+
() => socket.write("blocked"),
50+
);
51+
socket.once("data", () => {
52+
socket.end();
53+
process.send?.(
54+
encodeSearchResponse(
55+
Exit.succeed({
56+
entries: [{ path: String(process.pid), kind: "file" }],
57+
truncated: false,
58+
}),
59+
),
60+
);
61+
});
62+
return;
63+
}
4564
if (input.method === "refresh") {
4665
process.send?.(
4766
encodeSearchResponse(

‎apps/server/src/workspace/WorkspaceSearchHost.ts‎

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type { SearchOperation } from "./workspaceSearchProtocol.ts";
1313
const make = Effect.gen(function* () {
1414
const semaphore = yield* Semaphore.make(1);
1515
let current: { process: SearchProcess; indexes: Set<number> } | undefined;
16+
let activeIndex: number | undefined;
1617
let nextId = 0;
1718
let closed = false;
1819
const stop = Effect.fn("WorkspaceSearchHost.stop")(function* () {
@@ -41,6 +42,7 @@ const make = Effect.gen(function* () {
4142
return yield* new WorkspaceSearchProcessFailed({
4243
cause: new Error("Workspace search index is closed."),
4344
});
45+
activeIndex = id;
4446
return yield* Effect.gen(function* () {
4547
const active = yield* Effect.gen(function* () {
4648
if (current) return current;
@@ -55,7 +57,14 @@ const make = Effect.gen(function* () {
5557
return operation.method === "initialize"
5658
? null
5759
: yield* active.process.request({ id, operation });
58-
}).pipe(Effect.onError(stop));
60+
}).pipe(
61+
Effect.onError(stop),
62+
Effect.ensuring(
63+
Effect.sync(() => {
64+
activeIndex = undefined;
65+
}),
66+
),
67+
);
5968
},
6069
semaphore.withPermits(1),
6170
Effect.timeout("20 seconds"),
@@ -64,19 +73,20 @@ const make = Effect.gen(function* () {
6473
yield* Effect.addFinalizer(() =>
6574
Effect.gen(function* () {
6675
released = true;
67-
// Disposal must not wait behind a stuck search or native destructor.
68-
// Other handles rebuild lazily if this retires the shared process.
69-
yield* Effect.acquireUseRelease(
70-
semaphore.take(1).pipe(Effect.timeout("1 second"), Effect.onError(stop)),
71-
() =>
72-
Effect.gen(function* () {
73-
if (!current?.indexes.has(id)) return;
74-
current.indexes.delete(id);
75-
if (current.indexes.size === 0) return yield* stop();
76-
yield* current.process.request({ id, operation: { method: "dispose" } });
77-
}).pipe(Effect.timeout("1 second"), Effect.onError(stop)),
78-
() => semaphore.release(1),
79-
).pipe(Effect.catchCause(() => Effect.void));
76+
if (activeIndex === id) yield* stop();
77+
// Give unrelated requests their own deadline. The shorter disposal
78+
// deadline bounds the native destructor, not another workspace's scan.
79+
yield* Effect.gen(function* () {
80+
if (!current?.indexes.has(id)) return;
81+
current.indexes.delete(id);
82+
if (current.indexes.size === 0) return yield* stop();
83+
yield* current.process.request({ id, operation: { method: "dispose" } });
84+
}).pipe(
85+
Effect.timeout("1 second"),
86+
Effect.onError(stop),
87+
semaphore.withPermits(1),
88+
Effect.catchCause(() => Effect.void),
89+
);
8090
}),
8191
);
8292
yield* run(initialize);

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ const withWorker = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
2525
const receipts = Effect.fn(function* () {
2626
const blocked = yield* Deferred.make<void>();
2727
const exited = yield* Deferred.make<void>();
28+
const connection = yield* Deferred.make<NodeNet.Socket>();
2829
const server = NodeNet.createServer((socket) => {
30+
// The fixture can be killed while its receipt socket has a write in flight.
31+
socket.on("error", () => {});
32+
Deferred.doneUnsafe(connection, Effect.succeed(socket));
2933
socket.once("data", () => Deferred.doneUnsafe(blocked, Effect.void));
3034
socket.once("close", () => Deferred.doneUnsafe(exited, Effect.void));
3135
});
@@ -38,6 +42,9 @@ const receipts = Effect.fn(function* () {
3842
return {
3943
blocked,
4044
exited,
45+
release: Deferred.await(connection).pipe(
46+
Effect.flatMap((socket) => Effect.sync(() => socket.write("release"))),
47+
),
4148
environment: { ...process.env, T3_SEARCH_TEST_RECEIPT_PORT: String(address.port) },
4249
};
4350
});
@@ -225,3 +232,26 @@ it.effect("cancels a queued request without terminating the active search proces
225232
}).pipe(withWorker),
226233
),
227234
);
235+
236+
it.effect("lets an active workspace finish while an unrelated index expires", () =>
237+
Effect.scoped(
238+
Effect.gen(function* () {
239+
const control = yield* receipts();
240+
yield* Effect.gen(function* () {
241+
const scope = yield* Scope.make();
242+
yield* Effect.addFinalizer(() => Scope.close(scope, Exit.void));
243+
yield* Index.make("retiring").pipe(Scope.provide(scope));
244+
const surviving = yield* Index.make("surviving");
245+
const before = yield* surviving.list();
246+
const active = yield* surviving.search("hold", 1).pipe(Effect.exit, Effect.forkChild);
247+
yield* Deferred.await(control.blocked);
248+
const closing = yield* Scope.close(scope, Exit.void).pipe(Effect.forkChild);
249+
yield* TestClock.adjust("1 second");
250+
yield* control.release;
251+
expect(yield* Fiber.join(active)).toEqual(Exit.succeed(before));
252+
yield* Fiber.join(closing);
253+
expect((yield* surviving.list()).entries).toEqual(before.entries);
254+
}).pipe(Effect.provideService(HostProcessEnvironment, control.environment));
255+
}).pipe(withWorker),
256+
),
257+
);

‎knip.jsonc‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"src/bin.ts!",
2323
"src/service-launcher.ts!",
2424
"src/claudeHistoryWorker.ts!",
25+
"src/workspaceSearchWorker.ts!",
2526
"scripts/cli.ts",
2627
"src/provider/testFixtures/*.mjs",
2728
],

0 commit comments

Comments
 (0)