Skip to content

fix(lsp): report a start that exits in the same turn as the next call - #101

Merged
avifenesh merged 2 commits into
mainfrom
fix/lsp-start-failure-report
Nov 5, 2026
Merged

avifenesh merged 2 commits into
mainfrom
fix/lsp-start-failure-report

Conversation

@avifenesh

@avifenesh avifenesh commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up to #95, from the last review's non-blocking notes (#95 (comment)).

1. A start that exits in the same turn as the next call

The exit handler marked a starting server crashed at once, but its start failed one event-loop turn later, when markGone ran from setImmediate. A call that reached ensureServer in that turn found the server crashed with no stored error. It started a second server, and the first start's reason was never reported.

A server still starting now stays starting until its start fails. The start's own handler marks it crashed together with the error to report, so no call sees it crashed without that error. A call in that turn gets server_starting, and the next one gets the report.

Test: abort the first ensureServer at once, then call ensureServer on every setImmediate turn against a server that exits with code 3 during initialize. It expects the exit error with the stderr text and one spawn.

main (c1709b5) this branch
Exit-turn test 2 spawns, 3 of 3 runs exit code 3 with the stderr text, 1 spawn

2. Spec and changeset

  • Section 3.9, SERVER_NOT_AVAILABLE: also when the server could not start (spawn error, exit during initialize, no initialize answer within the bound), including the once-only delayed report.
  • Section 5.5, first bullet: the next call replaces a ready server that exited while idle. A server that exits while still starting in the background is reported first, and counts as starting until then.
  • Section 8, first bullet: the next call can also get SERVER_NOT_AVAILABLE when the start failed meanwhile.
  • The pending lsp-server-exit changeset now names the missed initialize bound as a reported reason.

3. "Failed to start", not "Failed to spawn"

Every start failure was prefixed "Failed to spawn", even when the server spawned and then exited or missed the initialize bound. It now reads "Failed to start". No test pinned the old text. The background-exit test now asserts the full prefix, so it fails on main.

Checks

pnpm test:dependencies, pnpm lint:harness and turbo run build typecheck test without harness-e2e pass (39 of 39 tasks, 61 harness-lsp tests). cargo test --workspace --locked passes (278, no Rust changes). The spawn-client file (32 tests) passed 20 of 20 runs idle and 25 of 25 pinned to one CPU. lsp-transport.test.ts against typescript-language-server passed 30 of 30 idle and 30 of 30 pinned, with no unhandled error. Nothing was written to /tmp.


Note

Medium Risk
Touches LSP spawn lifecycle and error reporting timing; wrong state transitions could still cause duplicate spawns or lost failure reasons, though behavior is heavily covered by spawn-client tests.

Overview
Fixes a race in createSpawnLspClient: on process exit during initialize, the client used to flip internal state to crashed immediately while the real failure (stderr + LspServerExitedError) was deferred to the next event-loop turn via setImmediate. A ensureServer call in that gap saw a crashed entry with no stored start error, spawned a second server, and never surfaced the first failure.

Behavior change: markCrashed now leaves a server starting until the start promise fails and records unreportedStartError. Calls in the gap get server_starting (or the stored error on the next call), not a duplicate spawn. User-facing copy switches "Failed to spawn" → "Failed to start" in the TS and Rust lsp orchestrators.

Docs/changesets: agent-knowledge/design/lsp.md and changesets document SERVER_NOT_AVAILABLE for delayed start failures (including same-turn exit) and the initialize timeout as a reported reason. A new patch changeset captures this follow-up.

Reviewed by Cursor Bugbot for commit d0556f9. Configure here.

Address the last review notes on the server-exit change.

The exit handler marked a starting server crashed at once, but its start
failed one event-loop turn later, when markGone ran. A call in that turn
found the server crashed with no stored error, started a second server,
and the first start's reason was never reported. A server still starting
now stays "starting" until its start fails, and the start's handler marks
it crashed together with the error to report. A test calls ensureServer
on every turn and expects the exit text and one spawn.

