|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import path from "path" |
| 3 | +import { PassThrough } from "node:stream" |
| 4 | +import { withTestInstance } from "../fixture/fixture" |
| 5 | +import { LSPClient, safeWritable } from "@/lsp/client" |
| 6 | +import * as LSPServer from "@/lsp/server" |
| 7 | + |
| 8 | +function spawnFakeServer() { |
| 9 | + const { spawn } = require("child_process") |
| 10 | + const serverPath = path.join(__dirname, "../fixture/lsp/fake-lsp-server.js") |
| 11 | + return { |
| 12 | + process: spawn(process.execPath, [serverPath], { |
| 13 | + stdio: "pipe", |
| 14 | + }), |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +function createClient(handle: { process: unknown }) { |
| 19 | + return withTestInstance({ |
| 20 | + directory: process.cwd(), |
| 21 | + fn: (ctx) => |
| 22 | + LSPClient.create({ |
| 23 | + serverID: "fake", |
| 24 | + server: handle as unknown as LSPServer.Handle, |
| 25 | + root: process.cwd(), |
| 26 | + directory: process.cwd(), |
| 27 | + instance: ctx, |
| 28 | + }), |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +describe("LSPClient dead server", () => { |
| 33 | + // Regression: a write to a language server whose stdin has been destroyed must |
| 34 | + // resolve as a no-op instead of erroring. vscode-jsonrpc emits writes we never |
| 35 | + // await (e.g. $/cancelRequest on request cancellation), so the failure is |
| 36 | + // uncatchable at the call site and previously terminated the worker ("Worker has |
| 37 | + // been terminated"). safeWritable must contain it at the stream boundary. |
| 38 | + test("safeWritable swallows writes to a destroyed stream instead of erroring", async () => { |
| 39 | + const destroyed = new PassThrough() |
| 40 | + destroyed.destroy() |
| 41 | + await new Promise((resolve) => setImmediate(resolve)) |
| 42 | + expect(destroyed.destroyed).toBe(true) |
| 43 | + |
| 44 | + // The raw stream errors on write after destroy (the source of the rejection). |
| 45 | + const rawError = await new Promise<Error | null>((resolve) => { |
| 46 | + try { |
| 47 | + destroyed.write(Buffer.from("x"), (err) => resolve(err ?? null)) |
| 48 | + } catch (err) { |
| 49 | + resolve(err as Error) |
| 50 | + } |
| 51 | + }) |
| 52 | + expect(rawError).not.toBeNull() |
| 53 | + |
| 54 | + // The wrapper completes the write callback with no error. |
| 55 | + const wrapped = safeWritable(destroyed) |
| 56 | + const wrappedError = await new Promise<Error | null>((resolve, reject) => { |
| 57 | + const timer = setTimeout(() => reject(new Error("write callback never fired")), 1000) |
| 58 | + wrapped.write(Buffer.from("hello"), (err) => { |
| 59 | + clearTimeout(timer) |
| 60 | + resolve(err ?? null) |
| 61 | + }) |
| 62 | + }) |
| 63 | + expect(wrappedError).toBeNull() |
| 64 | + }) |
| 65 | + |
| 66 | + // The wrapper must still forward writes to a live stream unchanged. |
| 67 | + test("safeWritable forwards writes to a live stream", async () => { |
| 68 | + const live = new PassThrough() |
| 69 | + const received = new Promise<string>((resolve) => { |
| 70 | + live.once("data", (chunk) => resolve(chunk.toString())) |
| 71 | + }) |
| 72 | + const wrapped = safeWritable(live) |
| 73 | + wrapped.write(Buffer.from("payload")) |
| 74 | + expect(await received).toBe("payload") |
| 75 | + }) |
| 76 | + |
| 77 | + // Regression: when the server process exits, the client must report closed and |
| 78 | + // notify listeners, so the pool can evict it and stop handing out a dead client. |
| 79 | + test("marks the client closed and notifies listeners when the server process exits", async () => { |
| 80 | + const handle = spawnFakeServer() as any |
| 81 | + const client = await createClient(handle) |
| 82 | + |
| 83 | + let notified = false |
| 84 | + client.onClose(() => { |
| 85 | + notified = true |
| 86 | + }) |
| 87 | + |
| 88 | + await new Promise<void>((resolve) => { |
| 89 | + handle.process.once("exit", () => resolve()) |
| 90 | + handle.process.kill("SIGKILL") |
| 91 | + }) |
| 92 | + await new Promise((resolve) => setTimeout(resolve, 100)) |
| 93 | + |
| 94 | + expect(client.closed).toBe(true) |
| 95 | + expect(notified).toBe(true) |
| 96 | + |
| 97 | + await client.shutdown().catch(() => {}) |
| 98 | + }) |
| 99 | + |
| 100 | + // After the server exits, notify.open must short-circuit instead of writing to a |
| 101 | + // dead server (and must not throw or reject). |
| 102 | + test("notify.open is a no-op after the server process exits", async () => { |
| 103 | + const handle = spawnFakeServer() as any |
| 104 | + const client = await createClient(handle) |
| 105 | + |
| 106 | + await new Promise<void>((resolve) => { |
| 107 | + handle.process.once("exit", () => resolve()) |
| 108 | + handle.process.kill("SIGKILL") |
| 109 | + }) |
| 110 | + await new Promise((resolve) => setTimeout(resolve, 100)) |
| 111 | + |
| 112 | + expect(client.closed).toBe(true) |
| 113 | + await expect(client.notify.open({ path: path.join(process.cwd(), "package.json") })).resolves.toBeDefined() |
| 114 | + |
| 115 | + await client.shutdown().catch(() => {}) |
| 116 | + }) |
| 117 | +}) |
0 commit comments