Repository navigation
fix(lsp): report a server that exits instead of hanging on it - #95
Conversation
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: adcd0cd6-d1ec-42c8-bcbe-664f48e1c030) |
There was a problem hiding this comment.
This is an auto review done by revuto.
Revuto completed the review and found no evidence-backed concerns.
080032b to
61f99f3
Compare
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: f04bcc54-658e-4ce9-89b0-394943dadb7c) |
There was a problem hiding this comment.
This is an auto review done by revuto.
Revuto completed the review and found no evidence-backed concerns.
|
Verdict: needs changes. One blocking item, and it is a one-line test fix. The runtime changes work as described: every probe below behaved as the PR body says. Blocking1. The
Evidence. I removed the
A replay of the test steps against that build shows why. As written, Fix: let const op = client.hover(handle, file, POS, controller.signal);
// requireReady is async: let hover start the open before the abort.
await new Promise((resolve) => process.nextTick(resolve));
controller.abort();With that line, the test fails 5 of 5 against the unbounded build, and passes 10 of 10 unpinned and 5 of 5 pinned on this PR. Keeping Non-blocking
Temp dirs in /tmpThis should not be fixed in #95. The leak is not limited to lsp. One full Checked and fine
What I ran
|
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: 4903a2ac-b175-44e4-a8b1-24386102ebdb) |
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: f3bd179b-1bbf-4a15-8e36-468b3537fd74) |
|
Re #95 (comment): all five points are addressed in 31360e6.
Startup with a broken stdin now also rejects with The temp-dir cleanup goes in a separate PR to main, as you suggested. Reran: harness-lsp build, typecheck and tests (53 pass, 24 in the spawn-client file). |
There was a problem hiding this comment.
This is an auto review done by revuto.
Revuto completed the review and found no evidence-backed concerns.
A full test run left about 220 directories in the system temp dir: write, skill, read, glob, grep, bash and lsp test helpers create one per test and never remove it, and the tools package's ledger test leaves two. Turbo's strict env mode also dropped TMPDIR, so the tests wrote to /tmp even when TMPDIR pointed elsewhere. Every package's vitest config now runs scripts/vitest-temp-dir.ts as a global setup. It creates a fresh directory under the inherited temp dir, points TMPDIR at it for the run, and removes it when the run ends. That covers what the helpers create, what source code under test writes through os.tmpdir(), and caches dependencies keep there. core and tools get a vitest config for it. turbo.json passes TMPDIR through to tasks and lists the setup script as a global dependency.
|
Verdict: needs changes. Two blocking items, both small. All five earlier findings are fixed, and each fix's test fails on 61f99f3 and passes on 31360e6. Blocking1. With the default timeouts, a slow start does not keep starting. The tool's budget is Probe: a fake server that answers
Same shape at small scale (both bounds 500 ms, server answers after 800 ms, calls 200 ms apart): Spec 5.3 ( Fix: default the initialize bound above the tool's budget, so the start can outlive the call that began it. 2. The changeset bump is Narrowing Nothing I could find relies on Non-blocking
Earlier findings, rechecked
The new spawn-client file against 61f99f3's source: 7 of 24 tests fail. They are the five new tests for items 2, 4 and 5, and the two #94 tests that now expect Paths probedPlain Node scripts against the built
What I ran
|
webfetch.test.ts shares one handler across tests and only tests that call setHandler replace it. "allows public IP" relies on the default, so under a shuffled order it saw whatever the previous test installed and failed (vitest --sequence.shuffle, seeds 7, 11, 13 and 17 of 12 tried). beforeEach now restores the default handler.
31360e6 to
74bcb30
Compare
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: 49f2498d-9211-4882-b622-cfd29bd9388c) |
|
Re #95 (comment): addressed in 74bcb30, rebased on main. The commit list now also shows bc23e07 (#98, already on main), because the base branch is still at e68a4ab.
Reran on 74bcb30: |
|
Verdict: needs changes. Three blocking items, all small. All four points from the last review are fixed, and nothing from earlier rounds regressed. Blocking1. When a start fails after its first call has left, no call reports why. The background start is new in this PR. If it ends in an exit or at the Probes against the built
Suggested fix: when This also covers a boundary the defaults leave open. 2. No test fails when the fix itself is reverted. A test that checks the default the client actually uses, with fake it("bounds initialize by DEFAULT_INITIALIZE_TIMEOUT_MS by default", async () => {
const client = createSpawnLspClient();
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
try {
const outcome = client
.ensureServer({ language: "fake", root, profile: fakeProfile("silent-on-initialize", root) })
.then(() => "ready", (e: unknown) => (e as Error).message);
let settled: string | undefined;
void outcome.then((m) => (settled = m));
await vi.advanceTimersByTimeAsync(DEFAULT_TIMEOUT_MS + 1);
expect(settled).toBeUndefined();
await vi.advanceTimersByTimeAsync(DEFAULT_INITIALIZE_TIMEOUT_MS - DEFAULT_TIMEOUT_MS);
expect(await outcome).toContain(`within ${DEFAULT_INITIALIZE_TIMEOUT_MS}ms`);
} finally {
vi.useRealTimers();
await client.closeSession();
}
}, HANG_LIMIT_MS);It passes on 74bcb30 (10 of 10 runs idle, 5 of 5 pinned to one CPU) and fails on the reverted line ("did not answer initialize within 30000ms" after 30 s of fake time). It runs in well under a second. 3. The new cancel-during-start test is flaky on a busy CPU. Abort once the start has begun, not on a clock. For example, wrap the client so const inner = createSpawnLspClient();
const cancel = new AbortController();
const client: LspClient = {
...inner,
ensureServer: (args) => {
const starting = inner.ensureServer(args);
cancel.abort();
return starting;
},
};
// makeSession(root, client, { ..., signal: cancel.signal }); closeSession on `inner`.That version passed 10 of 10 runs pinned and fails on 31360e6 with the old message. Non-blocking
The last review's points
New tests against 31360e6's source: all 4 new tests fail (the constant is missing; the scaled test on the message only; both cancel tests on the message). The other 24 pass. On 74bcb30 all 28 pass. Earlier rounds, recheckedProbes against the built
What I ran
|
Four ways the spawn client could wait forever or lose a server: A request in flight when the server process exited waited for the caller's abort, because vscode-jsonrpc rejects pending requests only on dispose. Every send now runs through one helper that rejects at once with LspServerExitedError once the process is gone. The error carries the exit code or signal and the server's last stderr output, and the lsp tool returns it as SERVER_CRASHED for that call. ensureServer waited forever when the server died during initialize, and had no bound on a server that never answered. It now rejects on exit or on a failed spawn (the spawn error, for example ENOENT), and initialize is bounded by the per-operation timeout, DEFAULT_TIMEOUT_MS from design spec section 8. createSpawnLspClient takes initializeTimeoutMs to override it. The server's stderr pipe was never read, so a server that wrote enough to it blocked. The client now drains it and keeps the last 4 KB for the exit error. A server whose stdin broke after startup stayed marked ready and was never re-spawned. A write that finds stdin broken while the process lives now marks the server crashed and stops it; closeSession waits for that stop too. Each case has a fake-server test that fails on the parent branch and passes here.
Address review on the server-exit fix. The exit-write test aborted in the same tick as the hover. With requireReady async, the abort landed before the open started, so the test passed without the exit bound. It now lets the hover run every step that needs no I/O before the abort, and fails again with the bound removed. A call that found stdin broken returned IO_ERROR although the client had already retired the server. It now rejects with LspServerCrashedError, the new base class of LspServerExitedError, and the tool reports SERVER_CRASHED. A server that closed stdout but stayed alive was never retired, so every later call failed with "Connection is closed". When the connection closes and the process does not exit within one stop step (2s), the server is retired the same way and calls waiting on it get SERVER_CRASHED. ensureServer takes the call's signal. The lsp tool's timeout now covers starting the server: a slow start returns TIMEOUT, the handshake goes on in the background, and the next call gets server_starting. ensureServer never returned a crashed handle, so the tool's crashed branch could not fire. The branch is gone, ServerHandle.state is starting or ready, and design spec section 5.5 now says an idle exit is re-spawned by the next call.
…ancelled Address review on the follow-up. With the defaults, the call budget and the initialize bound were both 30s. The call's timer fired first and returned TIMEOUT saying the server keeps starting, and the initialize bound then stopped that server, so a server slower than 30s never finished starting. The initialize bound now defaults to DEFAULT_INITIALIZE_TIMEOUT_MS, the 60s session backstop, which also caps every call's budget. A test pins that relation, and a scaled-down test checks the flow: TIMEOUT, then server_starting, then the result, from one spawn. The tool now tells its own timeout apart from the caller cancelling. A call whose signal is already aborted says it was cancelled before it started and sends nothing; one cancelled during a start or an operation says it was cancelled. Narrowing ServerHandle.state breaks consumer compiles, so the changeset is minor for harness-lsp and the umbrella. The Rust port matches the spec: ServerState has no Crashed variant and run has no crashed branch. harness-lsp goes to 0.2.0 and the harness-tools crate, which re-exports it, to 0.5.0.
Address review on the follow-up. When the call that began a start gave up, a later failure of that start (an exit, or the initialize limit) was dropped, and the next call quietly spawned a new server. The model saw TIMEOUT and server_starting over and over, never the exit code or stderr. The failed start's error now stays on its server entry until a caller has seen it. The next ensureServer for that key throws it once, so the tool returns SERVER_NOT_AVAILABLE with the reason, and the call after that starts a new server. Design spec 5.3 says so, and that the initialize limit must stay above the call budget. A fake-timer test checks the client's default initialize limit itself, so reverting the default fails a test. The cancel-during-start test now cancels once the start has begun instead of on a 200ms clock, which a busy machine could beat. The crate READMEs pin harness-lsp 0.2 and harness-tools 0.5.
74bcb30 to
c1709b5
Compare
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: 7cc5a992-f141-4a44-b4e6-ae7157ea64b8) |
|
Re #95 (comment): addressed in c1709b5, rebased on main (#99).
Reran on c1709b5: |
|
Verdict: ready to land. All four points from the last review are fixed, and each new test fails without its fix. Nothing from earlier rounds regressed. Three non-blocking notes; the first is a small gap in the new report. Non-blocking1. A call in the event-loop turn of the exit misses the stored error. When the child exits, Probe against A real call lands there only when its last await before 2. Spec text around the new report.
3. The message says "Failed to spawn" for a server that did spawn. The last review's points
Each new test without its fixc1709b5's
Stored report, in each orderProbes against the built
In every probe: no unhandled rejection, no live server, no active resource left, 0 listeners. Earlier rounds, rechecked
What I ran
|
Stacked on #94. Review and land #94 first.
What was wrong
Four places where the spawn client could wait forever or keep a dead server.
dispose(), not when the process exits. A hover in flight when the server died waited for the caller's abort. Through thelsptool that becameTIMEOUTafter 30s, notSERVER_CRASHED.ensureServerduringinitialize. A server that died before answeringinitializeleftensureServerpending forever. So did one that stayed alive and never answered. A missing binary rejected withCannot call write after a stream was destroyed.ready. Every later call reused it and failed, and it was never re-spawned.Fix
packages/lsp/src/spawnClient.ts:call(). When the process exits, the client builds anLspServerExitedErrorwith the exit code or signal and the server's stderr tail. Each in-flight send rejects with it at once. A send that fails because the process is gone reports the exit, not the broken pipe. Sends subscribe to a one-shot notice and unsubscribe when they settle, so a long-lived server holds no reference to finished requests.ensureServerrejects on exit and on a failed spawn, with the spawn error itself (for examplespawn ... ENOENT).initializeis bounded byinitializeTimeoutMs. It defaults toDEFAULT_TIMEOUT_MS, the 30s per-operation timeout in design spec section 8. That is the existing tool timeout. The spec's other startup number,SERVER_STARTUP_MAX_WAIT_MS(5s), is the indexing wait beforeserver_starting, not a handshake bound.closeSessionwaits for these stops too, even after a re-spawn replaced the entry. A failedinitializealso goes through this path, so a server that ignores SIGTERM is still stopped.ready, so each process's handlers only ever touch their own entry.packages/lsp/src/lsp.tsmapsLspServerExitedErrortoSERVER_CRASHED, as section 5.5 asks for the call that saw the death.LspServerExitedErrorandSpawnLspClientOptionsare exported. Sections 5.3 and 5.5 of the spec describe the new behavior.Tests
New cases in
packages/lsp/test/spawnClient.test.ts, with new fake-server modes. All eight fail on #94's head (e68a4ab) and pass here.abortedat the 5s signalLspServerExitedError, exit code 1, stderr text; next call re-spawnslsptoolTIMEOUTafter 5sSERVER_CRASHEDwith the stderr textensureServerstarts a new server; the old one is gone aftercloseSessioninitializeLspServerExitedError, exit code 3initialize(300ms bound)Cannot call write after a stream was destroyedENOENTinitializeThe file passed 10 runs in a row, and 5 more pinned to one CPU.
Real server loop
packages/harness-e2e/test/lsp-transport.test.tsagainst typescript-language-server: 40 of 40 runs idle and 40 of 40 pinned to one CPU passed, with no unhandled error.Checks
pnpm test:dependencies,pnpm lint:harness, andturbo run build typecheck testwith harness-e2e filtered out all pass. The test file also type-checks under the package's strict settings. No Rust changes.Review changes (31360e6)
exit-write test tested nothing. It now letshoverrun every step that needs no I/O before the abort, so the open is reading the file when the signal fires. With the 2sexitbound removed it fails 3 of 3 runs (10s test limit); with the bound it passes.IO_ERROR. The call that finds it now rejects withLspServerCrashedError, the new base class ofLspServerExitedError, and the tool returnsSERVER_CRASHEDwith "The next call starts it again." Startup gets the same error, with theEPIPEascause.crashedbranch. An idle exit is re-spawned by the next call, which proceeds; the spec now says so.ensureServernever returned a crashed handle, so the branch inlsp.tsis gone andServerHandle.stateisstartingorready. Third-party clients that returnedcrashedmust throw instead.SERVER_CRASHED, and the next call starts a new server.ensureServertakes a signal. Thelsptool now creates its timeout beforeensureServerand passes the signal, so one budget covers starting the server and the operation. A slow start returnsTIMEOUT; the handshake goes on in the background, bounded byinitializeTimeoutMs, and the next call getsserver_starting. An already-aborted signal spawns nothing. The tool also removes its listener fromsession.signalwhen the call ends.IO_ERROR: LSP error: write EPIPE, hoverSERVER_CRASHED, hoverabortedat the 5s signalLspServerCrashedErrorafter 2s; next call starts a new server; old one gone aftercloseSessionensureServerwith a 300ms signal on a silent serveraborted; the next call gets the same server asstartingensureServerwith an aborted signalreadyTIMEOUT"while the fake server was starting", thenserver_startingwrite EPIPELspServerCrashedErrorwith theEPIPEascauseAll spawn-client tests (24) pass; 7 of them fail on 61f99f3. The file passed 10 runs in a row, and 5 more pinned to one CPU.
lsp-transport.test.tspassed 40 of 40 runs idle and 40 of 40 pinned to one CPU, with no unhandled error.pnpm test:dependencies,pnpm lint:harnessandturbo run build typecheck testwithout harness-e2e pass.Review changes (74bcb30, rebased on main)
The branch is rebased on main, so this PR's commit list also shows bc23e07 (#98, already on main): the base branch
fix/lsp-close-epipeis still at e68a4ab.initializebound now defaults toDEFAULT_INITIALIZE_TIMEOUT_MS, the 60s session backstop (spec section 8), which also caps every call's budget. Spec 4, 5.3 and 8 and the changeset say so. Full-scale probe with the defaults and a server that answersinitializeafter 35s: call 1 at 30.0sTIMEOUT"The start goes on in the background; retry.", call 2 at 31.0sserver_starting, call 3 at 36.0s a hover, one spawn. On 31360e6 the same shape gaveTIMEOUTtwice and two spawns.minorfor harness-lsp and for the umbrella, which re-exports the narrowed type. It now spells out the break and what an adapter should do instead.ServerState::Crashedand the crashed branch inrun.rsare gone;ensure_serverreturns an existing entry's handle directly, as it already did in practice. harness-lsp goes to 0.2.0 and the harness-tools crate, which re-exports it, to 0.5.0.cargo test --workspace --lockedpasses.TIMEOUT"The start goes on in the background; retry.",server_starting, hover, one spawnTIMEOUT"exceeded 30000ms while the fake server was starting"harness-lsp: 57 tests pass; the spawn-client file passed 10 runs in a row and 5 pinned to one CPU.
lsp-transport.test.tspassed 40 of 40 idle and 40 of 40 pinned, no unhandled error.pnpm test:dependencies,pnpm lint:harness,turbo run build typecheck testwithout harness-e2e (39 of 39) andcargo test --workspace --lockedpass.Review changes, round 4 (c1709b5)
Rebased on main (#99). The Rust port gaps that predate this PR are tracked in #100.
ensureServerfor that key throws it once, so the tool returnsSERVER_NOT_AVAILABLEwith the reason, and the call after that starts a new server. Spec 5.3 and the changeset say so, and theDEFAULT_INITIALIZE_TIMEOUT_MSJSDoc says the limit must stay above the session's call budget.DEFAULT_TIMEOUT_MSand fails atDEFAULT_INITIALIZE_TIMEOUT_MS. With the client's default set back to 30s, it fails.harness-lsp = "0.2"andharness-tools = "0.5".TIMEOUT,TIMEOUT(second spawn)TIMEOUT,SERVER_NOT_AVAILABLE"did not answer initialize within 800ms" (1 spawn),TIMEOUT(2 spawns)TIMEOUT,TIMEOUTTIMEOUT,SERVER_NOT_AVAILABLEwith exit code 3 and the stderr text, 1 spawn"Before" is 28957b2, the rebased 74bcb30.
Checks on c1709b5:
pnpm test:dependencies,pnpm lint:harness,turbo run build typecheck testwithout harness-e2e (39 of 39, 60 harness-lsp tests),cargo test --workspace --locked(278 passed). Spawn-client file: 20 of 20 runs idle, 25 of 25 pinned to one CPU.lsp-transport.test.ts: 40 of 40 idle, 40 of 40 pinned, no unhandled error.Note
Medium Risk
Changes core LSP process lifecycle, timeouts, and a breaking public type; incorrect handling could still leak processes or mis-report errors, but behavior is heavily tested.
Overview
Fixes hangs and wrong errors when a spawned language server exits, stalls on
initialize, floods stderr, or breaks stdio while the harness still treats it as usable.The spawn client routes every JSON-RPC send through
call(), rejecting in-flight work immediately withLspServerExitedError(exit code/signal plus a 4 KB stderr tail) orLspServerCrashedErrorfor broken pipes. It drains stderr, boundsinitializewithDEFAULT_INITIALIZE_TIMEOUT_MS(60s, overridable viacreateSpawnLspClient), and **retire()**s servers after stdin breaks or stdout closes without exit.ensureServernow accepts the tool call'sAbortSignal: slow starts canTIMEOUTwhile startup continues; a failed start after the caller gave up surfaces once asSERVER_NOT_AVAILABLE.The
lsporchestrator applies one timeout budget to startup plus the operation, maps crash errors toSERVER_CRASHED, and distinguishes timeout vs cancellation in messages. Breaking: publicServerHandle.stateis onlystarting|ready(RustServerState::Crashedremoved; 0.2.0 / 0.5.0). Design spec and extensive fake-server tests document the behavior; Vitest runs use an isolatedTMPDIRvia shared global setup.Reviewed by Cursor Bugbot for commit c1709b5. Configure here.
Review: revuto stopped at its 3-round limit on this PR, so an independent review gated the landing: #95 (comment) (ready to land, no blocking findings).