Start failures read "Failed to start" instead of "Failed to spawn": the
server may have spawned and then exited or missed the initialize bound.

Design spec 3.9 (SERVER_NOT_AVAILABLE), 5.5 and 8 now mention the delayed
report, and the pending changeset names the missed initialize bound.
@cursor

cursor Bot commented Nov 5, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot 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: c84cc0ce-bdc9-4689-bd88-65f455376bd6)

@revuto-review revuto-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an auto review done by revuto.


Revuto completed the review and found no evidence-backed concerns.

@avifenesh

Copy link
Copy Markdown
Collaborator Author

Verdict: ready to land. The exit-turn fix works, the new test fails on c1709b5 and passes here, and every path that ends a start reports once with one spawn. No leak and no unhandled rejection in any probe or loop. Three non-blocking notes.

Non-blocking

1. The SERVER_NOT_AVAILABLE row lists three start failures, the code reports five. agent-knowledge/design/lsp.md:238 names a failed spawn, an exit during initialize and the missed bound. Two more come back the same way, to the waiting call and as the delayed report: a pipe that breaks during the start ("stopped reading its input (write EPIPE)", "closed its output and stopped answering"), and an error reply to initialize (the reject-initialize-slow-exit test). The SERVER_CRASHED row says "lost a pipe, while this call was using it", so a reader can file a broken pipe during a start there. The same short list is in 5.3 (lsp.md:311, "the exit code or signal and the stderr tail, or the missed bound") and in the changeset sentence this PR edits (.changeset/lsp-server-exit.md:6). A wording that covers all of them: "the spawn failed; it exited, lost a pipe or rejected initialize during the start; or it did not answer initialize within the bound".

2. The new comment is exact for exits and spawn errors, not for broken pipes. packages/lsp/src/spawnClient.ts:296-298 says a starting server stays starting until its start fails. When a pipe breaks during the start, retire() sets crashed at once (:215, called from :181 and :337), and the start's handler stores the error a few promise jobs later (:398). Only promise jobs of the same turn run in between, so no call can land there. The probes below get one spawn and one report on both pipe paths, on main and here. No behavior change needed. Either let retire() leave a starting entry to the start's handler, or say in the comment that a broken pipe marks it crashed in the same turn.

3. The Rust port still says "Failed to spawn". crates/lsp/src/run.rs:165 prefixes every ensure_server error that way, and the Rust ensure_server also fails after a successful spawn (the initialize reply). The spec does not pin the prefix and no Rust test does, so this is a one-line change here or an item for #100. Nothing parses the text: in the repo only lsp.ts:210, that Rust line, the test and the changeset carry it, and a code search over the org finds no other LSP use.

The fix, path by path

Probes against the built dist of this branch and of c1709b5, with a fake server per path. The first ensureServer is aborted at once, then ensureServer runs on every setImmediate turn, then on every setTimeout(0) turn. A hook registered after the client's own exit listener also calls ensureServer three times inside the exit turn.

Path that ends the start c1709b5 this branch
Exit during initialize, setImmediate loop 2 spawns 1 spawn, then the exit error with stderr
Exit during initialize, 3 calls in the exit turn 1 call starts a 2nd server; server 1's reason never comes back 3 x starting, then the report, 1 spawn
Spawn error (missing binary), 3 calls on the tick after the error event 1 call starts a 2nd server; server 1's report is lost 3 x starting, then ENOENT once, 1 spawn
No initialize answer within 500 ms 1 spawn, reported once same
stdin closed, initialized write gets EPIPE 1 spawn, reported once same
stdout closed, process alive (2 s grace) 1 spawn, reported once same
Answers initialize, then exits (ready, idle) replaced silently, no report same

The spawn-error row is only reachable from a tick right after the error emit, so no real call hits it. Reverting the markGone change at :303 alone still passes all 32 tests, which fits. Reverting the exit handler at :315 alone fails the new test with "expected 2 to be 1".

