Skip to content

Commit bcd2407

Browse files
committed
fix(server): give queued searches a fresh active deadline
1 parent c7e5f96 commit bcd2407

3 files changed

Lines changed: 89 additions & 56 deletions

File tree

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

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ const decodeSearchRequest = Schema.decodeUnknownSync(SearchRequest);
1414
const encodeSearchResponse = Schema.encodeSync(SearchResponse);
1515

1616
const directories = new Map<number, string>();
17+
const hold = (value: unknown) => {
18+
const socket = NodeNet.connect(Number(process.env.T3_SEARCH_TEST_RECEIPT_PORT), "127.0.0.1", () =>
19+
socket.write("blocked"),
20+
);
21+
socket.once("data", () => {
22+
socket.end();
23+
process.send?.(encodeSearchResponse(Exit.succeed(value)));
24+
});
25+
};
1726
const block = () => {
1827
const socket = NodeNet.connect(
1928
Number(process.env.T3_SEARCH_TEST_RECEIPT_PORT),
@@ -30,6 +39,14 @@ process.on("message", (message) => {
3039
const { id, operation: input } = decodeSearchRequest(message);
3140
if (input.method === "initialize") directories.set(id, input.cwd);
3241
const cwd = directories.get(id) ?? "";
42+
if (
43+
input.method === "initialize" &&
44+
cwd === "hold-initialize" &&
45+
process.env.T3_SEARCH_TEST_HOLD_INITIALIZE === "1"
46+
) {
47+
hold(null);
48+
return;
49+
}
3350
if (input.method === "dispose") {
3451
if (cwd === "block-dispose") {
3552
block();
@@ -60,21 +77,9 @@ process.on("message", (message) => {
6077
return;
6178
}
6279
if (input.method === "search" && input.query === "hold") {
63-
const socket = NodeNet.connect(
64-
Number(process.env.T3_SEARCH_TEST_RECEIPT_PORT),
65-
"127.0.0.1",
66-
() => socket.write("blocked"),
67-
);
68-
socket.once("data", () => {
69-
socket.end();
70-
process.send?.(
71-
encodeSearchResponse(
72-
Exit.succeed({
73-
entries: [{ path: String(process.pid), kind: "file" }],
74-
truncated: false,
75-
}),
76-
),
77-
);
80+
hold({
81+
entries: [{ path: String(process.pid), kind: "file" }],
82+
truncated: false,
7883
});
7984
return;
8085
}

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

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -61,47 +61,42 @@ export const make = Effect.gen(function* () {
6161
const id = ++nextId;
6262
let released = false;
6363
const initialize: SearchOperation = { method: "initialize", cwd, variant };
64-
const run = Effect.fn("WorkspaceSearchHost.request")(
65-
function* (operation: SearchOperation) {
66-
yield* Effect.annotateCurrentSpan({ cwd, variant, operation: operation.method });
67-
if (closed || released)
68-
return yield* new WorkspaceSearchProcessFailed({
69-
cause: new Error("Workspace search index is closed."),
70-
});
71-
activeIndex = id;
72-
return yield* Effect.gen(function* () {
73-
const active = yield* Effect.gen(function* () {
74-
if (current) return current;
75-
const process = yield* startSearchProcess();
76-
current = { process, indexes: new Set<number>() };
77-
return current;
78-
}).pipe(Effect.uninterruptible);
79-
if (!active.indexes.has(id)) {
80-
yield* active.process.request({ id, operation: initialize });
81-
active.indexes.add(id);
82-
}
83-
return operation.method === "initialize"
84-
? null
85-
: yield* active.process.request({ id, operation });
86-
}).pipe(
87-
Effect.onError((cause) =>
88-
cause.reasons.length > 0 &&
89-
cause.reasons.every(
90-
(reason) => Cause.isFailReason(reason) && isRecoverable(reason.error),
91-
)
92-
? Effect.void
93-
: stop(),
94-
),
95-
Effect.ensuring(
96-
Effect.sync(() => {
97-
activeIndex = undefined;
98-
}),
99-
),
100-
);
101-
},
102-
semaphore.withPermits(1),
103-
Effect.timeout("20 seconds"),
104-
);
64+
const run = Effect.fn("WorkspaceSearchHost.request")(function* (operation: SearchOperation) {
65+
yield* Effect.annotateCurrentSpan({ cwd, variant, operation: operation.method });
66+
if (closed || released)
67+
return yield* new WorkspaceSearchProcessFailed({
68+
cause: new Error("Workspace search index is closed."),
69+
});
70+
activeIndex = id;
71+
return yield* Effect.gen(function* () {
72+
const active = yield* Effect.gen(function* () {
73+
if (current) return current;
74+
const process = yield* startSearchProcess();
75+
current = { process, indexes: new Set<number>() };
76+
return current;
77+
}).pipe(Effect.uninterruptible);
78+
if (!active.indexes.has(id)) {
79+
yield* active.process.request({ id, operation: initialize });
80+
active.indexes.add(id);
81+
}
82+
return operation.method === "initialize"
83+
? null
84+
: yield* active.process.request({ id, operation });
85+
}).pipe(
86+
Effect.timeout("20 seconds"),
87+
Effect.onError((cause) =>
88+
cause.reasons.length > 0 &&
89+
cause.reasons.every((reason) => Cause.isFailReason(reason) && isRecoverable(reason.error))
90+
? Effect.void
91+
: stop(),
92+
),
93+
Effect.ensuring(
94+
Effect.sync(() => {
95+
activeIndex = undefined;
96+
}),
97+
),
98+
);
99+
}, semaphore.withPermits(1));
105100

106101
yield* Effect.addFinalizer(() =>
107102
Effect.gen(function* () {

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,39 @@ it.effect(
9292
),
9393
);
9494

95+
it.effect("gives a queued request a fresh deadline for a delayed worker rebuild", () =>
96+
Effect.scoped(
97+
Effect.gen(function* () {
98+
const activeControl = yield* receipts();
99+
const rebuildControl = yield* receipts();
100+
yield* Effect.gen(function* () {
101+
const index = yield* Index.make("workspace");
102+
const other = yield* Index.make("hold-initialize");
103+
const original = yield* index.list();
104+
const blocked = yield* index.search("block", 1).pipe(Effect.exit, Effect.forkChild);
105+
yield* Deferred.await(activeControl.blocked);
106+
yield* TestClock.adjust("1 second");
107+
const queued = yield* other.list().pipe(
108+
Effect.provideService(HostProcessEnvironment, {
109+
...rebuildControl.environment,
110+
T3_SEARCH_TEST_HOLD_INITIALIZE: "1",
111+
}),
112+
Effect.forkChild,
113+
);
114+
yield* TestClock.adjust("19 seconds");
115+
expect(Exit.isFailure(yield* Fiber.join(blocked))).toBe(true);
116+
yield* Deferred.await(activeControl.exited);
117+
yield* Deferred.await(rebuildControl.blocked);
118+
yield* TestClock.adjust("2 seconds");
119+
yield* rebuildControl.release;
120+
const rebuilt = yield* Fiber.join(queued);
121+
expect(rebuilt.entries).not.toEqual(original.entries);
122+
expect((yield* index.list()).entries).toEqual(rebuilt.entries);
123+
}).pipe(Effect.provideService(HostProcessEnvironment, activeControl.environment));
124+
}).pipe(withWorker),
125+
),
126+
);
127+
95128
it.effect("cancels a blocked request before allowing the queued request to rebuild", () =>
96129
Effect.scoped(
97130
Effect.gen(function* () {

0 commit comments

Comments
 (0)