Skip to content

Commit 5820ed5

Browse files
fix(agents): close LSP requests before shutdown
Reject retained tools at disposal and release outstanding request abort listeners before the shutdown handshake. Preserve normal cancellation, shutdown, exit, and final cleanup. Co-authored-by: qingminlong <[email protected]>
1 parent 081a77c commit 5820ed5

2 files changed

Lines changed: 75 additions & 15 deletions

File tree

‎src/agents/agent-bundle-lsp-runtime.test.ts‎

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -486,28 +486,85 @@ describe("bundle LSP runtime", () => {
486486
expect(killProcessTreeMock).not.toHaveBeenCalled();
487487
});
488488

489-
it("rejects retained tools after the LSP runtime is disposed", async () => {
489+
it.each([
490+
["lsp_hover_typescript", "textDocument/hover"],
491+
["lsp_definition_typescript", "textDocument/definition"],
492+
["lsp_references_typescript", "textDocument/references"],
493+
])("rejects retained %s requests throughout disposal", async (toolName, method) => {
490494
configureSingleLspServer();
491495
const child = new MockChildProcess();
492496
spawnMock.mockReturnValue(child);
493-
494497
const runtime = await createBundleLspToolRuntime({ workspaceDir: "/tmp/workspace" });
495-
const hoverTool = runtime.tools.find((tool) => tool.name === "lsp_hover_typescript");
496-
if (!hoverTool) {
497-
throw new Error("expected hover tool");
498+
const tool = runtime.tools.find((candidate) => candidate.name === toolName);
499+
if (!tool) {
500+
throw new Error(`expected ${toolName} tool`);
498501
}
502+
const input = { uri: "file:///tmp/workspace/index.ts", line: 0, character: 0 };
503+
await tool.execute("before-dispose", input);
499504

500-
await runtime.dispose();
505+
const disposal = runtime.dispose();
506+
const during = await tool.execute("during-dispose", input).then(
507+
() => "accepted",
508+
(error: unknown) => (error instanceof Error ? error.message : String(error)),
509+
);
510+
await disposal;
511+
const after = await tool.execute("after-dispose", input).then(
512+
() => "accepted",
513+
(error: unknown) => (error instanceof Error ? error.message : String(error)),
514+
);
501515

502-
await expect(
503-
hoverTool.execute("retained-call", {
504-
uri: "file:///tmp/workspace/index.ts",
505-
line: 0,
506-
character: 0,
507-
}),
508-
).rejects.toThrow("LSP session disposed");
509-
expect(child.receivedMessages).not.toContainEqual(
510-
expect.objectContaining({ method: "textDocument/hover" }),
516+
expect({ during, after }).toEqual({
517+
during: "LSP session disposed",
518+
after: "LSP session disposed",
519+
});
520+
expect(child.receivedMessages.filter((message) => message.method === method)).toHaveLength(1);
521+
expect(child.receivedMessages.filter((message) => message.method === "shutdown")).toHaveLength(
522+
1,
511523
);
524+
expect(child.receivedMessages.filter((message) => message.method === "exit")).toHaveLength(1);
525+
expect(killProcessTreeMock).toHaveBeenCalledTimes(1);
526+
});
527+
528+
it("rejects outstanding requests before shutdown and detaches their abort listeners", async () => {
529+
configureSingleLspServer();
530+
const child = new MockChildProcess("", new Set(["initialize"]));
531+
spawnMock.mockReturnValue(child);
532+
const runtime = await createBundleLspToolRuntime({ workspaceDir: "/tmp/workspace" });
533+
const hover = runtime.tools.find((tool) => tool.name === "lsp_hover_typescript");
534+
if (!hover) {
535+
throw new Error("expected hover tool");
536+
}
537+
const controller = new AbortController();
538+
const pending = hover
539+
.execute(
540+
"pending",
541+
{
542+
uri: "file:///tmp/workspace/index.ts",
543+
line: 0,
544+
character: 0,
545+
},
546+
controller.signal,
547+
)
548+
.then(
549+
() => "accepted",
550+
(error: unknown) => (error instanceof Error ? error.message : String(error)),
551+
);
552+
553+
const disposal = runtime.dispose();
554+
const shutdown = child.receivedMessages.find((message) => message.method === "shutdown");
555+
controller.abort(new Error("caller aborted during shutdown"));
556+
const outcome = await pending;
557+
child.stdout.write(encodeLspMessage({ jsonrpc: "2.0", id: shutdown?.id, result: null }));
558+
await disposal;
559+
560+
expect(outcome).toBe("LSP session disposed");
561+
expect(child.receivedMessages.map((message) => message.method)).toEqual([
562+
"initialize",
563+
"initialized",
564+
"textDocument/hover",
565+
"shutdown",
566+
"exit",
567+
]);
568+
expect(killProcessTreeMock).toHaveBeenCalledTimes(1);
512569
});
513570
});

‎src/agents/agent-bundle-lsp-runtime.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ function sendRequest(
257257
params?: unknown,
258258
signal?: AbortSignal,
259259
): Promise<unknown> {
260+
// Disposal closes tool requests before the child finishes its shutdown handshake.
260261
if (session.disposed && method !== "shutdown") {
261262
return Promise.reject(lspSessionDisposedError());
262263
}
@@ -387,6 +388,8 @@ async function disposeSession(session: LspSession) {
387388
return;
388389
}
389390
session.disposed = true;
391+
// Release abort listeners before shutdown forbids cancellation notifications.
392+
session.pendingRequests.rejectAll(lspSessionDisposedError());
390393
activeBundleLspSessions.delete(session);
391394

392395
if (session.initialized) {

0 commit comments

Comments
 (0)