Through the lsp tool (call budget 100 ms, server delay 300 ms), for exit, the missed bound, EPIPE and stdout closed: TIMEOUT, then SERVER_NOT_AVAILABLE "Failed to start probe server: ..." with the reason, then TIMEOUT from a new start, then server_starting twice. c1709b5 gives the same sequence apart from the prefix: a tool call awaits the path, permission and root lookups first, so it reaches ensureServer after the exit turn. A missing binary gives SERVER_NOT_AVAILABLE "Failed to start probe server: spawn ... ENOENT" on each call.

Stored report, concurrency, close

  • Once: in every probe the report comes back to one call, and the call after it starts a new server.
  • Four calls at once after the failure: one report, one new start, two server_starting. 2 spawns.
  • closeSession with a stored error: returns in 0 ms. The next call starts fresh, with no stale report.
  • closeSession inside the exit turn: returns in 1 ms (0 ms on the spawn-error path). The late start failure lands on the dropped entry, and the next call starts fresh.
  • Every probe ends with no live child, no abort listener left on any signal, no active handle or timer above the baseline, and no unhandled rejection or uncaught exception.

What I ran

  • The new test on c1709b5's packages/lsp/src: failed 8 of 8, 3 unpinned and 5 pinned to one CPU, each with "expected 2 to be 1" at the spawn count. The whole file there: 2 failed (the new test, and the prefix assertion in the background-exit test), 30 passed. On this branch: 32 passed.
  • turbo run build typecheck test --filter='!@agent-sh/harness-e2e' --force: 39 of 39 tasks, 61 harness-lsp tests.
  • pnpm test:dependencies (3 of 3) and pnpm lint:harness (4 of 4) pass. changeset status: @agent-sh/harness-lsp and @agent-sh/harness-tools at minor; the new patch changeset folds into it.
  • cargo test --workspace --locked: 278 passed, 0 failed.
  • spawnClient.test.ts (32 tests): 20 of 20 runs unpinned and 25 of 25 pinned with taskset -c 0, host load 6 to 18.
  • packages/harness-e2e/test/lsp-transport.test.ts against typescript-language-server: 30 of 30 unpinned and 30 of 30 pinned.
  • In every loop run: no unhandled error, EPIPE or ERR_STREAM_DESTROYED in the log, and no server left after the run.
  • CI on 54140d3: TypeScript, Rust and CodeQL pass. revuto approved with no concerns. Bugbot skipped at its usage limit.

…n Rust

The SERVER_NOT_AVAILABLE row, section 5.3 and the changeset now also name a
pipe that breaks during the start and an error reply to initialize. The
spawn-client comment says how a broken pipe reaches the crashed state, and
the Rust port reports a start failure as Failed to start, like the TypeScript
client.
@avifenesh

Copy link
Copy Markdown
Collaborator Author

Addressed the three notes in d0556f9 (text only, plus the Rust message):

  1. The SERVER_NOT_AVAILABLE row, section 5.3 and .changeset/lsp-server-exit.md now also list a pipe that breaks during the start and an error reply to initialize.
  2. The comment at spawnClient.ts:296 now says a broken pipe during the start marks the server crashed first and stores the error later in the same turn.
  3. crates/lsp/src/run.rs now says "Failed to start", like the TypeScript client.

Ran pnpm test:dependencies, pnpm lint:harness, turbo build, typecheck and test for harness-lsp and its dependents (6 of 6 tasks), and cargo test -p harness-lsp --locked (16 passed).

@revuto-review revuto-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an auto review done by revuto.


Revuto completed the review and found no evidence-backed concerns.

@avifenesh
avifenesh merged commit d0556f9 into main Nov 5, 2026
8 checks passed
@avifenesh
avifenesh deleted the fix/lsp-start-failure-report branch November 5, 2026 01:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant